我试图用keras编写一个模型,构建如下所示:
| +-----+
+->+------+ | |
+--->| NN |------>| |
| +------+ | |
| | | |
| +->+------+ | |
+--->| NN |------>| L |
| +------+ | S |------+-----> Output value
| | T | |
| ... | M | |
| | | | |
| +->+------+ | | |
+--->| NN |------>| | |
| +------+ | | |
| +-----+ |
| +-------+ |
+----| Delay |-------------------+
+-------+我有几个简单的顺序模型(标记为NN),它们接收两个数字值作为输入,它们计算其他一些数值(每个网络一个)。这些值被传递到LSTM网络,LSTM网络产生单个值作为输出,这个值被附加到初始网络(可能有延迟)作为两个输入之一。我使用时间序列,因此计算出的最终值与下一个时间序列值一起传递给网络。
我使用LSTM存储一些“状态”。构建单独的“子模型”并不困难,但我没有意识到,如何根据我的需要将它们连接在一起,即如何将最终的输出传递给初始网络,以及如何以描述的方式(而不是作为链)来堆叠它们。
我所发现的:我找到了keras.layers.Concatenate,但这似乎不是我要找的.但也许(我希望)我弄错了。
发布于 2021-03-03 07:54:26
您可以使用下面的类型代码。
import tensorflow as tf
from tensorflow.keras.layers import Input, Dense, LSTM, Concatenate
from tensorflow.keras.models import Model
# input of first NN
input_l1 = Input(shape=(2,))
out_l1 = Dense(1)(input_l1)
# input is 2nd NN
input_l2 = Input(shape=(2,))
out_l2 = Dense(1)(input_l2)
# concat layer output shape will be (None, 2) becuase we concatinated 2 dense layer outputs/
concat_vec = Concatenate()([out_l1, out_l2])
# we need 3d input to LSTM i.e. ( Batch_Size, no of time steps, feature space)
# We have 2 inputs so expanded dim to (None, 2, 1)
expanded_concat = tf.expand_dims(concat_vec, axis=2)
# LSTM
lstm_out = LSTM(15)(expanded_concat)
model = Model(inputs=[input_l1, input_l2], outputs=lstm_out)https://datascience.stackexchange.com/questions/90177
复制相似问题