01 基本环境
#《深度学习原理与TensorFlow实战》05 RNN能说会道
# 书源码地址:https://github.com/DeepVisionTeam/TensorFlowBook.git
# 视频讲座地址:http://edu.csdn.net/course/detail/5222
# win10 Tensorflow1.2.0 python3.6.1
# CUDA v8.0 cudnn-8.0-windows10-x64-v5.1
# 本地代码位置:D:\git\DeepLearning\TensorFlowBook\neural_style\neural_style.py
# https://github.com/DeepVisionTeam/TensorFlowBook/blob/master/rnn/sin/rnn_sin.py
# https://github.com/DeepVisionTeam/TensorFlowBook/blob/master/rnn/sin/lstm_sin.py
# 在tensorflow1.2.0版本上,对rnn_sin.py和lstm_sin.py稍作了修改
02 rnn_sin.py
from __future__ import absolute_import, division, print_function
import random
import numpy as np
import tensorflow as tf
from tensorflow.contrib import rnn
def build_data(n):
xs = []
ys = []
for i in range(0, 2000):
k = random.uniform(1, 50)
x = [[np.sin(k + j)] for j in range(0, n)]
y = [np.sin(k + n)]
xs.append(x)
ys.append(y)
train_x = np.array(xs[0: 1500])
train_y = np.array(ys[0: 1500])
test_x = np.array(xs[1500:])
test_y = np.array(ys[1500:])
return (train_x, train_y, test_x, test_y)
length = 10
time_step_size = length
vector_size = 1
batch_size = 10
test_size = 10
(train_x, train_y, test_x, test_y) = build_data(length)
print(train_x.shape, train_y.shape, test_x.shape, test_y.shape)
X = tf.placeholder("float", [None, length, vector_size])
Y = tf.placeholder("float", [None, 1])
W = tf.Variable(tf.random_normal([10, 1], stddev=0.01))
B = tf.Variable(tf.random_normal([1], stddev=0.01))
def seq_predict_model(X, w, b, time_step_size, vector_size):
X = tf.transpose(X, [1, 0, 2])
X = tf.reshape(X, [-1, vector_size])
X = tf.split(X, time_step_size, 0)
cell = rnn.BasicRNNCell(num_units=10)
initial_state = tf.zeros([batch_size, cell.state_size])
outputs, _states = rnn.static_rnn(cell, X, initial_state=initial_state)
return tf.matmul(outputs[-1], w) + b, cell.state_size
pred_y, _ = seq_predict_model(X, W, B, time_step_size, vector_size)
loss = tf.square(tf.subtract(Y, pred_y))
train_op = tf.train.GradientDescentOptimizer(0.001).minimize(loss)
with tf.Session() as sess:
tf.global_variables_initializer().run()
for i in range(50):
for end in range(batch_size, len(train_x), batch_size):
begin = end - batch_size
x_value = train_x[begin: end]
y_value = train_y[begin: end]
sess.run(train_op, feed_dict={X: x_value, Y: y_value})
test_indices = np.arange(len(test_x))
np.random.shuffle(test_indices)
test_indices = test_indices[0: test_size]
x_value = test_x[test_indices]
y_value = test_y[test_indices]
val_loss = np.mean(sess.run(loss,
feed_dict={X: x_value, Y: y_value}))
print('Run %s' % i, val_loss)
for b in range(0, len(test_x), test_size):
x_value = test_x[b: b + test_size]
y_value = test_y[b: b + test_size]
pred = sess.run(pred_y, feed_dict={X: x_value})
for i in range(len(pred)):
print(pred[i], y_value[i], pred[i] - y_value[i])
'''
(1500, 10, 1) (1500, 1) (500, 10, 1) (500, 1)
Run 0 0.000285596
Run 1 7.50029e-05
Run 2 8.55531e-05
...
Run 48 4.05094e-05
Run 49 2.30997e-05
[ 0.60128832] [ 0.61017627] [-0.00888795]
[ 0.77109075] [ 0.77846642] [-0.00737568]
[ 0.41146952] [ 0.40745923] [ 0.00401029]
...
[ 0.98992503] [ 0.98445371] [ 0.00547131]
[ 0.35108942] [ 0.34326224] [ 0.00782718]
[-0.29042405] [-0.27972568] [-0.01069837]
'''
03 lstm_sin.py
from __future__ import absolute_import, division, print_function
import random
import numpy as np
import tensorflow as tf
from tensorflow.contrib import rnn
def build_data(n):
xs = []
ys = []
for i in range(0, 2000):
k = random.uniform(1, 50)
x = [[np.sin(k + j)] for j in range(0, n)]
y = [np.sin(k + n)]
xs.append(x)
ys.append(y)
train_x = np.array(xs[0: 1500])
train_y = np.array(ys[0: 1500])
test_x = np.array(xs[1500:])
test_y = np.array(ys[1500:])
return (train_x, train_y, test_x, test_y)
length = 10
time_step_size = length
vector_size = 1
batch_size = 10
test_size = 10
(train_x, train_y, test_x, test_y) = build_data(length)
print(train_x.shape, train_y.shape, test_x.shape, test_y.shape)
X = tf.placeholder("float", [None, length, vector_size])
Y = tf.placeholder("float", [None, 1])
W = tf.Variable(tf.random_normal([10, 1], stddev=0.01))
B = tf.Variable(tf.random_normal([1], stddev=0.01))
def seq_predict_model(X, w, b, time_step_size, vector_size):
X = tf.transpose(X, [1, 0, 2])
X = tf.reshape(X, [-1, vector_size])
X = tf.split(X, time_step_size, 0)
cell = rnn.BasicLSTMCell(num_units=10,
forget_bias=1.0,
state_is_tuple=True)
outputs, _states = rnn.static_rnn(cell, X, dtype=tf.float32)
return tf.matmul(outputs[-1], w) + b, cell.state_size
pred_y, _ = seq_predict_model(X, W, B, time_step_size, vector_size)
loss = tf.square(tf.subtract(Y, pred_y))
train_op = tf.train.GradientDescentOptimizer(0.001).minimize(loss)
with tf.Session() as sess:
tf.global_variables_initializer().run()
writer = tf.summary.FileWriter('./log', sess.graph)
for i in range(50):
for end in range(batch_size, len(train_x), batch_size):
begin = end - batch_size
x_value = train_x[begin: end]
y_value = train_y[begin: end]
sess.run(train_op, feed_dict={X: x_value, Y: y_value})
test_indices = np.arange(len(test_x))
np.random.shuffle(test_indices)
test_indices = test_indices[0: test_size]
x_value = test_x[test_indices]
y_value = test_y[test_indices]
val_loss = np.mean(sess.run(loss,
feed_dict={X: x_value, Y: y_value}))
print('Run %s' % i, val_loss)
for b in range(0, len(test_x), test_size):
x_value = test_x[b: b + test_size]
y_value = test_y[b: b + test_size]
pred = sess.run(pred_y, feed_dict={X: x_value})
for i in range(len(pred)):
print(pred[i], y_value[i], pred[i] - y_value[i])
'''
(1500, 10, 1) (1500, 1) (500, 10, 1) (500, 1)
Run 0 0.501963
Run 1 0.490687
Run 2 0.328985
...
Run 48 0.000195758
Run 49 0.000305991
[ 0.16057003] [ 0.16943663] [-0.00886661]
[ 0.25243664] [ 0.24671422] [ 0.00572242]
...
[-0.22830085] [-0.22997938] [ 0.00167852]
[-0.80274117] [-0.82659579] [ 0.02385462]
'''