作者:xfziyou315 | 来源:互联网 | 2023-09-23 02:20
Pytorch LSTM时间序列预测https://github.com/pytorch/examples/blob/master/time_sequence_predicti
Pytorch LSTM 时间序列预测
https://github.com/pytorch/examples/blob/master/time_sequence_prediction/generate_sine_wave.py
Pytorch官网提供初学者入门的一个例子,有助于学习Pytorch时间序列预测。本例中使用两个LSTMCell单元学习从不同相位开始的一些正弦波信号,LSTM网络在学习了正弦波之后,试图预测未来的信号值。
generate_sine_wave.py生成模拟数据:
# -*- coding: utf-8 -*-
import numpy as np
import torch
np.random.seed(2)
T = 20
L = 1000
N = 100
x = np.empty((N, L), 'int64')
x[:] = np.array(range(L)) + np.random.randint(-4 * T, 4 * T, N).reshape(N, 1)
data = np.sin(x / 1.0 / T).astype('float64')
torch.save(data, open('traindata.pt', 'wb'))
LSTM数据序列预测:
# -*- coding: utf-8 -*-
from __future__ import print_function
import torch
import torch.nn as nn
import torch.optim as optim
import numpy a