2017-05-31 25 views
1

埋め込みレイヤーがマスキングでどのように機能するかを理解しようとしています。AttributeError: '埋め込み'オブジェクトには、TensorFlowバックエンドの属性 'get_shape'がありません。

この単純なコードは、エラー:AttributeError: 'Embedding' object has no attribute 'get_shape'で失敗します。しかし、私はそれを解決する方法がわかりません。何かヒント?

import numpy as np 
from keras.layers import Input, Dense, LSTM 
from keras.layers.embeddings import Embedding 
from keras.layers.merge import Concatenate 
from keras.models import Model 
from keras.utils import plot_model 

trainExs = np.asarray([ [1, 2, 3], [2, 3, 1]]) 
trainLabels = np.asarray([[1, 1, 1], [2, 2, 2]]) 

print('Examples, shape:', trainExs.shape) 
print('Labels, shape:', trainLabels.shape) 

W = [[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]] 
symDim = 3 

# E M B E D D I N G S 
# symbol_in = Input(shape=(None, 1), dtype='float32', name='symbol_input') 
symbol_emb = Embedding(symDim+1, symDim, 
         weights=np.asarray(W), trainable=False, input_length=3) 

symbol_dense = Dense(symDim, use_bias=True, name='symbol_dense')(symbol_emb) 

output_layer = Dense(symDim, dtype='float32', name='output')(symbol_dense) 

# M O D E L 
model = Model(inputs=[symbol_emb], outputs=[output_layer]) 
model.compile(loss='mean_squared_error', optimizer='RMSprop', metrics=['accuracy']) 
# print(model.summary()) 

フルスタックトレースは次のとおりです。

D:\python\python.exe D:/workspace/TESTS/test/testEMb.py 
Using TensorFlow backend. 
Examples, shape: (2, 3) 
Labels, shape: (2, 3) 
Traceback (most recent call last): 
    File "D:/workspace/TESTS/test/testEMb.py", line 21, in <module> 
    symbol_dense = Dense(symDim, use_bias=True, name='symbol_dense')(symbol_emb) 
    File "D:\python\lib\site-packages\keras\engine\topology.py", line 541, in __call__ 
    self.assert_input_compatibility(inputs) 
    File "D:\python\lib\site-packages\keras\engine\topology.py", line 450, in assert_input_compatibility 
    ndim = K.ndim(x) 
    File "D:\python\lib\site-packages\keras\backend\tensorflow_backend.py", line 479, in ndim 
    dims = x.get_shape()._dims 
AttributeError: 'Embedding' object has no attribute 'get_shape' 

答えて

1

をあなたが入力としてモデルにsymbol_embを供給しているが、symbol_embがあなたの埋め込み層の名前であり、有効な入力ではありません。あなたはEmbeddinginput_lengthをこのように定義する必要はありません

input = Input(shape=input_shape) 
symbol_emb = Embedding(symDim+1, symDim, 
         weights=np.asarray(W), trainable=False)(input) 

... 
... 

model = Model(inputs=[input], outputs=[output_layer]) 

注:などの入力を定義します。

+0

michetonuありがとうございます。埋め込みレイヤーがモデルの最初のレイヤーでなければならないと言う文書から、埋め込みの前に入力レイヤーを追加できないと思っていました。 –

+0

この場合、input_shapeはどうなりますか? 1つの番号を受け取ったので、Wへのインデックス... –

+0

(3、)は機能しませんか? – michetonu

関連する問題