将连接的输入传递给 keras 中的 LSTM
Issue passing concatenated inputs to LSTM in keras
我有几个神经网络。它们的输出被连接起来,然后传递给 LSTM。
这是一个简化的代码片段:
import keras.backend as K
from keras.layers import Input, Dense, LSTM, concatenate
from keras.models import Model
# 1st NN
input_l1 = Input(shape=(1, ))
out_l1 = Dense(1)(input_l1)
# 2nd NN
input_l2 = Input(shape=(1, ))
out_l2 = Dense(1)(input_l2)
# concatenated layer
concat_vec = concatenate([out_l1, out_l2])
# expand dimensions to (None, 2, 1)
expanded_concat = K.expand_dims(concat_vec, axis=2)
lstm_out = LSTM(10)(expanded_concat)
model = keras.Model(inputs=[input_l1, input_l2], outputs=lstm_out)
不幸的是,我在最后一行收到错误消息:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-53-a16fe60c0fc3> in <module>
2 lstm_out = LSTM(10)(expanded_concat)
3
----> 4 model = keras.Model(inputs=[input_l1, input_l2], outputs=lstm_out)
/usr/local/lib/python3.9/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name + '` call to the ' +
90 'Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
/usr/local/lib/python3.9/site-packages/keras/engine/network.py in __init__(self, *args, **kwargs)
91 'inputs' in kwargs and 'outputs' in kwargs):
92 # Graph network
---> 93 self._init_graph_network(*args, **kwargs)
94 else:
95 # Subclassed network
/usr/local/lib/python3.9/site-packages/keras/engine/network.py in _init_graph_network(self, inputs, outputs, name)
228
229 # Keep track of the network's nodes and layers.
--> 230 nodes, nodes_by_depth, layers, layers_by_depth = _map_graph_network(
231 self.inputs, self.outputs)
232 self._network_nodes = nodes
/usr/local/lib/python3.9/site-packages/keras/engine/network.py in _map_graph_network(inputs, outputs)
1361 for x in outputs:
1362 layer, node_index, tensor_index = x._keras_history
-> 1363 build_map(x, finished_nodes, nodes_in_progress,
1364 layer=layer,
1365 node_index=node_index,
/usr/local/lib/python3.9/site-packages/keras/engine/network.py in build_map(tensor, finished_nodes, nodes_in_progress, layer, node_index, tensor_index)
1350 node_index = node.node_indices[i]
1351 tensor_index = node.tensor_indices[i]
-> 1352 build_map(x, finished_nodes, nodes_in_progress, layer,
1353 node_index, tensor_index)
1354
/usr/local/lib/python3.9/site-packages/keras/engine/network.py in build_map(tensor, finished_nodes, nodes_in_progress, layer, node_index, tensor_index)
1323 ValueError: if a cycle is detected.
1324 """
-> 1325 node = layer._inbound_nodes[node_index]
1326
1327 # Prevent cycles.
AttributeError: 'NoneType' object has no attribute '_inbound_nodes'
有办法解决吗?如果它很重要,我使用 PlaidML 后端作为支持独立 GPU 的 macOS 的唯一选择。
要实现此处的目标,您可以使用 Reshape 层,将输入转换为目标形状。
Keras 与 Tensorflow 集成。这是 Tensorflow 版本中的工作代码。
import tensorflow as tf
from tensorflow.keras.layers import Input, Dense, LSTM, concatenate
from tensorflow.keras.models import Model
# 1st NN
input_l1 = Input(shape=(1, ))
out_l1 = Dense(1)(input_l1)
# 2nd NN
input_l2 = Input(shape=(1, ))
out_l2 = Dense(1)(input_l2)
# concatenated layer
concat_vec = concatenate([out_l1, out_l2])
# expand dimensions to (None, 2, 1)
expanded_concat = tf.keras.layers.Reshape((2, 1))(concat_vec)
#expanded_concat = K.expand_dims(concat_vec, axis=2)
lstm_out = LSTM(10)(expanded_concat)
model = Model(inputs=[input_l1, input_l2], outputs=lstm_out)
model.summary()
输出:
Model: "model"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) [(None, 1)] 0
__________________________________________________________________________________________________
input_2 (InputLayer) [(None, 1)] 0
__________________________________________________________________________________________________
dense (Dense) (None, 1) 2 input_1[0][0]
__________________________________________________________________________________________________
dense_1 (Dense) (None, 1) 2 input_2[0][0]
__________________________________________________________________________________________________
concatenate (Concatenate) (None, 2) 0 dense[0][0]
dense_1[0][0]
__________________________________________________________________________________________________
reshape_1 (Reshape) (None, 2, 1) 0 concatenate[0][0]
__________________________________________________________________________________________________
lstm (LSTM) (None, 10) 480 reshape_1[0][0]
==================================================================================================
Total params: 484
Trainable params: 484
Non-trainable params: 0
__________________________________________________________________________________________________
我有几个神经网络。它们的输出被连接起来,然后传递给 LSTM。
这是一个简化的代码片段:
import keras.backend as K
from keras.layers import Input, Dense, LSTM, concatenate
from keras.models import Model
# 1st NN
input_l1 = Input(shape=(1, ))
out_l1 = Dense(1)(input_l1)
# 2nd NN
input_l2 = Input(shape=(1, ))
out_l2 = Dense(1)(input_l2)
# concatenated layer
concat_vec = concatenate([out_l1, out_l2])
# expand dimensions to (None, 2, 1)
expanded_concat = K.expand_dims(concat_vec, axis=2)
lstm_out = LSTM(10)(expanded_concat)
model = keras.Model(inputs=[input_l1, input_l2], outputs=lstm_out)
不幸的是,我在最后一行收到错误消息:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-53-a16fe60c0fc3> in <module>
2 lstm_out = LSTM(10)(expanded_concat)
3
----> 4 model = keras.Model(inputs=[input_l1, input_l2], outputs=lstm_out)
/usr/local/lib/python3.9/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name + '` call to the ' +
90 'Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
/usr/local/lib/python3.9/site-packages/keras/engine/network.py in __init__(self, *args, **kwargs)
91 'inputs' in kwargs and 'outputs' in kwargs):
92 # Graph network
---> 93 self._init_graph_network(*args, **kwargs)
94 else:
95 # Subclassed network
/usr/local/lib/python3.9/site-packages/keras/engine/network.py in _init_graph_network(self, inputs, outputs, name)
228
229 # Keep track of the network's nodes and layers.
--> 230 nodes, nodes_by_depth, layers, layers_by_depth = _map_graph_network(
231 self.inputs, self.outputs)
232 self._network_nodes = nodes
/usr/local/lib/python3.9/site-packages/keras/engine/network.py in _map_graph_network(inputs, outputs)
1361 for x in outputs:
1362 layer, node_index, tensor_index = x._keras_history
-> 1363 build_map(x, finished_nodes, nodes_in_progress,
1364 layer=layer,
1365 node_index=node_index,
/usr/local/lib/python3.9/site-packages/keras/engine/network.py in build_map(tensor, finished_nodes, nodes_in_progress, layer, node_index, tensor_index)
1350 node_index = node.node_indices[i]
1351 tensor_index = node.tensor_indices[i]
-> 1352 build_map(x, finished_nodes, nodes_in_progress, layer,
1353 node_index, tensor_index)
1354
/usr/local/lib/python3.9/site-packages/keras/engine/network.py in build_map(tensor, finished_nodes, nodes_in_progress, layer, node_index, tensor_index)
1323 ValueError: if a cycle is detected.
1324 """
-> 1325 node = layer._inbound_nodes[node_index]
1326
1327 # Prevent cycles.
AttributeError: 'NoneType' object has no attribute '_inbound_nodes'
有办法解决吗?如果它很重要,我使用 PlaidML 后端作为支持独立 GPU 的 macOS 的唯一选择。
要实现此处的目标,您可以使用 Reshape 层,将输入转换为目标形状。
Keras 与 Tensorflow 集成。这是 Tensorflow 版本中的工作代码。
import tensorflow as tf
from tensorflow.keras.layers import Input, Dense, LSTM, concatenate
from tensorflow.keras.models import Model
# 1st NN
input_l1 = Input(shape=(1, ))
out_l1 = Dense(1)(input_l1)
# 2nd NN
input_l2 = Input(shape=(1, ))
out_l2 = Dense(1)(input_l2)
# concatenated layer
concat_vec = concatenate([out_l1, out_l2])
# expand dimensions to (None, 2, 1)
expanded_concat = tf.keras.layers.Reshape((2, 1))(concat_vec)
#expanded_concat = K.expand_dims(concat_vec, axis=2)
lstm_out = LSTM(10)(expanded_concat)
model = Model(inputs=[input_l1, input_l2], outputs=lstm_out)
model.summary()
输出:
Model: "model"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) [(None, 1)] 0
__________________________________________________________________________________________________
input_2 (InputLayer) [(None, 1)] 0
__________________________________________________________________________________________________
dense (Dense) (None, 1) 2 input_1[0][0]
__________________________________________________________________________________________________
dense_1 (Dense) (None, 1) 2 input_2[0][0]
__________________________________________________________________________________________________
concatenate (Concatenate) (None, 2) 0 dense[0][0]
dense_1[0][0]
__________________________________________________________________________________________________
reshape_1 (Reshape) (None, 2, 1) 0 concatenate[0][0]
__________________________________________________________________________________________________
lstm (LSTM) (None, 10) 480 reshape_1[0][0]
==================================================================================================
Total params: 484
Trainable params: 484
Non-trainable params: 0
__________________________________________________________________________________________________