TensorFlow【极简】序列预测

余弦曲线拟合效果

极简代码

一维卷积

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))  # shape (1000, 75, 1)
Y = y[window:].reshape(-1, 1)  # shape (1000, 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))  # [卷积长度、通道数、filter数]
b_conv = tf.Variable(tf.constant(.1, shape=[40]))  # [filter数]
h_conv = tf.nn.conv1d(value=h_x, filters=W_conv, stride=1, padding='VALID') + b_conv  # 滑窗步幅:1
h_relu = tf.nn.relu(h_conv)  # ReLU激活
# 平化
h_flat = tf.reshape(h_relu, [-1, (75 - 10 + 1) * 40])
# 全连接1
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)
# 全连接2
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)
# Adam优化器
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  # RNN神经元数量
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))  # shape (1000, 75)
Y = y[window:].reshape(-1, 1)  # shape (1000, 1)

"""建模"""
# 输入
h_x = tf.placeholder('float', [batch_size, window])
h_y = tf.placeholder('float', [batch_size, 1])
# RNN
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  # output

# 损失:均方误差
loss = tf.losses.mean_squared_error(o, h_y)
# Adam优化器
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】基础补充

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小基基o_O

您的鼓励是我创作的巨大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值