我正在尝试构建一个如下图所示的模型。我们的想法是采用多个分类特征(单热矢量)并将它们分开嵌入,然后将这些嵌入的矢量与用于LSTM的3D张量相结合。
使用 Keras2.0.2 中的以下代码,在创建具有多个输入的Model()
对象时,会引发类似于this问题的AttributeError: 'NoneType' object has no attribute 'inbound_nodes'
。任何人都可以帮我弄清问题是什么?
型号:
代码:
from keras.layers import Dense, LSTM, Input
from keras.layers.merge import concatenate
from keras import backend as K
from keras.models import Model
cat_feats_dims = [315, 14] # Dimensions of the cat_feats
emd_inputs = [Input(shape=(in_size,)) for in_size in cat_feats_dims]
emd_out = concatenate([Dense(20, use_bias=False)(inp) for inp in emd_inputs])
emd_out_3d = K.repeat(emd_out, 10)
lstm_input = Input(shape=(10,5))
merged = concatenate([emd_out_3d,lstm_input])
lstm_output = LSTM(32)(merged)
dense_output = Dense(1, activation='linear')(lstm_output)
model = Model(inputs=emd_inputs+[lstm_input], outputs=[dense_output])
#ERROR MESSAGE
Traceback (most recent call last):
File "C:\Program Files\Anaconda2\envs\mle-env\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-a9da7f276aa7>", line 14, in <module>
model = Model(inputs=emd_inputs+[lstm_input], outputs=[dense_output])
File "C:\Program Files\Anaconda2\envs\mle-env\lib\site-packages\keras\legacy\interfaces.py", line 88, in wrapper
return func(*args, **kwargs)
File "C:\Program Files\Anaconda2\envs\mle-env\lib\site-packages\keras\engine\topology.py", line 1648, in __init__
build_map_of_graph(x, seen_nodes, depth=0)
File "C:\Program Files\Anaconda2\envs\mle-env\lib\site-packages\keras\engine\topology.py", line 1644, in build_map_of_graph
layer, node_index, tensor_index)
File "C:\Program Files\Anaconda2\envs\mle-env\lib\site-packages\keras\engine\topology.py", line 1644, in build_map_of_graph
layer, node_index, tensor_index)
File "C:\Program Files\Anaconda2\envs\mle-env\lib\site-packages\keras\engine\topology.py", line 1639, in build_map_of_graph
next_node = layer.inbound_nodes[node_index]
AttributeError: 'NoneType' object has no attribute 'inbound_nodes'
答案 0 :(得分:5)
keras.backend.repeat是一个函数,而不是一个图层。请尝试使用keras.layers.core.RepeatVector。它具有与函数相同的功能。
emd_out_3d = RepeatVector(10)(emd_out)
答案 1 :(得分:0)
不仅针对这种情况,而且在一般情况下,如果您希望向模型中添加一些没有等效层实现的函数,则可以将该函数作为Lambda层。
例如,我需要在我的模型中添加axis = 1上的均值运算符。这是假设的代码,假设我当前的张量为xinput,输出张量被输出,代码应如下所示。
# suppose my tensor named xinput
meaner=Lambda(lambda x: K.mean(x, axis=1) )
agglayer = meaner(xinput)
output = Dense(1, activation="linear", name="output_layer")(agglayer)
代替使用Lambda函数,而是直接添加K.mean函数,您将得到相同的错误。