import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
HIDDEN_SIZE = 30 # LSTM中隐藏节点的个数。
NUM_LAYERS = 2 # LSTM的层数。
TIMESTEPS = 10 # 循环神经网络的训练序列长度。
TRAINING_STEPS = 5000 # 训练轮数。
BATCH_SIZE = 32 # batch大小。
TRAINING_EXAMPLES = 5000 # 训练数据个数。
TESTING_EXAMPLES = 1000 # 测试数据个数。
SAMPLE_GAP = 0.01 # 采样间隔。
def generate_data(seq):
X = []
y = []
# 序列的第i项和后面的TIMESTEPS-1项合在一起作为输入;第i + TIMESTEPS项作为输
# 出。即用sin函数前面的TIMESTEPS个点的信息,预测第i + TIMESTEPS个点的函数值。
for i in range(len(seq) - TIMESTEPS):
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 + TIMESTEPS) * SAMPLE_GAP
test_end = test_start + (TESTING_EXAMPLES + TIMESTEPS) * SAMPLE_GAP
train_X, train_y = generate_data(np.cos(np.linspace(
0, test_start, TRAINING_EXAMPLES + TIMESTEPS, dtype=np.float32)))
test_X, test_y = generate_data(np.cos(np.linspace(
test_start, test_end, TESTING_EXAMPLES + TIMESTEPS, dtype=np.float32)))
print(train_X.shape, train_y.shape)
print(test_X.shape, test_y.shape)
def lstm_model(X, y, is_training):
# 使用多层的LSTM结构。
cell = tf.nn.rnn_cell.MultiRNNCell([
tf.nn.rnn_cell.BasicLSTMCell(HIDDEN_SIZE)
for _ in range(NUM_LAYERS)])
# 使用TensorFlow接口将多层的LSTM结构连接成RNN网络并计算其前向传播结果。
outputs, _ = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)
output = outputs[:, -1, :]
# 对LSTM网络的输出再做加一层全链接层并计算损失。注意这里默认的损失为平均
# 平方差损失函数。
predictions = tf.contrib.layers.fully_connected(
output, 1, activation_fn=None)
# 只在训练时计算损失函数和优化步骤。测试时直接返回预测结果。
if not is_training:
return predictions, None, None
# 计算损失函数。
loss = tf.losses.mean_squared_error(labels=y, predictions=predictions)
# 创建模型优化器并得到优化步骤。
train_op = tf.contrib.layers.optimize_loss(
loss, tf.train.get_global_step(),
optimizer="Adagrad", learning_rate=0.1)
return predictions, loss, train_op
def run_eval(sess, test_X, test_y):
# 将测试数据以数据集的方式提供给计算图。
ds = tf.data.Dataset.from_tensor_slices((test_X, test_y))
ds = ds.batch(1)
X, y = ds.make_one_shot_iterator().get_next()
# 调用模型得到计算结果。这里不需要输入真实的y值。
with tf.variable_scope("model", reuse=True):
prediction, _, _ = lstm_model(X, [0.0], False)
# 将预测结果存入一个数组。
predictions = []
labels = []
for i in range(TESTING_EXAMPLES):
p, l = sess.run([prediction, y])
predictions.append(p)
labels.append(l)
# 计算rmse作为评价指标。
predictions = np.array(predictions).squeeze()
labels = np.array(labels).squeeze()
rmse = np.sqrt(((predictions - labels) ** 2).mean(axis=0))
print("Root Mean Square Error is: %f" % rmse)
# 对预测的sin函数曲线进行绘图。
# plt.figure()
plt.plot(predictions, 'x',label='predictions')
plt.plot(labels, label='real_sin')
plt.legend()
plt.show()
# 将训练数据以数据集的方式提供给计算图。
ds = tf.data.Dataset.from_tensor_slices((train_X, train_y))
ds = ds.repeat().shuffle(1000).batch(BATCH_SIZE)
X, y = ds.make_one_shot_iterator().get_next()
# 定义模型,得到预测结果、损失函数,和训练操作。
with tf.variable_scope("model"):
_, loss, train_op = lstm_model(X, y, True)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# 测试在训练之前的模型效果。
print("Evaluate model before training.")
run_eval(sess, test_X, test_y)
# 训练模型。
for i in range(TRAINING_STEPS):
_, l = sess.run([train_op, loss])
if i % 1000 == 0:
print("train step: " + str(i) + ", loss: " + str(l))
# 使用训练好的模型对测试数据进行预测。
print("Evaluate model after training.")
run_eval(sess, test_X, test_y)
from keras.models import Sequential
from keras.layers import Dense, Dropout
import keras.callbacks
from keras.layers import LSTM
import matplotlib.pyplot as plt
import numpy as np
timesteps = 10 # 循环神经网络的训练序列长度。
batch_size = 128 # batch大小。
TRAINING_EXAMPLES = 5000 # 训练数据个数。
TESTING_EXAMPLES = 1000 # 测试数据个数。
SAMPLE_GAP = 0.01 # 采样间隔。
def generate_data(seq):
X = []
y = []
# 序列的第i项和后面的TIMESTEPS-1项合在一起作为输入;第i + TIMESTEPS项作为输
# 出。即用sin函数前面的TIMESTEPS个点的信息,预测第i + TIMESTEPS个点的函数值。
for i in range(len(seq) - timesteps):
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 + timesteps) * SAMPLE_GAP
test_end = test_start + (TESTING_EXAMPLES + timesteps) * SAMPLE_GAP
train_x, train_y = generate_data(np.cos(np.linspace(
0, test_start, TRAINING_EXAMPLES + timesteps, dtype=np.float32)))
test_x, test_y = generate_data(np.cos(np.linspace(
test_start, test_end, TESTING_EXAMPLES + timesteps, dtype=np.float32)))
# print(train_x.shape, train_y.shape) # (5000, 1, 10) (5000, 1)
# print(test_x.shape, test_y.shape) # (1000, 1, 10) (1000, 1)
model = Sequential()
# model.add(LSTM(64, input_shape=(X.shape[1], X.shape[2])))
# LSTM中可以直接添加dropout, Dense不行
# model.add(LSTM(units=320,input_shape=(1,timesteps), return_sequences=True,activation='relu'))
model.add(LSTM(units=320,input_shape=(1,timesteps), return_sequences=True,activation='tanh'))
# LSTM 最后一层 return_sequences=False
model.add(LSTM(320, return_sequences=False,activation='tanh'))
# model.add(Dropout(0.2))
model.add(Dense(1, activation='linear'))
log_filepath = 'E:\AAA\PYTHON\conda_test\keras_log'
tb_cb = keras.callbacks.TensorBoard(log_dir=log_filepath, write_images=1)
# 设置log的存储位置,将网络权值以图片格式保持在tensorboard中显示,设置每一个周期计算一次网络的权值,每层输出值的分布直方图
cbks = [tb_cb]
model.compile(loss='mse',
optimizer='adam',
)
model.fit(train_x, train_y, batch_size=batch_size, epochs=10, callbacks=cbks)
# p = model.predict(train_x, batch_size=batch_size)
# print(p.shape) #(1000, 1)
p = model.predict(test_x, batch_size=batch_size)
plt.plot(p,'r')
# plt.plot(train_y,'b')
plt.plot(test_y,'b')
plt.show()
# tensorboard --logdir=E:\AAA\PYTHON\conda_test\keras_log
from keras.models import Sequential
from keras.layers import Dense, Dropout
import keras.callbacks
from keras.layers import LSTM
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
matplotlib.rcParams['font.family']='SimHei'
timesteps = 10 # 循环神经网络的训练序列长度。
batch_size = 128 # batch大小。
TRAINING_EXAMPLES = 5000 # 训练数据个数。
TESTING_EXAMPLES = 1000 # 测试数据个数。
SAMPLE_GAP = 0.01 # 采样间隔。
def generate_data(seq):
X = []
y = []
# 序列的第i项和后面的TIMESTEPS-1项合在一起作为输入;第i + TIMESTEPS项作为输
# 出。即用sin函数前面的TIMESTEPS个点的信息,预测第i + TIMESTEPS个点的函数值。
for i in range(len(seq) - timesteps):
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 + timesteps) * SAMPLE_GAP
test_end = test_start + (TESTING_EXAMPLES + timesteps) * SAMPLE_GAP
train_x, train_y = generate_data(np.cos(np.linspace(
0, test_start, TRAINING_EXAMPLES + timesteps, dtype=np.float32)))
test_x, test_y = generate_data(np.cos(np.linspace(
test_start, test_end, TESTING_EXAMPLES + timesteps, dtype=np.float32)))
print(train_x.shape, train_y.shape) # (5000, 1, 10) (5000, 1)
# print(test_x.shape, test_y.shape) # (1000, 1, 10) (1000, 1)
model = Sequential()
model.add(LSTM(units=320,input_shape=(1,timesteps), return_sequences=True))
# model.add(LSTM(units=320,input_shape=(1,timesteps),activation='tanh'))
# LSTM 最后一层 return_sequences=False
model.add(LSTM(320, return_sequences=False))
model.add(Dense(1, activation='linear'))
# model.add(Dense(1))
log_filepath = 'E:\AAA\PYTHON\conda_test\keras_log'
tb_cb = keras.callbacks.TensorBoard(log_dir=log_filepath, write_images=1)
# 设置log的存储位置,将网络权值以图片格式保持在tensorboard中显示,设置每一个周期计算一次网络的权值,每层输出值的分布直方图
cbks = [tb_cb]
model.compile(loss='mse', optimizer='adam')
model.fit(train_x, train_y, batch_size=batch_size, epochs=10, callbacks=cbks)
# p= model.predict(train_x, batch_size=batch_size)
# print(p.shape) #(1000, 1)
p = model.predict(test_x, batch_size=batch_size)
plt.plot(p,'r',label = '预测值')
# plt.plot(train_y,'b')
plt.plot(test_y,'b',label = '真实值')
plt.legend()
plt.show()
# tensorboard --logdir=E:\AAA\PYTHON\conda_test\keras_log
from keras.models import Sequential
from keras.layers import Dense, Dropout
import keras.callbacks
from keras.layers import LSTM
import matplotlib.pyplot as plt
import numpy as np
timesteps = 10 # 循环神经网络的训练序列长度。
batch_size = 128 # batch大小。
TRAINING_EXAMPLES = 5000 # 训练数据个数。
TESTING_EXAMPLES = 1000 # 测试数据个数。
SAMPLE_GAP = 0.01 # 采样间隔。
def generate_data(seq):
X = []
y = []
# 序列的第i项和后面的TIMESTEPS-1项合在一起作为输入;第i + TIMESTEPS项作为输
# 出。即用sin函数前面的TIMESTEPS个点的信息,预测第i + TIMESTEPS个点的函数值。
for i in range(len(seq) - timesteps-2):
X.append([seq[i: i + timesteps]])
y.append([seq[i + timesteps+2]])
return np.array(X, dtype=np.float32), np.array(y, dtype=np.float32)
# 用正弦函数生成训练和测试数据集合。
test_start = (TRAINING_EXAMPLES + timesteps) * SAMPLE_GAP
test_end = test_start + (TESTING_EXAMPLES + timesteps) * SAMPLE_GAP
train_x, train_y = generate_data(np.cos(np.linspace(
0, test_start, TRAINING_EXAMPLES + timesteps, dtype=np.float32)))
test_x, test_y = generate_data(np.cos(np.linspace(
test_start, test_end, TESTING_EXAMPLES + timesteps, dtype=np.float32)))
print(train_x.shape, train_y.shape) # (5000, 1, 10) (5000, 1)
print(test_x.shape, test_y.shape) # (1000, 1, 10) (1000, 1)
model = Sequential()
# model.add(LSTM(64, input_shape=(X.shape[1], X.shape[2])))
# model.add(LSTM(units=320,input_shape=(1,timesteps), return_sequences=True,activation='relu'))
model.add(LSTM(units=320,input_shape=(1,timesteps), return_sequences=True,activation='tanh'))
# LSTM 最后一层 return_sequences=False
model.add(LSTM(320, return_sequences=False,activation='tanh'))
# model.add(Dropout(0.2))
model.add(Dense(1, activation='linear'))
log_filepath = 'E:\AAA\PYTHON\conda_test\keras_log'
tb_cb = keras.callbacks.TensorBoard(log_dir=log_filepath, write_images=1)
# 设置log的存储位置,将网络权值以图片格式保持在tensorboard中显示,设置每一个周期计算一次网络的权值,每层输出值的分布直方图
cbks = [tb_cb]
model.compile(loss='mse',
optimizer='adam',
)
model.fit(train_x, train_y, batch_size=batch_size, epochs=10, callbacks=cbks)
# p = model.predict(train_x, batch_size=batch_size)
# print(p.shape) #(1000, 1)
p = model.predict(test_x, batch_size=batch_size)
plt.plot(p,'r')
# plt.plot(train_y,'b')
plt.plot(test_y,'b')
plt.show()
# tensorboard --logdir=E:\AAA\PYTHON\conda_test\keras_log