如何在TensorFlow中创建和执行一个基本的LSTM网络?

2024-04-27 03:06:01 发布

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

我想创建一个基本的LSTM网络,它接受5维向量序列(例如nx5数组)并返回相应的4维隐藏和单元向量序列(nx4数组),其中N是时间步数。你知道吗

我怎样才能做到张量流?你知道吗

添加了

到目前为止,我得到了以下代码:

num_units = 4
lstm = tf.nn.rnn_cell.LSTMCell(num_units = num_units)

timesteps = 18
num_input = 5
X = tf.placeholder("float", [None, timesteps, num_input])
x = tf.unstack(X, timesteps, 1)
outputs, states = tf.contrib.rnn.static_rnn(lstm, x, dtype=tf.float32)

sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)

x_val = np.random.normal(size = (12,18,5))
res = sess.run(outputs, feed_dict = {X:x_val})
sess.close()

然而,还有许多悬而未决的问题:

  1. 为什么预设时间步数?LSTM不应该接受任意长度的序列吗?你知道吗
  2. 为什么要按时间步分割数据(使用unstack)?你知道吗
  3. 如何解释“输出”和“状态”?你知道吗

Tags: inputtf时间序列数组向量numsess
1条回答
网友
1楼 · 发布于 2024-04-27 03:06:01

Why number of time steps is preset? Shouldn't LSTM be able to accept sequences of arbitrary length?

如果您想接受任意长度的序列,我建议使用dynamic_rnn,您可以参考here来理解它们之间的区别。你知道吗

例如:

num_units = 4
lstm = tf.nn.rnn_cell.LSTMCell(num_units = num_units)

num_input = 5
X = tf.placeholder("float", [None, None, num_input])
outputs, states = tf.nn.dynamic_rnn(lstm, X, dtype=tf.float32)

sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)

x_val = np.random.normal(size = (12,18,5))
res = sess.run(outputs, feed_dict = {X:x_val})

x_val = np.random.normal(size = (12,16,5))
res = sess.run(outputs, feed_dict = {X:x_val})
sess.close()

dynamic_rnn在一个批处理中需要相同的长度,但是在一个批处理中需要任意长度时,可以在填充批处理数据之后使用sequence_length参数指定每个长度。你知道吗

We do we split data by time-steps (using unstack)?

只是static_rnn需要用unstack分割数据,这取决于它们不同的输入需求。static_rnn的输入形状是[timesteps,batch_size, features],这是形状[batch_size, features]的2D张量列表。但是dynamic_rnn的输入形状是[timesteps,batch_size, features][batch_size,timesteps, features],取决于time_major是真是假。你知道吗

How to interpret the "outputs" and "states"?

在LSTMCell中,states的形状是[2,batch_size,num_units ],一个[batch_size, num_units ]表示C,另一个[batch_size, num_units ]表示h。你可以看到下面的图片。你知道吗

enter image description here

enter image description here

同样,您将得到GRUCell中的statesis [batch_size, num_units ]的形状。你知道吗

outputs表示每个时间步的输出,因此默认情况下(time\u major=False)其形状是[batch_size, timesteps, num_units]。你很容易得出结论 state[1, batch_size, : ] == outputs[ batch_size, -1, : ]。你知道吗

相关问题 更多 >