【深度学习 项目实战】一个简单的LSTM实现时间序列预测

我们将通过一个简单实例来讲解LSTM建模过程以及模型的使用方法。实例描述如下,我们有一个整数序列[10, 20, 30, 40, 50, 60, 70],整数序列中共有7个数字,我们的目的或者说我们要解决的问题是,利用已有的这个整数序列来建立模型,用模型预测整数序列的下一个数字应该是多少?也就是整数序列的第8个数字应该是多少?一眼就能看出来,下一个数字应该是80,但是我们需要的是让模型预测出来这个数字。

下面我们梳理一下思路,我们希望向模型输入3个连续的整数,模型能够预测下一个整数,例如,当输入[20, 30, 40]时,模型能够输出[50]。看到这里,想必你已经明白我的意思了,没错,当输入[50, 60, 70]时,我们希望模型能够准确的预测出下一个数字,也就是整数序列的下一个数字。为了训练模型,我们需要把整数序列做一下调整,调整如下:

在这里插入图片描述

X表示模型的输入,Y表示模型的输出,每个X,Y对表示一个样本,X表示样本的输入,Y表示样本标签,在本例中共有4个样本。

用Keras建立模型时,要求模型的输入格式为[samples, timesteps, features](注:samples为样本数,timesteps为时间步数,features为特征数),
我们很期待,当输入[ 50, 60, 70]时,模型能准确预测出80



# LSTM单变量时间序列预测模型
from numpy import array
import numpy as np
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense

# 定义训练样本
X = array([[10, 20, 30], [20, 30, 40], [30, 40, 50], [40, 50, 60]])
y = array([40, 50, 60, 70])

# 将训练样本的数据格式从[samples, timesteps]转换为[samples, timesteps, features]
# X = X.reshape((X.shape[0], X.shape[1], 1))
X = X.reshape((4,3,1))


# 定义模型
model = Sequential()
# 只定义一个LSTM层
model.add(LSTM(50, activation='relu', input_shape=(3, 1)))
# 输出层为全连接层
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
# 训练模型
model.fit(X, y, epochs=1500, verbose=0)
# 用模型进行预测,输入值为[50, 60, 70]
x_input = array([50, 60, 70])
x_input = x_input.reshape((1, 3, 1))
yhat = model.predict(x_input, verbose=0)
print(yhat)



Using TensorFlow backend.
2019-12-19 12:18:15.896560: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_100.dll
2019-12-19 12:18:17.545710: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2019-12-19 12:18:17.569281: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties: 
name: GeForce GTX 1650 major: 7 minor: 5 memoryClockRate(GHz): 1.56
pciBusID: 0000:01:00.0
2019-12-19 12:18:17.569516: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU libraries are statically linked, skip dlopen check.
2019-12-19 12:18:17.570018: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
2019-12-19 12:18:17.570323: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2019-12-19 12:18:17.572096: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties: 
name: GeForce GTX 1650 major: 7 minor: 5 memoryClockRate(GHz): 1.56
pciBusID: 0000:01:00.0
2019-12-19 12:18:17.572320: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU libraries are statically linked, skip dlopen check.
2019-12-19 12:18:17.572812: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
2019-12-19 12:18:18.096889: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1159] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-12-19 12:18:18.097077: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1165]      0 
2019-12-19 12:18:18.097188: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 0:   N 
2019-12-19 12:18:18.098009: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1304] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 2919 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1650, pci bus id: 0000:01:00.0, compute capability: 7.5)
2019-12-19 12:18:18.986839: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_100.dll
[[80.615295]]

Process finished with exit code 0
  • 6
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

东华果汁哥

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值