# 循环神经网络

1.上下文信息

2.循环神经网络（RNN）简介

3.实施循环神经网络

```import numpy as np
import tensorflow as tf
from tensorflow.contrib import rnn```

```class SeriesPredictor:
def __init__(self, input_dim, seq_size, hidden_dim=10):
self.input_dim = input_dim //#A
self.seq_size = seq_size  //#A
self.hidden_dim = hidden_dim  //#A
self.W_out = tf.Variable(tf.random_normal([hidden_dim, 1]),name='W_out') //#B
self.b_out = tf.Variable(tf.random_normal([1]), name='b_out')  //#B
self.x = tf.placeholder(tf.float32, [None, seq_size, input_dim]) //#B
self.y = tf.placeholder(tf.float32, [None, seq_size]) //#B
self.cost = tf.reduce_mean(tf.square(self.model() - self.y)) //#C
self.train_op = tf.train.AdamOptimizer().minimize(self.cost) //#C
self.saver = tf.train.Saver()  //#D

#A超参数。
#B权重变量和输入占位符。
#C成本优化器（cost optimizer）。
#D辅助操作。```

```def model(self):
"""
:param x: inputs of size [T, batch_size, input_size]
:param W: matrix of fully-connected output layer weights
:param b: vector of fully-connected output layer biases
"""
cell = rnn.BasicLSTMCell(self.hidden_dim)  #A
outputs, states = tf.nn.dynamic_rnn(cell, self.x, dtype=tf.float32) #B
num_examples = tf.shape(self.x)[0]
W_repeated = tf.tile(tf.expand_dims(self.W_out, 0), [num_examples, 1, 1])#C
out = tf.matmul(outputs, W_repeated) + self.b_out
out = tf.squeeze(out)
return out

#A创建一个LSTM单元。
#B运行输入单元，获取输出和状态的张量。
#C将输出层计算为完全连接的线性函数。```

```def train(self, train_x, train_y):
with tf.Session() as sess:
tf.get_variable_scope().reuse_variables()
sess.run(tf.global_variables_initializer())
for i in range(1000):  #A
mse = sess.run([self.train_op, self.cost], feed_dict={self.x: train_x, self.y: train_y})
if i % 100 == 0:
print(i, mse)
save_path = self.saver.save(sess, 'model.ckpt')
print('Model saved to {}'.format(save_path))

#A训练1000次```

```def test(self, test_x):
with tf.Session() as sess:
tf.get_variable_scope().reuse_variables()
self.saver.restore(sess, './model.ckpt')
output = sess.run(self.model(), feed_dict={self.x: test_x})
print(output)```

```if __name__ == '__main__':
predictor = SeriesPredictor(input_dim=1, seq_size=4, hidden_dim=10)
train_x = [[[1], [2], [5], [6]],
[[5], [7], [7], [8]],
[[3], [4], [5], [7]]]
train_y = [[1, 3, 7, 11],
[5, 12, 14, 15],
[3, 7, 9, 12]]
predictor.train(train_x, train_y)
test_x = [[[1], [2], [3], [4]],  #A
[[4], [5], [6], [7]]]  #B
predictor.test(test_x)

#A预测结果应为1，3，5，7。
#B预测结果应为4，9，11，13。```

57 篇文章34 人订阅

0 条评论

## 相关文章

38430

26550

45610

### TensorFlow实现卷积神经网络

1.卷积神经网络简介 卷积神经网络（convolutional neural network, CNN），最早是19世纪60年代，生物学家对猫视觉皮层研究发...

31890

35940

30980

57280

365100

344110

### TensorFlow从0到1 - 7 - TensorFlow线性回归的参数溢出之坑

? 上一篇 6 解锁梯度下降算法解释清楚了学习率（learning rate）。本篇基于对梯度下降算法和学习率的理解，去填下之前在线性回归中发现的一个坑。 ...

38870