废话不多说,把代码贴上去,就可以运行。然后看注释,自己慢慢品,细细品。
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
# 1. 生成时间序列数据,这里使用正弦函数模拟,生成(500,1)的数据集,500个点
def generate_time_series():
time_steps = np.linspace(0, 10 * np.pi, 500, dtype=np.float32)
data = np.sin(time_steps)
data = np.expand_dims(data, axis=-1)
return data
# 2. 划分训练集和测试集,前400个训练,后100个验证
def prepare_data(data):
train_data = data[:400]
test_data = data[400:]
return train_data, test_data
# 3. 创建数据集,time_steps是数据步长,现在用前time_steps数据,预测后1个数据。
# 有时候可能是前60个数据,输出后10个数据,那这个函数可以改成time_steps_x,time_step_y
# 这里Xs是输入数据,ys是输出数据
def create_dataset(data, time_steps):
Xs, ys = [], []
for i in range(len(data) - time_steps):
v = data[i:(i + time_steps)]
#前steps的数据是输入
Xs.append(v)
#后steps的数据是输出
ys.append(data[i + time_steps])
#返回输入,输出的结果集,输入(390,10,1),输出(390,1)
return np.array(Xs), np.array(ys)
# 4. 定义 LSTM 模型
# build_model() 函数使用 tf.keras.Sequential 构建一个 LSTM 模型。
# 第一个 LSTM 层有 50 个单元,设置 return_sequences=True 表示输出序列而不是仅输出最后一个时间步的输出。
# 第二个 LSTM 层有 50 个单元。
# 最后一个 Dense 层有 1 个单元,用于输出预测值。
# 使用 adam 优化器和 mse(均方误差)作为损失函数进行编译。
def build_model(input_shape):
model = tf.keras.Sequential([
tf.keras.layers.LSTM(units=50, return_sequences=True, input_shape=input_shape),
tf.keras.layers.LSTM(units=50),
tf.keras.layers.Dense(units=1)
])
model.compile(optimizer='adam', loss='mse')
return model
# 5. 训练模型
# train_model() 函数使用 model.fit() 进行训练,训练数据为 X_train 和 y_train,训练 20 个周期,批次大小为 32,同时设置 10% 的验证集,并关闭数据的随机打乱。
def train_model(model, X_train, y_train, epochs=20):
history = model.fit(
X_train, y_train,
epochs=epochs,
batch_size=32,
validation_split=0.1,
shuffle=False
)
return history
# 6. 预测和可视化结果
# predict_and_visualize() 函数使用训练好的模型进行预测,并将训练集的真实值、预测值以及测试集的真实值和预测值绘制在同一张图中进行对比。
def predict_and_visualize(model, X_train, y_train, X_test, y_test):
train_predict = model.predict(X_train)
test_predict = model.predict(X_test)
plt.figure(figsize=(10, 6))
plt.plot(y_train, label='True Train')
plt.plot(train_predict, label='Predicted Train')
plt.plot(range(len(y_train), len(y_train) + len(y_test)), y_test, label='True Test')
plt.plot(range(len(y_train), len(y_train) + len(y_test)), test_predict, label='Predicted Test')
plt.legend(loc='upper left')
plt.show()
# 7. 采用plot显示项目
def plot_loss(history):
plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(loc='upper right')
plt.show()
if __name__ == "__main__":
# 生成数据
data = generate_time_series()
train_data, test_data = prepare_data(data)
time_steps = 10
X_train, y_train = create_dataset(train_data, time_steps)
X_test, y_test = create_dataset(test_data, time_steps)
print(X_train.shape,y_train.shape)
# 构建模型
input_shape = (X_train.shape[1], X_train.shape[2])
model = build_model(input_shape)
# 训练模型
history = train_model(model, X_train, y_train, epochs=20)
# 显示训练的loss,val_loss
plot_loss(history)
# 预测和可视化
predict_and_visualize(model, X_train, y_train, X_test, y_test)