Datawhale——Pytorch基础(四)

用PyTorch实现多层网络(给代码截图参考)

  1. 引入模块,读取数据
  2. 构建计算图(构建网络模型)
  3. 损失函数与优化器
  4. 开始训练模型
  5. 对训练的模型预测结果进行评估

参考资料

新手必备 | 史上最全的PyTorch学习资源汇总

快速上手笔记,PyTorch模型训练实用教程(附代码)

PyTorch学习笔记

《深度学习框架PyTorch:入门与实践》的对应代码

PyTorch 中文文档

用PyTorch实现多层网络

引入模块

# 构建计算图(构建网络模型)
import torch.nn.functional as F
import torch.nn.init as init
import math
import torch
import torch.nn.init as init
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt 
%matplotlib inline

读取数据

# 如何用一个PyTorch类来搭建神经网络,
# 首先从nn.module中继承整个神经网络的搭建原件,
# 这个类中我们可以继承所有可以用于搭建后续神经网络原件
data = pd.read_csv("diabetes.csv",engine = "python").values
x = torch.from_numpy(data[:,0:-1])
y = torch.from_numpy(data[:,-1])
#划分训练数据和测试数据
x_train, x_test,y_train,y_test= train_test_split(x.numpy(),y.numpy(), test_size=0.3,random_state=2018)   
ss = StandardScaler() 
x_train = torch.tensor(ss.fit_transform(x_train))
x_test = torch.tensor(ss.fit_transform(x_test))
y_train = torch.tensor(y_train)
y_test = torch.tensor(y_test)
x.shape,y.shape,x_train.shape,x_test.shape,y_train.shape,y_test.shape
(torch.Size([768, 8]),
 torch.Size([768]),
 torch.Size([537, 8]),
 torch.Size([231, 8]),
 torch.Size([537]),
 torch.Size([231]))

建立网络模型

# 建立网络模型
class Model(torch.nn.Module):
    def __init__(self):
        super(Model,self).__init__()
        # linear 可以让我们使用整个linear来进行y = Ax + b 俗称affine transformation linear (包含weight和bias) 
        # 定义一个矩阵乘法, 需要填入的参数  一个是in features ,另一个是out features ,代表乘法的两个维度
        self.l1 = torch.nn.Linear(8,10) # torch.nn.Linear中的w和b一定要记得初始化。
        # 如果不进行初始化,nn.Linear将采用默认的初始化进行初始化
        self.l2 = torch.nn.Linear(10,10)
        self.l3 = torch.nn.Linear(10,10)
        self.l4 = torch.nn.Linear(10,1)
#         self.l4 = torch.nn.Linear(10,1)
        # init.normal(self.l1.weight.mean = 0,std = 1. math.sqrt(self.l1.weight.size(1)))
        # 初始化影响训练的效果,Linear类中自带的初始化方法
        # init.normal(self.l1.weight.bias,mean =0 ,std = 1 ) 自己设定的初始化
        # 激活函数,即可以使用nn。又可以直接掉调用nn.founction
        # 直接使用forward函数,对数据进行变换,forward已经在nn.module定义好了
    def forward(self,x):
        out1 = F.tanh(self.l1(x.float()))# 直接调用torch。nn.functional中集成好的ReLu
#         out2 = F.dropout(out1,p = 0.5)
        out2 = F.tanh(self.l2(out1))
#         out4 = F.dropout(out3,p = 0.5)
        out3 = F.tanh(self.l3(out2))
        y_pred = self.l4(out3)
        return y_pred

手写初始化函数 ,对所有层一次赋值

# 手写初始化函数 ,对所有层一次赋值
def weights_init(m):
    classname = m.__class__.__name__
    # 不同种类的神经网络初始化的方法不同,因此对于复杂的网络来说,我们需要针对不同的类进行初始化
    # 例如卷积有卷积初始化的方法,linear有linear类的初始化的方法,因此我们针对不同的类写了下面的初始化的方法代码
    if classname.find("Linear") != -1:
        m.weight.data = torch.randn(m.weight.data.size()[0],m.weight.data.size()[1])
        m.bias.data = torch.randn(m.bias.data.size()[0]) 

 

定义模型

# 定义模型
model = Model()
model.apply(weights_init)
criterion = torch.nn.BCEWithLogitsLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=1e-1, momentum=0.9)
Loss = []
Acc = []
epochs = 5000
for epoch in range(epochs):
    y_pred = model(x_train)
    loss = criterion(y_pred,y_train.float().view(-1,1))
    pred = torch.tensor(y_pred >= 0 )
    corrects = torch.sum(pred.byte() == y_train.view(-1,1).byte())
    acc = corrects.item()/len(x)
    if epoch%1000 ==0:
        print("corrects:",corrects)
        print("epoch = {0}, loss = {1}, acc = {2}".format(epoch, loss, acc))
        Loss.append(loss)
        Acc.append(acc)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
corrects: tensor(344)
epoch = 0, loss = 0.7365305423736572, acc = 0.4479166666666667
corrects: tensor(528)
epoch = 1000, loss = 0.057022031396627426, acc = 0.6875
corrects: tensor(533)
epoch = 2000, loss = 0.022687390446662903, acc = 0.6940104166666666
corrects: tensor(537)
epoch = 3000, loss = 0.0071221026591956615, acc = 0.69921875
corrects: tensor(537)
epoch = 4000, loss = 0.003895694389939308, acc = 0.69921875

绘制loss以及Acc图像

plt.plot(range(len(Loss)), Loss)
plt.ylabel('loss')
plt.xlabel('epochs')
plt.show()
plt.plot(range(len(Acc)), Acc)
plt.ylabel('acc')
plt.xlabel('epochs')
plt.show()

在这里插入图片描述

在这里插入图片描述

模型预测

y_pred = model(x_test)
preds = torch.tensor(y_pred >= 0)
corrects = torch.sum(preds.byte() == y_test.view(-1,1).byte())
acc = corrects.item()/len(x_test)
print("corrects:",corrects.numpy().item())
print("acc = {}".format(acc))
corrects: 147
acc = 0.6363636363636364

结果

  • tanh acc 0.63
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值