tensorflow1.0 LSTM实现
2017-06-20 17:21
1186 查看
代码中num_step可以设置为需要的序列长度,常常为了梯度反传的方便将num_step设置为较小的值。
def LSTM_single( name, _X, _istate, _weights, _biases):
# input shape: (batch_size, n_steps, n_input)
_X = tf.transpose(_X, [1, 0, 2]) # permute num_steps and batch_size
# Reshape to prepare input to hidden activation
_X = tf.reshape(_X, [self.num_steps * self.batch_size, self.num_input]) # (num_steps*batch_size, num_input)
# Split data because rnn cell needs a list of inputs for the RNN inner loop
_X = tf.split(axis=0, num_or_size_splits=self.num_steps, value=_X) # n_steps * (batch_size, num_input)
#print("_X: ", _X)
#cell = tf.nn.rnn_cell.LSTMCell(self.num_input, self.num_input)
cell = tf.contrib.rnn.LSTMCell(self.num_input, state_is_tuple=False)
state = _istate
outputs_seq = []
for step in range(self.num_steps):
outputs, state = tf.contrib.rnn.static_rnn(cell, [_X [step] ], state, dtype=tf.float32)
#get all output from inner loop by jr
outputs_seq.append(outputs)
tf.get_variable_scope().reuse_variables()
#reshape output as input _X dim by jr#
outputs_seq = tf.reshape(outputs_seq,[self.num_steps,self.batch_size,self.num_input])
outputs_seq = tf.transpose(outputs_seq,[1, 0, 2])
#return outputs
return outputs_seq,state
调用LSTM进行训练代码说明:
outputs,state = LSTM_single('lstm_train', x,
istate, weights, biases
correct_prediction = tf.square(outputs - y)
accuracy = tf.reduce_mean(correct_prediction) * 100
learning_rate = 0.00001
with tf.variable_scope(tf.get_variable_scope(), reuse=False):
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(accuracy) # Adam Optimizer
with tf.Session() as sess:
while i < iterations:
outputs = sess.run(outputs_seq, feed_dict={x: batch_xs, y: batch_ys,
istate: state})
sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys, istate: state})
state = state.eval(feed_dict={x: batch_xs, y: batch_ys, istate: state})
def LSTM_single( name, _X, _istate, _weights, _biases):
# input shape: (batch_size, n_steps, n_input)
_X = tf.transpose(_X, [1, 0, 2]) # permute num_steps and batch_size
# Reshape to prepare input to hidden activation
_X = tf.reshape(_X, [self.num_steps * self.batch_size, self.num_input]) # (num_steps*batch_size, num_input)
# Split data because rnn cell needs a list of inputs for the RNN inner loop
_X = tf.split(axis=0, num_or_size_splits=self.num_steps, value=_X) # n_steps * (batch_size, num_input)
#print("_X: ", _X)
#cell = tf.nn.rnn_cell.LSTMCell(self.num_input, self.num_input)
cell = tf.contrib.rnn.LSTMCell(self.num_input, state_is_tuple=False)
state = _istate
outputs_seq = []
for step in range(self.num_steps):
outputs, state = tf.contrib.rnn.static_rnn(cell, [_X [step] ], state, dtype=tf.float32)
#get all output from inner loop by jr
outputs_seq.append(outputs)
tf.get_variable_scope().reuse_variables()
#reshape output as input _X dim by jr#
outputs_seq = tf.reshape(outputs_seq,[self.num_steps,self.batch_size,self.num_input])
outputs_seq = tf.transpose(outputs_seq,[1, 0, 2])
#return outputs
return outputs_seq,state
调用LSTM进行训练代码说明:
outputs,state = LSTM_single('lstm_train', x,
istate, weights, biases
correct_prediction = tf.square(outputs - y)
accuracy = tf.reduce_mean(correct_prediction) * 100
learning_rate = 0.00001
with tf.variable_scope(tf.get_variable_scope(), reuse=False):
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(accuracy) # Adam Optimizer
with tf.Session() as sess:
while i < iterations:
outputs = sess.run(outputs_seq, feed_dict={x: batch_xs, y: batch_ys,
istate: state})
sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys, istate: state})
state = state.eval(feed_dict={x: batch_xs, y: batch_ys, istate: state})
相关文章推荐
- Tensorflow1.0空间变换网络(SpatialTransformer Networks)实现
- tensorflow 双LSTM实现地址相似性判断
- 如何基于TensorFlow使用LSTM和CNN实现时序分类任务
- 使用MNIST数据集,在TensorFlow上实现基础LSTM网络
- 利用 TensorFlow 高级 API Keras 实现 MLP,CNN,LSTM
- tensorflow实现基于LSTM的文本分类方法
- Tensorflow[基础篇]——LSTM的理解与实现
- 【Ubuntu-Tensorflow】TF1.0到TF1.2出现“Key LSTM/basic_lstm_cell/bias not found in checkpoin”问题
- 如何用 TensorFlow 实现基于 LSTM 的文本分类
- Tensorflow实现LSTM
- tensorflow38《TensorFlow实战》笔记-07-02 TensorFlow实现基于LSTM的语言模型 code
- TensorFlow (RNN)深度学习 双向LSTM(BiLSTM)+CRF 实现 sequence labeling 序列标注问题 源码下载
- TensorFlow (RNN)深度学习 双向LSTM(BiLSTM)+CRF 实现 sequence labeling 序列标注问题 源码下载
- tensorflow 学习笔记12 循环神经网络RNN LSTM结构实现MNIST手写识别
- 深度学习(08)_RNN-LSTM循环神经网络-03-Tensorflow进阶实现
- 利用 TensorFlow 高级 API Keras 实现 MLP,CNN,LSTM
- TensorFlow入门(六) 双端 LSTM 实现序列标注(分词)
- TensorFlow (RNN)深度学习下 双向LSTM(BiLSTM)+CRF 实现 sequence labeling 双向LSTM+CRF跑序列标注问题
- 基于tensorflow的RNN-LSTM(一)实现RNN
- TensorFlow 使用例子-LSTM实现序列标注