【问题标题】:How to use the last state of a variable as the next state in Tensorflow?如何使用变量的最后一个状态作为 Tensorflow 中的下一个状态?
【发布时间】:2016-03-25 18:18:17
【问题描述】:

出于学习目的,我想在 Tensorflow 中构建自己的 LSTM 模型。问题是,如何训练是使用上一个时间步的状态来初始化某个时间步的状态。 Tensorflow 中有这样的机制吗?

class Lstm:

    def __init__(self, x, steps):
        self.initial = tf.placeholder(tf.float32, [None, size])
        self.state = self.initial
        for _ in range(steps):
            x = self.layer_lstm(x, 100)
        x = self.layer_softmax(x, 10)
        self.prediction = x

    def step_lstm(self, x, size):
        stream = self.layer(x, size)
        input_ = self.layer(x, size)
        forget = self.layer(x, size, bias=1)
        output = self.layer(x, size)
        self.state = stream * input_ + self.state * forget
        x = self.state * output
        return x

    def layer_softmax(self, x, size):
        x = self.layer(x, size)
        x = tf.nn.softmax(x)
        return x

    def layer(self, x, size, bias=0.1):
        in_size = int(x.get_shape()[1])
        weight = tf.Variable(tf.truncated_normal([in_size, size], stddev=0.1))
        bias = tf.Variable(tf.constant(bias, shape=[size]))
        x = tf.matmul(x, weight) + bias
        return x

【问题讨论】:

    标签: python tensorflow lstm


    【解决方案1】:

    @danijar - 您可能想查看this page 的“变量”部分,了解如何在对子图的调用中维护状态的简单示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-19
      • 1970-01-01
      • 2016-11-09
      相关资源
      最近更新 更多