在TensorFlow中将一个LSTM的输出用作另一个LSTM的输入

2024-03-29 08:23:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我想建立一个基于LSTM的神经网络,它接受两种输入并预测两种输出。大致结构如下图所示

neural network structure

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 2Output 1无关,但Output 2肯定受到Output 1的影响。例如Output 1是水流Output 2是水质,因此水质受水流的影响

此代码运行良好,但我不确定此代码是否符合我的预期,即LSTM 2的工作受到LSTM 1输出的影响

请验证实施和推理是否正确


Tags: inimportoutputsizelentfnprandom
1条回答
网友
1楼 · 发布于 2024-03-29 08:23:20

This code runs fine, but I am not sure if this code does what I intend it to do, i.e. the working of LSTM 2 being influenced by the output of LSTM 1.

Please verify if the implementation and reasoning are right or wrong?

是的,实现和推理是正确的。将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)

# define model
inputs1 = Input(shape=(seq_len, in_features1))
inputs2 = Input(shape=(seq_len, in_features2))

lstm1 = LSTM(units, return_sequences=True)
lstm1_out = lstm1(inputs1)

lstm2_input = tf.keras.layers.concatenate([inputs2, lstm1_out])
lstm2 = LSTM(units)
lstm2_out = lstm2(lstm2_input)

dense1 = Dense(1)
dense1_out = dense1(lstm1_out)

dense2 = Dense(1)
dense2_out = dense2(lstm2_out)

希望有帮助!:)

相关问题 更多 >