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