栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

动手学深度学习---序列模型

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

动手学深度学习---序列模型

%matplotlib inline
import torch
from torch import nn
from d2l import torch as d2l
T = 1000  # 总共产生1000个点
time = torch.arange(1,T+1,dtype = torch.float32)
x = torch.sin(0.01*time) + torch.normal(0,0.2,(T,))
d2l.plot(time, [x], 'time', 'x', xlim=[1, 1000], figsize=(6, 3))

tau =4
features = torch.zeros((T-tau,tau))# 996*4
for i in range(tau):
    features[:,i] = x[i:T-tau+i]#features第0列赋值x【0:996】 第二列赋值【1:997】
labels = x[tau:].reshape((-1,1))# 996*1
#600个值为训练
batch_size,n_train = 16,600
train_iter = d2l.load_array((features[:n_train],labels[:n_train]),batch_size)
#初始化网络权重
def init_weights(m):
    if type(m) == nn.Linear:
        nn.init.xavier_uniform_(m.weight)
# 一个简单的多层感知机
def get_net():
    net = nn.Sequential(nn.Linear(4,10),nn.ReLU(),nn.Linear(10,1))
    net.apply(init_weights)
    return net
# 平方损失。注意:MSELoss计算平方误差时不带系数1/2
loss = nn.MSELoss(reduction="none")
#训练
import numpy as np
def train(net,train_iter,loss,epochs,lr):
    optimizer = torch.optim.Adam(net.parameters(),lr)
    for epoch in range(epochs):
        net.train()
        losses = []
        for X,y in train_iter:
            optimizer.zero_grad()
            l = loss(net(X),y)
            l.sum().backward()
            optimizer.step()
            losses.append(l.sum())
        print(f"epoch: {epoch+1},loss: {d2l.evaluate_loss(net,train_iter,loss)}")
net = get_net()
train(net,train_iter,loss,5,0.01)
epoch: 1,loss: 0.061126494656006496
epoch: 2,loss: 0.05946366389592488
epoch: 3,loss: 0.05584181000789006
epoch: 4,loss: 0.055096316635608676
epoch: 5,loss: 0.056480858325958255
#画出单步预测
onestep_preds = net(features)
d2l.plot([time,time[tau:]],[x.detach().numpy(), onestep_preds.detach().numpy()],'time',
         'x',legend=['data','1-step preds'],xlim=[1,1000],figsize=(6,3))

multistep_preds = torch.zeros(T)
multistep_preds[:n_train+tau]=x[:n_train+tau]
for i in range(n_train+tau,T):
    multistep_preds[i] = net(multistep_preds[i-tau:i].reshape((1,-1)))
    
onestep_preds = net(features)
d2l.plot([time,time[tau:],time[n_train + tau:]],[x.detach().numpy(), onestep_preds.detach().numpy(),multistep_preds[n_train + tau:].detach().numpy()],'time',
         'x',legend=['data','1-step preds','multistep preds'],xlim=[1,1000],figsize=(6,3))

#基于(k = 1, 4, 16, 64),通过对整个序列预测的计算, 让我们更仔细地看一下(k)步预测的困难。
max_steps = 64
features = torch.zeros((T - tau - max_steps + 1, tau + max_steps)) #933*68
# 列i(i 
tensor([[-0.4184,  0.4557,  0.2906,  ...,  0.0000,  0.0000,  0.0000],
        [ 0.4557,  0.2906, -0.0850,  ...,  0.0000,  0.0000,  0.0000],
        [ 0.2906, -0.0850,  0.0959,  ...,  0.0000,  0.0000,  0.0000],
        ...,
        [ 0.4088,  0.1587,  0.1699,  ...,  0.0000,  0.0000,  0.0000],
        [ 0.1587,  0.1699, -0.0654,  ...,  0.0000,  0.0000,  0.0000],
        [ 0.1699, -0.0654,  0.2530,  ...,  0.0000,  0.0000,  0.0000]])
# 列i(i>=tau)是来自(i-tau+1)步的预测,其时间步从(i+1)到(i+T-tau-max_steps+1)
#
for i in range(tau, tau + max_steps):
    features[:, i] = net(features[:, i - tau:i]).reshape(-1)
steps = (1, 4, 16, 64)
d2l.plot([time[tau + i - 1: T - max_steps + i] for i in steps],
         [features[:, (tau + i - 1)].detach().numpy() for i in steps], 'time', 'x',
         legend=[f'{i}-step preds' for i in steps], xlim=[5, 1000],
         figsize=(6, 3))

总结
  1. 内插法(在现有观测值之间进行估计)和外推法(对超出已知观测范围进行预测)在实践的难度上差别很大。因此,对于你所拥有的序列数据,在训练时始终要尊重其时间顺序,即最好不要基于未来的数据进行训练。

  2. 序列模型的估计需要专门的统计工具,两种较流行的选择是自回归模型和隐变量自回归模型。

  3. 对于时间是向前推进的因果模型,正向估计通常比反向估计更容易。

  4. 对于直到时间步(t)的观测序列,其在时间步(t+k)的预测输出是“(k)步预测”。随着我们对预测时间(k)值的增加,会造成误差的快速累积和预测质量的极速下降。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/926188.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号