RNN - 预测正弦函数
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
数据准备
training_examples = 10000
testing_examples = 1000
sample_gap = 0.01
timesteps = 20
def generate_data(seq):
'''
生成数据,seq是一序列的连续的sin的值
'''
X = []
y = []
for i in range(len(seq) - timesteps -1):
X.append(seq[i : i+timesteps])
y.append(seq[i+timesteps])
return np.array(X, dtype=np.float32), np.array(y, dtype=np.float32)
test_start = training_examples*sample_gap
test_end = test_start + testing_examples*sample_gap
train_x, train_y = generate_data( np.sin( np.linspace(0, test_start, training_examples) ) )
test_x, test_y = generate_data( np.sin( np.linspace(test_start, test_end, testing_examples) ) )
建立RNN模型
设置模型参数
lstm_size = 30
lstm_layers = 2
batch_size = 64
定义输入输出
x = tf.placeholder(tf.float32, [None, timesteps, 1], name='input_x')
y_ = tf.placeholder(tf.float32, [None, 1], name='input_y')
keep_prob = tf.placeholder(tf.float32, name='keep_prob')
建立LSTM层
lstm = tf.contrib.rnn.BasicLSTMCell(lstm_size)
drop = tf.contrib.rnn.DropoutWrapper(lstm, output_keep_prob=keep_prob)
def lstm_cell():
return tf.contrib.rnn.BasicLSTMCell(lstm_size)
cell = tf.contrib.rnn.MultiRNNCell([ lstm_cell() for _ in range(lstm_layers)])
outputs, final_state = tf.nn.dynamic_rnn(cell, x, dtype=tf.float32)
outputs = outputs[:,-1]
predictions = tf.contrib.layers.fully_connected(outputs, 1, activation_fn=tf.tanh)
cost = tf.losses.mean_squared_error(y_, predictions)
optimizer = tf.train.AdamOptimizer().minimize(cost)
训练
def get_batches(X, y, batch_size=64):
for i in range(0, len(X), batch_size):
begin_i = i
end_i = i + batch_size if (i+batch_size) < len(X) else len(X)
yield X[begin_i:end_i], y[begin_i:end_i]
epochs = 20
session = tf.Session()
with session.as_default() as sess:
tf.global_variables_initializer().run()
iteration = 1
for e in range(epochs):
for xs, ys in get_batches(train_x, train_y, batch_size):
feed_dict = { x:xs[:,:,None], y_:ys[:,None], keep_prob:.5 }
loss, _ = sess.run([cost, optimizer], feed_dict=feed_dict)
if iteration % 100 == 0:
print('Epochs:{}/{}'.format(e, epochs),
'Iteration:{}'.format(iteration),
'Train loss: {:.8f}'.format(loss))
iteration += 1
Epochs:0/20 Iteration:100 Train loss: 0.01009926
Epochs:1/20 Iteration:200 Train loss: 0.02012673
Epochs:1/20 Iteration:300 Train loss: 0.00237983
Epochs:2/20 Iteration:400 Train loss: 0.00029798
Epochs:3/20 Iteration:500 Train loss: 0.00283409
Epochs:3/20 Iteration:600 Train loss: 0.00115144
Epochs:4/20 Iteration:700 Train loss: 0.00130756
Epochs:5/20 Iteration:800 Train loss: 0.00029282
Epochs:5/20 Iteration:900 Train loss: 0.00045034
Epochs:6/20 Iteration:1000 Train loss: 0.00007531
Epochs:7/20 Iteration:1100 Train loss: 0.00189699
Epochs:7/20 Iteration:1200 Train loss: 0.00022669
Epochs:8/20 Iteration:1300 Train loss: 0.00065262
Epochs:8/20 Iteration:1400 Train loss: 0.00001342
Epochs:9/20 Iteration:1500 Train loss: 0.00037799
Epochs:10/20 Iteration:1600 Train loss: 0.00009412
Epochs:10/20 Iteration:1700 Train loss: 0.00110568
Epochs:11/20 Iteration:1800 Train loss: 0.00024895
Epochs:12/20 Iteration:1900 Train loss: 0.00287319
Epochs:12/20 Iteration:2000 Train loss: 0.00012025
Epochs:13/20 Iteration:2100 Train loss: 0.00353661
Epochs:14/20 Iteration:2200 Train loss: 0.00045697
Epochs:14/20 Iteration:2300 Train loss: 0.00103393
Epochs:15/20 Iteration:2400 Train loss: 0.00045038
Epochs:16/20 Iteration:2500 Train loss: 0.00022164
Epochs:16/20 Iteration:2600 Train loss: 0.00026206
Epochs:17/20 Iteration:2700 Train loss: 0.00279484
Epochs:17/20 Iteration:2800 Train loss: 0.00024887
Epochs:18/20 Iteration:2900 Train loss: 0.00263336
Epochs:19/20 Iteration:3000 Train loss: 0.00071482
Epochs:19/20 Iteration:3100 Train loss: 0.00026286
测试
with session.as_default() as sess:
feed_dict = {x:test_x[:,:,None], keep_prob:1.0}
results = sess.run(predictions, feed_dict=feed_dict)
plt.plot(results,'r', label='predicted')
plt.plot(test_y, 'g--', label='real sin')
plt.legend()
plt.show()