Keras学习(六)——RNN循环神经网络回归

本文主要介绍使用RNN来实现回归,对曲线进行预测。

示例代码:

import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, TimeDistributed, LSTM
from keras.optimizers import Adam
import matplotlib.pyplot as plt

# 使多次生成的随机数相同
np.random.seed(1337)

# 超参数
BATCH_START = 0
TIME_STEPS = 20
BATCH_SIZE = 50
INPUT_SIZE = 1
OUTPUT_SIZE = 1
CELL_SIZE = 20
LR = 0.006


# 生成数据
def get_batch():
    global BATCH_START, TIME_STEPS
    # xs shape (50batch, 20steps)
    xs = np.arange(BATCH_START, BATCH_START+TIME_STEPS*BATCH_SIZE).reshape((BATCH_SIZE, TIME_STEPS)) / (10*np.pi)
    seq = np.sin(xs)
    res = np.cos(xs)
    BATCH_START += TIME_STEPS
    # plt.plot(xs[0, :], res[0, :], 'r', xs[0, :], seq[0, :], 'b--')
    # plt.show()
    return [seq[:, :, np.newaxis], res[:, :, np.newaxis], xs]

# 查看数据
# get_batch()
# exit()
# 搭建网络
model = Sequential()

# 添加LSTM层
model.add(LSTM(
    batch_input_shape=(BATCH_SIZE, TIME_STEPS, INPUT_SIZE),
    output_dim=CELL_SIZE,
    return_sequences=True,  # 对于每一个时间点需不需要输出对应的output, True每个时刻都输出, False最后的输出output
    stateful=True,  # batch与batch之间是否有联系,需不需要将状态进行传递
))
# add output layer
model.add(TimeDistributed(Dense(OUTPUT_SIZE)))  # TimeDistributed:对每一个output进行全连接的计算

# 优化器
adam = Adam()
model.compile(
    optimizer=adam,
    loss='mse',
)

# 训练
print('Training ------------')
for step in range(501):
    # data shape = (batch_num, steps, inputs/outputs)
    X_batch, Y_batch, xs = get_batch()
    cost = model.train_on_batch(X_batch, Y_batch)
    pred = model.predict(X_batch, BATCH_SIZE)
    plt.plot(xs[0, :], Y_batch[0].flatten(), 'r', xs[0, :], pred.flatten()[:TIME_STEPS], 'b--')
    plt.ylim((-1.2, 1.2))
    plt.draw()
    plt.pause(0.1)
    if step % 10 == 0:
        print('train cost: ', cost)

数据示例:

def get_batch():
    global BATCH_START, TIME_STEPS
    # xs shape (50batch, 20steps)
    xs = np.arange(BATCH_START, BATCH_START+TIME_STEPS*BATCH_SIZE).reshape((BATCH_SIZE, TIME_STEPS)) / (1*np.pi)
    seq = np.sin(xs)
    res = np.cos(xs)
    BATCH_START += TIME_STEPS
    plt.plot(xs[0, :], res[0, :], 'r', xs[0, :], seq[0, :], 'b--')
    plt.show()
    return [seq[:, :, np.newaxis], res[:, :, np.newaxis], xs]

# 查看数据
get_batch()
exit()

Keras学习(六)——RNN循环神经网络回归_第1张图片

结果:

 

Keras学习(六)——RNN循环神经网络回归_第2张图片

Keras学习(六)——RNN循环神经网络回归_第3张图片

Keras学习(六)——RNN循环神经网络回归_第4张图片

train cost:  0.50940645
train cost:  0.4966624
train cost:  0.48060146
train cost:  0.45672885
train cost:  0.4108651
train cost:  0.31347314
train cost:  0.12554297
train cost:  0.07388962
train cost:  0.10137392
train cost:  0.046597198
train cost:  0.05946522
train cost:  0.040294208
train cost:  0.053411756
train cost:  0.15622795
train cost:  0.17914045
train cost:  0.16356382
train cost:  0.21077277
train cost:  0.20014948
train cost:  0.18070495
train cost:  0.16142645
train cost:  0.19912449
train cost:  0.16934186
train cost:  0.16477375
train cost:  0.17521137
train cost:  0.20553884
train cost:  0.15104571
train cost:  0.16296455
train cost:  0.16819069
train cost:  0.11465822
train cost:  0.14150377
train cost:  0.13508156
train cost:  0.13755415
train cost:  0.13000277
train cost:  0.11969448
train cost:  0.09293661
train cost:  0.0819223
train cost:  0.06903682
train cost:  0.07125411
train cost:  0.08032415
train cost:  0.07321488
train cost:  0.096763514
train cost:  0.078285255
train cost:  0.07236056
train cost:  0.065320924
train cost:  0.057717755
train cost:  0.063192114
train cost:  0.047402352
train cost:  0.05537389
train cost:  0.051893406
train cost:  0.052938405
train cost:  0.05649735

 

你可能感兴趣的:(Keras,Keras)