51序列模型
import torch
from torch import nn
from d2l import torch as d2l
import matplotlib.pyplot as plt
T = 1000
time = torch.arange(1, T + 1, dtype=torch.float32)
x = torch.sin(0.01 * time) + torch.normal(mean=0, std=0.2, size=(T,))
d2l.plot(time, [x], 'time', 'x', xlim=[1, 1000], figsize=(6, 3))
plt.show()
tau = 4
features = torch.zeros((T - tau, tau))
for i in range(tau):
features[:, i] = x[i: T - tau + i]
"""
x = [x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, ...]
features = [
[x0, x1, x2, x3],
[x1, x2, x3, x4],
[x2, x3, x4, x5],
[x3, x4, x5, x6],
[x4, x5, x6, x7],
[x5, x6, x7, x8],
...
]
"""
labels = x[tau:].reshape((-1, 1))
batch_size, n_train = 16, 600
train_iter = d2l.load_array(