我想建立一个基于LSTM的神经网络,它接受两种输入并预测两种输出。大致结构如下图所示
Output 2
依赖于Output 1
,如在回答类似问题here时所述,我试图通过从lstm1的隐藏状态设置lstm2的初始状态来实现这一点。我使用以下代码使用TensorFlow实现了这一点
import tensorflow as tf
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import LSTM
from tensorflow.keras.layers import Dense
import numpy as np
np.set_printoptions(suppress=True) # to suppress scientific notation while printing arrays
def reset_graph(seed=2):
tf.compat.v1.reset_default_graph()
tf.random.set_seed(seed) # tf.set_random_seed(seed)
np.random.seed(seed)
tf.__version__
seq_len = 10
in_features1 = 3
in_features2 = 5
batch_size = 2
units = 5
# define input data
data1 = np.random.normal(0,1, size=(batch_size, seq_len, in_features1))
print('input 1 shape is', data1.shape)
data2 = np.random.normal(0,1, size=(batch_size, seq_len, in_features2))
print('input 2 shape is', data2.shape)
reset_graph()
# define model
inputs1 = Input(shape=(seq_len, in_features1))
inputs2 = Input(shape=(seq_len, in_features2))
lstm1 = LSTM(units, return_state=True)
lstm1_out, lstm_h, lstm_c = lstm1(inputs1, initial_state=None)
dense1 = Dense(1)
dense1_out = dense1(lstm1_out)
lstm2 = LSTM(units)
lstm2_out = lstm2(inputs2, initial_state=[lstm_h, lstm_c])
dense2 = Dense(1)
dense2_out = dense2(lstm2_out)
两个LSTM的输入不完全相同,因为一些Input 2
与Output 1
无关,但Output 2
肯定受到Output 1
的影响。例如Output 1
是水流Output 2
是水质,因此水质受水流的影响
此代码运行良好,但我不确定此代码是否符合我的预期,即LSTM 2
的工作受到LSTM 1
输出的影响
请验证实施和推理是否正确
是的,实现和推理是正确的。将LSTM1的最终状态作为LSTM2的初始状态传递会创建您要查找的依赖项。然而,我并不完全相信这种解决办法。为了使LSTM2能够考虑output1,更自然的做法是将output1连接到input2,让LSTM2学习从[input2,output1]提取特征。但无论如何,这两种解决方案都在您描述的input2和input1之间创建了依赖关系
将LSTM1的输出连接到input2的解决方案可以这样描述:
当LSTM1返回一个序列(
return_sequence=True
)时,您可以将LSTM1(seq_len, num_units)
的输出连接到imput2(seq_len, in_features2)
,从而产生(seq_len, num_units + in_features2)
希望有帮助!:)
相关问题 更多 >
编程相关推荐