pytorch使用torch.nn.Sequential构建网络

以一个线性回归的例子为例:

全部代码

import torch
import numpy as np


def get_x_y():
    x = np.random.randint(0, 50, 300)
    y_values = 2 * x + 21
    x = np.array(x, dtype=np.float32)
    y = np.array(y_values, dtype=np.float32)
    x = x.reshape(-1, 1)
    y = y.reshape(-1, 1)
    return x, y


if __name__ == '__main__':
    train_x, train_y = get_x_y()
    input_size = train_x.shape[1]  # 输入的维度,只是1维
    output_size = 1  # 输出的个数
    batch_size = 16  # 每个 batch 的数量
    my_nn = torch.nn.Sequential(
        torch.nn.Linear(input_size, output_size),
    )
    cost = torch.nn.MSELoss(reduction='mean')  # 使用MSE作为损失函数
    optimizer = torch.optim.Adam(my_nn.parameters(), lr=0.001)  # 优化器

    # 训练网络
    losses = []
    for i in range(1000):
        batch_loss = []
        # MINI-Batch方法来进行训练
        for start in range(0, len(train_x), batch_size):
            end = start + batch_size if start + batch_size < len(train_x) else len(train_x)
            xx = torch.tensor(train_x[start:end], dtype=torch.float, requires_grad=True)
            yy = torch.tensor(train_y[start:end], dtype=torch.float, requires_grad=True)
            prediction = my_nn(xx)  # 自己的网络模型(x数据),会被认为做前向传播
            loss = cost(prediction, yy)  # 计算损失
            optimizer.zero_grad()  # 清零优化器!!!!一定要记得
            loss.backward(retain_graph=True)  # 反向传播,retain_graph表示是否重复执行操作,在循环中需要设置为True
            optimizer.step()  # 更新参数
            batch_loss.append(loss.data.numpy())
        # 打印损失
        if i % 100 == 0:
            losses.append(np.mean(batch_loss))
            print(i, np.mean(batch_loss))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

呆萌的代Ma

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

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

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

打赏作者

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

抵扣说明:

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

余额充值