Pytorch实现一个简单回归模型

Pytorch实现一个简单回归模型

之前学习了一些简单的tensor基础知识,现在我们就由浅入深学习如何使用pytorch实现神经网络,完成回归任务和分类任务。在本小节主要带领大家学习回归任务的代码编写,另外,本人参考的学习资料为【莫烦Python】,有兴趣观看视频的同学可以观看视频资料https://www.youtube.com/user/MorvanZhou,该作者讲解的很详细,推荐大家前往学习,特别是像我这种初入深度学习领域的小白。

本人写该博客的目的其一是自己学习了一些知识做一下记录,另外也是为【莫烦Python】做下推广,我是看了他的视频,感觉使用pytorch有种上手的感觉。

第一步:引入所需调用的package
编写os.environ[“KMP_DUPLICATE_LIB_OK”]="TRUE"的原因:如果不添加该语句程序可能会出现错误:OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.
OMP: Hint This means that multiple copies of the OpenMP runtime have been linked into the program. That is dangerous, since it can degrade performance or cause incorrect results. The best thing to do is to ensure that only a single OpenMP runtime is linked into the process, e.g. by avoiding static linking of the OpenMP runtime in any library. As an unsafe, unsupported, undocumented workaround you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the program to continue to execute, but that may cause crashes or silently produce incorrect results. For more information, please see http://www.intel.com/software/products/support/.

import torch
import torch.nn.functional as F
import matplotlib.pyplot as plt
import os
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"

第二步:先定义训练模型的训练数据
torch.linspace(-1, 1, 100)是在-1到1之间均匀取100个点。使用torch.unsqueeze的原因在于仅仅使用torch.linspace(-1, 1, 100)是一个一维的数据,而torch内需要至少二维类型的数据,因此使用torch.unsqueeze扩大了维度。

x = torch.unsqueeze(torch.linspace(-1, 1, 100), dim=1)  # x data (tensor), shape=(100, 1)
y = x.pow(2) + 0.2*torch.rand(x.size())                 # noisy y data (tensor), shape=(100, 1)

可视化生成的数据:
在这里插入图片描述

第三步:定义网络结构

class Net(torch.nn.Module): # torch.nn.Module是固定的,定义不同的网络结构都会编写
    def __init__(self, n_feature, n_hidden, n_output):
        super(Net, self).__init__()  #记住,在定义不同的网络结构都会用到super(Net, self).__init__(),Net与自己定义的网络class命名保持一致
        self.hidden = torch.nn.Linear(n_feature, n_hidden)   # hidden layer
        self.predict = torch.nn.Linear(n_hidden, n_output)   # output layer

    def forward(self, x):
        x = F.relu(self.hidden(x))      # activation function for hidden layer
        x = self.predict(x)             # linear output
        return x

net = Net(n_feature=1, n_hidden=10, n_output=1)     # define the network,因为我们生成的数据每个数据的特征数为1,因此n_feature=1,该网络的任务为回归,即输出为一个数据值,所以输出为1
print(net)  # net architecture  Net(
                                   # (hidden): Linear(in_features=1, out_features=10, bias=True)
                                   # (predict): Linear(in_features=10, out_features=1, bias=True)
)

第四步:设置优化参数和损失函数
优化参数的方法有很多种,这里使用的是最常见之一的随机梯度下降方法SGD,回归任务最常见的损失函数均方误差MSE。

optimizer = torch.optim.SGD(net.parameters(), lr=0.2)
loss_func = torch.nn.MSELoss()  # this is for regression mean squared loss

第五步:进行训练

for t in range(200):  # 200在这里表示训练迭代200次
    prediction = net(x)     # input x and predict based on x

    loss = loss_func(prediction, y)     # must be (1. nn output, 2. target)
    optimizer.zero_grad()   # clear gradients for next train
    loss.backward()         # backpropagation, compute gradients
    optimizer.step()        # apply gradients
    '''每个网络训练都包含四步,1.预测;2.计算损失;3.优化器梯度清零;4.反馈计算梯度;5.应用梯度。特别是这三步的代码基本上在不同的代码里面都是一成不变的。
    optimizer.zero_grad()   # clear gradients for next train
    loss.backward()         # backpropagation, compute gradients
    optimizer.step()        # apply gradients
    '''

整体代码
因为没有测试代码,因此添加了可视化的网络学习过程。

import torch
import torch.nn.functional as F
import matplotlib.pyplot as plt
import os
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"

x = torch.unsqueeze(torch.linspace(-1, 1, 100), dim=1)  # x data (tensor), shape=(100, 1)
y = x.pow(2) + 0.2*torch.rand(x.size())                 # noisy y data (tensor), shape=(100, 1)

class Net(torch.nn.Module):
    def __init__(self, n_feature, n_hidden, n_output):
        super(Net, self).__init__()
        self.hidden = torch.nn.Linear(n_feature, n_hidden)   # hidden layer
        self.predict = torch.nn.Linear(n_hidden, n_output)   # output layer

    def forward(self, x):
        x = F.relu(self.hidden(x))      # activation function for hidden layer
        x = self.predict(x)             # linear output
        return x

net = Net(n_feature=1, n_hidden=10, n_output=1)     # define the network
print(net)  # net architecture

optimizer = torch.optim.SGD(net.parameters(), lr=0.2)
loss_func = torch.nn.MSELoss()  # this is for regression mean squared loss

plt.ion()   # something about plotting

for t in range(200):
    prediction = net(x)     # input x and predict based on x

    loss = loss_func(prediction, y)     # must be (1. nn output, 2. target)

    optimizer.zero_grad()   # clear gradients for next train
    loss.backward()         # backpropagation, compute gradients
    optimizer.step()        # apply gradients

    if t % 5 == 0:
        # plot and show learning process
        plt.cla()
        plt.scatter(x.data.numpy(), y.data.numpy())
        plt.plot(x.data.numpy(), prediction.data.numpy(), 'r-', lw=5)
        plt.text(0.5, 0, 'Loss=%.4f' % loss.data.numpy(), fontdict={'size': 20, 'color':  'red'})
        plt.pause(0.1)
plt.ioff()
plt.show()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值