03 Pytorch代码实践之【非线性回归】

import torch
import matplotlib.pyplot as plt
from torch import nn, optim
from time import perf_counter
x = torch.unsqueeze(torch.linspace(-3, 3, 10000), dim=1)
y = x.pow(3) + 0.3*torch.rand(x.size())
plt.scatter(x.numpy(), y.numpy(), s=0.01)
plt.show()

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hGVcLJis-1600866930893)(output_0_0.png)]

import torch
import matplotlib.pyplot as plt
from torch import nn, optim
from time import perf_counter

x = torch.unsqueeze(torch.linspace(-3, 3, 10000), dim=1)
y = x.pow(3) + 0.3 * torch.rand(x.size())


class Net(nn.Module):
    def __init__(self, input_feature, num_hidden, outputs):
        super(Net, self).__init__()
        self.hidden = nn.Linear(input_feature, num_hidden)
        self.out = nn.Linear(num_hidden, outputs)

    def forward(self, x):
        x = torch.nn.functional.relu(self.hidden(x))
        x = self.out(x)
        return x


CUDA = torch.cuda.is_available()
if CUDA:
    net = Net(input_feature=1, num_hidden=20, outputs=1).cuda()
    inputs = x.cuda()
    target = y.cuda()
else:
    net = Net(input_feature=1, num_hidden=20, outputs=1)
    inputs = x
    target = y

optimizer = optim.SGD(net.parameters(), lr=0.01)
criterion = nn.MSELoss()

for name, param in net.named_parameters(): #查看可优化的参数有哪些
  if param.requires_grad:
    print(name)

def draw(output, loss):
    plt.cla()  # 清空画布
    if CUDA:
        output = output.cpu()  # 还原为cpu类型才能进行绘图
    plt.scatter(x.numpy(), y.numpy(), s=0.001)
    plt.plot(x.numpy(), output.data.numpy(), 'r-', lw=5)
    plt.text(0.5, 0, 'loss=%s' % (loss.item()),
             fontdict={'size': 20, 'color': 'red'})
    plt.pause(0.005)


def train(model, criterion, optimizer, epochs):
    for epoch in range(epochs):
        outputs = model(inputs)
        loss = criterion(outputs, target)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        if epoch % 100 == 0:
            draw(outputs, loss)
    return model, loss


start = perf_counter()
model, loss = train(net, criterion, optimizer, 5000)
finish = perf_counter()
time = finish - start
print("计算时间:%s" % time)
print("final loss:", loss.item())
print("weights:", list(model.parameters()))

hidden.weight
hidden.bias
out.weight
out.bias

在这里插入图片描述

在这里插入图片描述

   计算时间:16.568853700000545
    final loss: 0.020885242149233818
    weights: [Parameter containing:
    tensor([[ 0.9034],
            [ 0.1697],
            [-1.6006],
            [-1.3026],
            [-1.5112],
            [-0.1496],
            [-1.3453],
            [-1.1424],
            [ 0.7827],
            [-0.8794],
            [-0.3463],
            [-1.7504],
            [-0.3597],
            [ 1.7153],
            [-0.5268],
            [-0.1672],
            [ 1.3576],
            [ 1.5040],
            [ 1.0761],
            [ 1.3660]], device='cuda:0', requires_grad=True), Parameter containing:
    tensor([-0.9302,  0.5086, -3.1035, -1.1606, -3.5677, -0.8621, -0.7312,  0.3425,
            -1.6159, -2.2425,  1.0380, -2.4755,  1.0784, -4.1484,  1.1116,  0.5482,
            -1.7674, -0.9522, -1.7016, -2.5550], device='cuda:0',
           requires_grad=True), Parameter containing:
    tensor([[ 1.2527,  0.4581, -3.3161, -1.4887, -3.6762,  0.0402, -1.0628, -0.1567,
              1.6498, -2.3455, -0.4392, -2.8871, -0.6040,  4.2925,  0.7922, -0.0791,
              2.1095,  1.6585,  1.9316,  2.8379]], device='cuda:0',
           requires_grad=True), Parameter containing:
    tensor([0.2591], device='cuda:0', requires_grad=True)]
    
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Shine.Zhang

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

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

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

打赏作者

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

抵扣说明:

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

余额充值