相关疑难解决方法(0)

在tensorflow中获取dynamic_rnn的最后一个输出?

我使用dynamic_rnn来处理MNIST数据:

# LSTM Cell
lstm = rnn_cell.LSTMCell(num_units=200,
                         forget_bias=1.0,
                         initializer=tf.random_normal)

# Initial state
istate = lstm.zero_state(batch_size, "float")

# Get lstm cell output
output, states = rnn.dynamic_rnn(lstm, X, initial_state=istate)

# Output at last time point T
output_at_T = output[:, 27, :]
Run Code Online (Sandbox Code Playgroud)

完整代码:http://pastebin.com/bhf9MgMe

lstm的输入是 (batch_size, sequence_length, input_size)

因此,尺寸output_at_T是(batch_size, sequence_length, num_units)在哪里num_units=200.

我需要沿sequence_length 维度获取最后一个输出.在上面的代码中,这是硬编码的27.但是,我sequence_length事先并不知道,因为它可以在我的应用程序中从批处理更改为批处理.

我试过了:

output_at_T = output[:, -1, :]
Run Code Online (Sandbox Code Playgroud)

但是它说负面索引还没有实现,我尝试使用占位符变量和常量(我可以理想地sequence_length为特定批次提供); 既没有奏效.

有什么方法可以在tensorflow atm中实现这样的东西吗?

python tensorflow

17
推荐指数
3
解决办法
8190
查看次数

Tensorflow LSTM中的c_state和m_state是什么?

Tensorflow r0.12的tf.nn.rnn_cell.LSTMCell文档将其描述为init:

tf.nn.rnn_cell.LSTMCell.__call__(inputs, state, scope=None)
Run Code Online (Sandbox Code Playgroud)

其中state如下:

state:如果state_is_tuple为False,则必须是状态Tensor,2-D,batch x state_size.如果state_is_tuple为True,则它必须是状态Tensors的元组,两者都是2-D,列大小为c_state和m_state.

它们是什么c_state以及m_state它们如何适合LSTM?我在文档中的任何地方都找不到对它们的引用.

以下是文档中该页面的链接.

python deep-learning lstm tensorflow

16
推荐指数
2
解决办法
9070
查看次数

如何从TensorFlow中的3-D Tensor中选择行?

我有一个logits尺寸张量[batch_size, num_rows, num_coordinates](即批次中的每个logit都是一个矩阵).在我的情况下,批量大小为2,有4行和4个坐标.

logits = tf.constant([[[10.0, 10.0, 20.0, 20.0],
                      [11.0, 10.0, 10.0, 30.0],
                      [12.0, 10.0, 10.0, 20.0],
                      [13.0, 10.0, 10.0, 20.0]],
                     [[14.0, 11.0, 21.0, 31.0],
                      [15.0, 11.0, 11.0, 21.0],
                      [16.0, 11.0, 11.0, 21.0],
                      [17.0, 11.0, 11.0, 21.0]]])
Run Code Online (Sandbox Code Playgroud)

我想选择第一批的第一行和第二行以及第二批的第二行和第四行.

indices = tf.constant([[0, 1], [1, 3]])
Run Code Online (Sandbox Code Playgroud)

所以期望的输出就是

logits = tf.constant([[[10.0, 10.0, 20.0, 20.0],
                      [11.0, 10.0, 10.0, 30.0]],
                     [[15.0, 11.0, 11.0, 21.0],
                      [17.0, 11.0, 11.0, 21.0]]])
Run Code Online (Sandbox Code Playgroud)

如何使用TensorFlow执行此操作?我尝试使用tf.gather(logits, indices)但它没有返回我的预期.谢谢!

tensorflow

6
推荐指数
1
解决办法
9265
查看次数

使用 seq2seq 时,tf.nn.dynamic_rnn 如何分别处理不同长度的输入?

我在这里问的是动态行为是如何工作的。这是上述 dynamic_rnn 函数的 tensorflow文档。此函数创建的 RNN 输入块的最大长度是多少?它会根据最大的输入填充所有较短的吗?

deep-learning tensorflow

2
推荐指数
1
解决办法
1550
查看次数

标签 统计

tensorflow ×4

deep-learning ×2

python ×2

lstm ×1