使用一个LSTM的输出作为TensorFlow中另一个LSTM的输入 [英] Using output from one LSTM as input into another LSTM in TensorFlow

查看:503
本文介绍了使用一个LSTM的输出作为TensorFlow中另一个LSTM的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个基于LSTM的神经网络,该网络接受两种输入并预测两种输出。下图可以看到一个粗略的结构。





输出2 取决于输出1 ,并在回答类似问题此处,我试图通过从LSTM 1的隐藏状态设置LSTM 2的初始状态来实现这一点。我已经使用TensorFlow使用以下代码实现了这一点。

 从tensorflow.keras.layers导入tf 

tensorflow从tensorflow.keras.layers导入输入
导入LSTM $ b来自tensorflow.keras.layers的$ b导入密集
导入numpy为np

np.set_printoptions(suppress = True)#抑制科学性打印数组时的c符号

def reset_graph(seed = 2):
tf.compat.v1.reset_default_graph()
tf.random.set_seed(seed)#tf.set_random_seed (种子)
np.random.seed(种子)

tf .__ version__

seq_len = 10
in_features1 = 3
in_features2 = 5
batch_size = 2
个单位= 5

#定义输入数据
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()

#定义模型
input1 = Input(shape =( seq_len,in_features1))
input2 =输入(shape =(seq_len,in_features2))
lstm1 = LSTM(单位,return_state = True)
lstm1_out,lstm_h,lstm_c = lstm1(inputs1,initial_state =无)
density1 =密集(1)
density1_out =密集1(lstm1_out)

lstm2 = LSTM(单位)
lstm2_out = lstm2(inputs2 ,initial_state = [lstm_h,lstm_c])

density2 =密集(1)
density2_out = density2(lstm2_out)

两个LSTM的输入并不完全相同,因为某些输入2 输出1 ,但是输出2 肯定受输出1 的影响。例如,输出1 是水流,而输出2 是水质,因此水质受水流的影响。 / p>

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



请验证实现和推理是对还是错?

解决方案

< blockquote>

此代码运行良好,但是我不确定该代码是否达到了我打算执行的目标
,即LSTM 2的工作受到
输出的影响LSTM 1。



请验证实现和推理是对还是错?


是的,实现和推理是正确的。将LSTM1的最终状态作为LSTM2的初始状态传递会创建您要查找的依赖项。但是,我不完全相信此解决方案。为了使LSTM2能够考虑到output1,将输出1连接到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)

 #定义模型
input1 = Input(shape =(seq_len,in_features1))
输入2 =输入(shape =(seq_len,in_features2))

lstm1 = LSTM(单位,return_sequences = True)
lstm1_out = lstm1(inputs1)

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

density1 = Dense(1)
density1_out =密集1(lstm1_out)

密集2 =密集(1)
密集2_out =密集2(lstm2_out)

希望有帮助! :)


I want to build an LSTM-based neural network which takes two kinds of inputs and predicts two kinds of outputs. A rough structure can be seen in the following figure.

The Output 2 is dependent upon Output 1 and as described in answer to a similar question here, I have tried to implement this by setting the initial state of LSTM 2 from hidden states of LSTM 1. I have implemented this using TensorFlow using the following code.

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)

The inputs to two LSTMs are not exactly same because some of the Input 2 has nothing to do with Output 1, but Output 2 is definitely influenced by Output 1. For example Output 1 is water flow and Output 2 is water quality, so water quality is influenced by water flow.

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?

解决方案

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?

Yes, the implementation and reasoning are right. Passing the final state of LSTM1 as the initial state of LSTM2 creates the dependency you are looking for. However, I am not completely convinced of this solution. In order to LSTM2 be able to take into account output1 it's more natural to just concatenate output1 to the input2 and let LSTM2 learn to abstract features from [Input2, Output1]. But anyway, both solutions create the dependency between input2 and input1 your described.

The solution that concatenates the output of LSTM1 to input2 can be described like this:

As LSTM1 return a sequence (return_sequence=True) you can just concatenate the output of the LSTM1 (seq_len, num_units) to imput2 (seq_len, in_features2) resulting in (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)

Hope it helps! :)

这篇关于使用一个LSTM的输出作为TensorFlow中另一个LSTM的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆