文章目录 案例:RNN LSTM Regressor 生成序列 搭建模型 训练 LSTM 循环神经网络 RNN 的弊端 LSTM 案例:RNN LSTM Regressor #RNN LSTM Regressor example import numpy as np np.random.seed(1337) # for reproducibility import matplotlib.pyplot as plt from keras.models import Sequential from keras.layers import LSTM, TimeDistributed, Dense from keras.optimizers import Adam 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] model = Sequential() # build a LSTM RNN</