如何使用变量的最后一个状态作为Tensorflow中的下一个状态?

时间:2016-03-25 18:18:17

标签: python tensorflow lstm

出于学习目的,我想在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

1 个答案:

答案 0 :(得分:0)

@danijar - 你可能想看一下'变量' this page的一节,是关于如何在子图调用之间保持状态的简单示例。