如何在串联图层keras中添加附加项

时间:2018-10-01 20:04:25

标签: python machine-learning keras nlp keras-layer

Y = Dense(2)(Y)
Z = LSTM(128, return_sequences=False)(X)
Z = Dense(2)(Z)
M = concatenate([Y, Z,Y+Z])
M=Dense(4)(M)
M = Dense(2)(M)
# Add a softmax activation
M = Activation('softmax')(M)

# Create Model instance which converts sentence_indices into X.
model = Model(inputs=sentence_indices, outputs=M)

return model

给定的块是我的代码。在这里,我给出了部分代码。我想要的是我想使用M = concatenate([Y, Z])合并图层,效果很好。然后我想到了向Dense层添加更多变量,所以我添加了M = concatenate([Y, Z,Y+Z]);但是,它不起作用。它给了我这个错误:

Traceback (most recent call last):
  File "/home/sathiyakugan/PycharmProjects/internal-apps/apps/support-tools/EscalationApp/IMDBmodified.py", line 213, in <module>
    model = buildModel((maxLen,), word_to_vec_map, word_to_index)
  File "/home/sathiyakugan/PycharmProjects/internal-apps/apps/support-tools/EscalationApp/IMDBmodified.py", line 206, in buildModel
    model = Model(inputs=sentence_indices, outputs=M)
  File "/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/keras/engine/network.py", line 91, in __init__
    self._init_graph_network(*args, **kwargs)
  File "/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/keras/engine/network.py", line 235, in _init_graph_network
    self.inputs, self.outputs)
  File "/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/keras/engine/network.py", line 1412, in _map_graph_network
    tensor_index=tensor_index)
  File "/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/keras/engine/network.py", line 1399, in build_map
    node_index, tensor_index)
  File "/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/keras/engine/network.py", line 1399, in build_map
    node_index, tensor_index)
  File "/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/keras/engine/network.py", line 1399, in build_map
    node_index, tensor_index)
  File "/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/keras/engine/network.py", line 1399, in build_map
    node_index, tensor_index)
  File "/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/keras/engine/network.py", line 1371, in build_map
    node = layer._inbound_nodes[node_index]
AttributeError: 'NoneType' object has no attribute '_inbound_nodes' 

能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

您可以使用add功能界面(或等效的Add层):

A = add([Y, Z])
M = concatenate([Y, Z, A])