python - Tensorflow 上的多维 RNN

标签 python tensorflow recurrent-neural-network

我正在尝试在人类行为分类的上下文中实现 2D RNN(关节在 RNN 的一个轴上,时间在另一轴上)并且一直在 Tensorflow 中到处寻找可以完成这项工作的东西。

我听说过 GridLSTMCell(internallyexternally 贡献)但无法让它与 dynamic_rnn 一起工作(接受 3-D 张量但我必须提供 4-D 张量 [batchsize、max_time、num_joints、n_features])。

此外,ndlstm 也是一个(有点未知)part TF 库的一部分,它基本上使用普通的 1-D LSTM 并将输出转置以将其提供给第二个 1-D LSTM。这也有人提倡here但我不太确定它是否正确/是否与我需要的想法相同。

如有任何帮助,我们将不胜感激。

最佳答案

我已经成功尝试在 tensorflow 中使用 GridLSTMndlstm

我不确定如何将 4D 张量转换为 3D 张量以使其被 dynamic_rnn 接受,但我认为这可能会让您了解如何使用 网格LSTM:

def reshape_to_rnn_dims(tensor, num_time_steps):
    return tf.unstack(tensor, num_time_steps, 1)


class GridLSTMCellTest(tf.test.TestCase):
    def setUp(self):
        self.num_features = 1
        self.time_steps = 1
        self.batch_size = 1
        tf.reset_default_graph()
        self.input_layer = tf.placeholder(tf.float32, [self.batch_size, self.time_steps, self.num_features])
        self.cell = grid_rnn.Grid1LSTMCell(num_units=8)

    def test_simple_grid_rnn(self):
        self.input_layer = reshape_to_rnn_dims(self.input_layer, self.time_steps)
        tf.nn.static_rnn(self.cell, self.input_layer, dtype=tf.float32)

    def test_dynamic_grid_rnn(self):
        tf.nn.dynamic_rnn(self.cell, self.input_layer, dtype=tf.float32)


class BidirectionalGridRNNCellTest(tf.test.TestCase):
    def setUp(self):
        self.num_features = 1
        self.time_steps = 1
        self.batch_size = 1
        tf.reset_default_graph()
        self.input_layer = tf.placeholder(tf.float32, [self.batch_size, self.time_steps, self.num_features])
        self.cell_fw = grid_rnn.Grid1LSTMCell(num_units=8)
        self.cell_bw = grid_rnn.Grid1LSTMCell(num_units=8)

    def test_simple_bidirectional_grid_rnn(self):
        self.input_layer = reshape_to_rnn_dims(self.input_layer, self.time_steps)
        tf.nn.static_bidirectional_rnn(self.cell_fw, self.cell_fw, self.input_layer, dtype=tf.float32)

    def test_bidirectional_dynamic_grid_rnn(self):
        tf.nn.bidirectional_dynamic_rnn(self.cell_fw, self.cell_bw, self.input_layer, dtype=tf.float32)

if __name__ == '__main__':
    tf.test.main()

显然,ndlstm 接受形状为 (batch_size, height, width, depth) 的 4D 张量,我有这些测试(一个涉及使用 tensorflow 的 ctc_loss。还发现了它与 conv2d 一起使用的 example:

class MultidimensionalRNNTest(tf.test.TestCase):
    def setUp(self):
        self.num_classes = 26
        self.num_features = 32
        self.time_steps = 64
        self.batch_size = 1 # Can't be dynamic, apparently.
        self.num_channels = 1
        self.num_filters = 16
        self.input_layer = tf.placeholder(tf.float32, [self.batch_size, self.time_steps, self.num_features, self.num_channels])
        self.labels = tf.sparse_placeholder(tf.int32)

    def test_simple_mdrnn(self):
        net = lstm2d.separable_lstm(self.input_layer, self.num_filters)

    def test_image_to_sequence(self):
        net = lstm2d.separable_lstm(self.input_layer, self.num_filters)
        net = lstm2d.images_to_sequence(net)

    def test_convert_to_ctc_dims(self):
        net = lstm2d.separable_lstm(self.input_layer, self.num_filters)
        net = lstm2d.images_to_sequence(net)

        net = tf.reshape(inputs, [-1, self.num_filters])

         W = tf.Variable(tf.truncated_normal([self.num_filters,
                                     self.num_classes],
                                    stddev=0.1, dtype=tf.float32), name='W')
         b = tf.Variable(tf.constant(0., dtype=tf.float32, shape=[self.num_classes], name='b'))

         net = tf.matmul(net, W) + b
         net = tf.reshape(net, [self.batch_size, -1, self.num_classes])

         net = tf.transpose(net, (1, 0, 2))

         loss = tf.nn.ctc_loss(inputs=net, labels=self.labels, sequence_length=[2])

    print(net)


if __name__ == '__main__':
    tf.test.main()

关于python - Tensorflow 上的多维 RNN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44257665/

相关文章:

python - 如何在python中使矩形保持在移动线上

python - 使用包含左外连接 Pandas 数据框

python - ValueError : Layer sequential expects 1 inputs, 但它在 tensorflow 2.0 中收到了 211 个输入张量

python - 无法让 tensorflow 建立一个甚至可以匹配非常简单的线性图的模型

python - 如何在 Keras 函数式 API 中使用逐元素乘法训练来组合 2 个向量?

python - TensorFlow:简单的递归神经网络

python - 当 input_shape 指定为 3-d 时,Keras SimpleRNN 出错

python - 多索引 Pandas 数据帧上的 cumsum()

python - 我收到错误提示 IndexError : list index out of range. 我该如何解决这个问题?

python - Seq2Seq 模型学会在几次迭代后只输出 EOS token (<\s>)