余弦曲线拟合效果
极简代码
一维卷积
import numpy as np, matplotlib.pyplot as mp
import tensorflow as tf
"""创建样本"""
x_len = 1075
x = np.linspace(0, np.pi * 10.75, x_len, endpoint=False)
y = np.cos(x)
window = 75
X = [y[i: i + window] for i in range(x_len - window)]
X = np.reshape(X, (-1, window, 1))
Y = y[window:].reshape(-1, 1)
"""建模"""
h_x = tf.placeholder('float', [None, window, 1])
h_y = tf.placeholder('float', [None, 1])
W_conv = tf.Variable(tf.truncated_normal([10, 1, 40], stddev=0.1))
b_conv = tf.Variable(tf.constant(.1, shape=[40]))
h_conv = tf.nn.conv1d(value=h_x, filters=W_conv, stride=1, padding='VALID') + b_conv
h_relu = tf.nn.relu(h_conv)
h_flat = tf.reshape(h_relu, [-1, (75 - 10 + 1) * 40])
W_fc1 = tf.Variable(tf.truncated_normal([(75 - 10 + 1) * 40, 1], stddev=.1))
b_fc1 = tf.Variable(tf.constant(.1, shape=[60]))
h_fc1 = tf.matmul(h_flat, W_fc1) + b_fc1
h_fc1 = tf.nn.relu(h_fc1)
W_fc2 = tf.Variable(tf.truncated_normal([60, 1], stddev=.1))
b_fc2 = tf.Variable(tf.constant(.1, shape=[1]))
h_fc2 = tf.matmul(h_fc1, W_fc2) + b_fc2
loss = tf.losses.mean_squared_error(h_fc2, h_y)
optimizer = tf.train.AdamOptimizer().minimize(loss)
"""训练"""
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
batch_size = 100
for e in range(600):
loss_ls = []
for i in range(0, x_len, batch_size):
Xi = X[i: i + batch_size]
Yi = Y[i: i + batch_size]
optimizer.run({h_x: Xi, h_y: Yi}, sess)
loss_ls.append(loss.eval({h_x: Xi, h_y: Yi}, sess))
print('epoch:', e, 'loss:', np.mean(loss_ls))
"""预测"""
pred_len = 150
for start in range(0, 1000, 200):
x_pred = x[window + start: window + start + pred_len]
y_pred = []
X_pred = X[start]
for i in range(pred_len):
Y_pred = h_fc2.eval({h_x: X_pred.reshape(-1, window, 1)}, sess)
y_pred.append(Y_pred[0])
X_pred = np.concatenate((X_pred, Y_pred))[1:]
mp.scatter(x_pred[0], y_pred[0], c='r', s=9)
mp.plot(x_pred, y_pred, 'r')
mp.plot(x, y, 'y', linewidth=5, alpha=0.3)
mp.show()
GRU
import numpy as np, matplotlib.pyplot as mp
import tensorflow as tf
from tensorflow.contrib.rnn import GRUCell
"""配置"""
x_len = 1075
window = 75
num_units = 50
batch_size = 1
"""创建样本"""
x = np.linspace(0, np.pi * 10.75, x_len, endpoint=False)
y = np.cos(x)
X = [y[i: i + window] for i in range(x_len - window)]
X = np.reshape(X, (-1, window))
Y = y[window:].reshape(-1, 1)
"""建模"""
h_x = tf.placeholder('float', [batch_size, window])
h_y = tf.placeholder('float', [batch_size, 1])
rnn_cell = GRUCell(num_units)
state = rnn_cell.zero_state(batch_size, tf.float32)
h, state = rnn_cell(h_x, state)
W = tf.Variable(tf.truncated_normal([num_units, 1], stddev=.1))
b = tf.Variable(tf.constant(.1, shape=[1]))
o = tf.matmul(h, W) + b
loss = tf.losses.mean_squared_error(o, h_y)
optimizer = tf.train.AdamOptimizer(1e-4).minimize(loss)
"""训练"""
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
for e in range(6):
loss_ls = []
for i in range(0, x_len - window, batch_size):
Xi = X[i: i + batch_size]
Yi = Y[i: i + batch_size]
optimizer.run({h_x: Xi, h_y: Yi}, sess)
loss_ls.append(loss.eval({h_x: Xi, h_y: Yi}, sess))
print('epoch:', e, 'loss:', np.mean(loss_ls))
"""预测"""
pred_len = 150
for start in range(0, x_len - window, 200):
x_pred = x[window + start: window + start + pred_len]
y_pred = []
X_pred = X[start]
for i in range(pred_len):
Y_pred = o.eval({h_x: X_pred.reshape(1, window)}, sess)[0]
y_pred.append(Y_pred)
X_pred = np.concatenate((X_pred, Y_pred))[1:]
mp.scatter(x_pred[0], y_pred[0], c='r', s=9)
mp.plot(x_pred, y_pred, 'r')
mp.plot(x, y, 'y', linewidth=5, alpha=0.3)
mp.show()
附录
TensorFlow【CNN】基础补充
TensorFlow【RNN】基础补充