RNN_Regression

本文介绍了一个使用PyTorch实现的循环神经网络(RNN)模型,该模型用于预测sin函数输入对应的cos函数输出。通过10个时间步长的训练,模型能够学习并预测出cos函数的值,展示了RNN在网络序列数据预测中的应用。
部署运行你感兴趣的模型镜像
"""
View more, visit my tutorial page: https://morvanzhou.github.io/tutorials/
My Youtube Channel: https://www.youtube.com/user/MorvanZhou
Dependencies:
torch: 0.4
matplotlib
numpy
"""
from torchsummary import summary
from torchviz import make_dot
import torch
from torch import nn
import numpy as np
import matplotlib.pyplot as plt

# torch.manual_seed(1)    # reproducible

# Hyper Parameters
TIME_STEP = 10  # rnn time step
INPUT_SIZE = 1  # rnn input size
LR = 0.02  # learning rate

# show data
# steps = np.linspace(0, np.pi * 2, 100, dtype=np.float32)  # float32 for converting torch FloatTensor
# x_np = np.sin(steps)
# y_np = np.cos(steps)
# plt.plot(steps, y_np, 'r-', label='target (cos)')
# plt.plot(steps, x_np, 'b-', label='input (sin)')
# plt.legend(loc='best')
# plt.show()


class RNN(nn.Module):
    def __init__(self):
        super(RNN, self).__init__()

        self.rnn = nn.RNN(
            input_size=INPUT_SIZE,
            hidden_size=32,  # rnn hidden unit
            num_layers=1,  # number of rnn layer
            batch_first=True,  # input & output will has batch size as 1s dimension. e.g. (batch, time_step, input_size)
        )
        self.out = nn.Linear(32, 1)

    def forward(self, x, h_state): # 因为 hidden state 是连续的, 所以我们要一直传递这一个 state
        # x (batch, time_step, input_size)
        # h_state (n_layers, batch, hidden_size)
        # r_out (batch, time_step, hidden_size) 1*10*32
        # 在rnn分类中 是批量放入28条数据 每一条输出的h_n,h_c自动传入下一条,自动了27次,最后一次输出了
        # 但在回归问题中 每个batch数据需要有之前的数据,所以上一个batch的h_state显式传给了下个batch
        # 一个batch内的h_state是隐式传递的 batch间的h_state是显式传递的
        r_out, h_state = self.rnn(x, h_state)
        print(torch._shape_as_tensor(r_out)) #tensor([ 1, 10, 32])

        outs = []  # save all predictions
        for time_step in range(r_out.size(1)):  # calculate output for each time step 从1到10(TIME_STEP)
            outs.append(self.out(r_out[:, time_step, :]))
        #r_out 为1*10*32,r_out[:, time_step, :]为1*1*32,过全连接得到1*1*1 TIME_STEP个append之后
        # 相比上一个回归中的输出 这里outs存储的是每一个时间步的输出
        # print(len(outs)) #为10
        return torch.stack(outs, dim=1), h_state #从10*1*1变为1*10*1
        # torch.Size([1, 10, 1]) time_step

        # instead, for simplicity, you can replace above codes by follows
        # r_out = r_out.view(-1, 32)
        # outs = self.out(r_out)
        # outs = outs.view(-1, TIME_STEP, 1)
        # return outs, h_state

        # or even simpler, since nn.Linear can accept inputs of any dimension
        # and returns outputs with same dimension except for the last
        # outs = self.out(r_out)
        # return outs


rnn = RNN()
print(rnn)

optimizer = torch.optim.Adam(rnn.parameters(), lr=LR)  # optimize all cnn parameters
loss_func = nn.MSELoss()

h_state = None  # for initial hidden state

plt.figure(1, figsize=(12, 5))
plt.ion()  # continuously plot

for step in range(85):
    start, end = step * np.pi, (step + 1) * np.pi  # time range
    # use sin predicts cos
    steps = np.linspace(start, end, TIME_STEP, dtype=np.float32,
                        endpoint=False)  # float32 for converting torch FloatTensor
    x_np = np.sin(steps)
    y_np = np.cos(steps)

    x = torch.from_numpy(x_np[np.newaxis, :, np.newaxis])  # shape (batch, time_step, input_size)
    y = torch.from_numpy(y_np[np.newaxis, :, np.newaxis])

    prediction, h_state = rnn(x, h_state)  # rnn output
    #print(prediction.size())
    # !! next step is important !!
    # !!  下一步十分重要 !!
    h_state = h_state.data  # 要把 h_state 重新包装一下才能放入下一个 iteration, 不然会报错

    loss = loss_func(prediction, y)  # calculate loss
    optimizer.zero_grad()  # clear gradients for this training step
    loss.backward()  # backpropagation, compute gradients
    optimizer.step()  # apply gradients

    # plotting
    plt.plot(steps, y_np.flatten(), 'r-')
    plt.plot(steps, prediction.data.numpy().flatten(), 'b-')
    plt.draw();
    plt.pause(0.05)

#summary(rnn, input_size=(1,10,1)) # input_size=(channels, H, W)

# 可视化网络2
vis_graph = make_dot(prediction, params=dict(rnn.named_parameters()))
vis_graph.view()
plt.ioff()
plt.show()

您可能感兴趣的与本文相关的镜像

PyTorch 2.5

PyTorch 2.5

PyTorch
Cuda

PyTorch 是一个开源的 Python 机器学习库,基于 Torch 库,底层由 C++ 实现,应用于人工智能领域,如计算机视觉和自然语言处理

for i, data in enumerate(tqdm(trainloader)): exemplar_imgs, instance_imgs, regression_target, conf_target = data regression_target, conf_target = regression_target.cuda(), conf_target.cuda() pred_score, pred_regression = model(exemplar_imgs.cuda(), instance_imgs.cuda()) pred_conf = pred_score.reshape(-1, 2, config.anchor_num * config.score_size * config.score_size).permute(0, 2, 1) pred_offset = pred_regression.reshape(-1, 4, config.anchor_num * config.score_size * config.score_size).permute(0, 2, 1) cls_loss = rpn_cross_entropy_balance(pred_conf, conf_target, config.num_pos, config.num_neg, anchors, ohem_pos=config.ohem_pos, ohem_neg=config.ohem_neg) reg_loss = rpn_smoothL1(pred_offset, regression_target, conf_target, config.num_pos, ohem=config.ohem_reg) loss = cls_loss + config.lamb * reg_loss #分类权重和回归权重 optimizer.zero_grad()#梯度 loss.backward() torch.nn.utils.clip_grad_norm_(model.parameters(), config.clip)#config.clip=10 ,clip_grad_norm_梯度裁剪,防止梯度爆炸 optimizer.step() step = (epoch - 1) * len(trainloader) + i summary_writer.add_scalar('train/cls_loss', cls_loss.data, step) summary_writer.add_scalar('train/reg_loss', reg_loss.data, step) train_loss.append(loss.detach().cpu())#当前计算图中分离下来的,但是仍指向原变量的存放位置,requires_grad=false loss_temp_cls += cls_loss.detach().cpu().numpy() loss_temp_reg += reg_loss.detach().cpu().numpy() if (i + 1) % config.show_interval == 0: tqdm.write("[epoch %2d][iter %4d] cls_loss: %.4f, reg_loss: %.4f lr: %.2e" % (epoch, i, loss_temp_cls / config.show_interval, loss_temp_reg / config.show_interval, optimizer.param_groups[0]['lr'])) loss_temp_cls = 0 loss_temp_reg = 0这是什么意思?
最新发布
11-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值