pytorch task4 用PyTorch实现多层网络

  1. 引入模块,读取数据
import numpy as np
import torch
from torch.autograd import Variable
import matplotlib.pyplot as plt
import torch.nn.functional as F

# import torch.nn.init as init
#
# import math
#
# # %matplotlib inline


# 如何用一个pytorch类来搭建神经网络呢,首先我们要能够从nn.module这个种继承整个神经网络的搭建元件,
# 这个类中我们可以继承所有可以用于搭建后续神经网络的元件。

xy = np.loadtxt('./data/diabetes.csv.gz', delimiter=',', dtype=np.float32)
x_data = Variable(torch.from_numpy(xy[:, 0:-1]))
y_data = Variable(torch.from_numpy(xy[:, [-1]]))

print(x_data.data.shape)
print(y_data.data.shape)

2.构建计算图(构建网络模型)



# 建立网络模型
class Model(torch.nn.Module):
    def __init__(self):
        """
        In the constructor we instantiate two nn.Linear module
        """
        super(Model, self).__init__()
        # linear 可以让我们使用整个linear来进行y = Ax + b俗称affline transformation linear 包含weight和bias
        # 定义一个矩阵乘法,需要填入的两个数字一个是in features ,一个是out features,代表乘法的两个维度
        self.l1 = torch.nn.Linear(8, 6)
        # torch.nn.Linear中的w和b一定要记得初始化,如果不进行初始化的话,nn.Linear 默认的初始化进行初始化
        self.l2 = torch.nn.Linear(4, 1)
        # init.normal(self.l1.weight,mean=0,std=1. / math.sqrt(self.l1.weight.size(1))) 初始化影响训练的效果,Linear类中自带的初始化方法。
        # init.normal(self.l1.bias,mean=0,std=1)#自己设定的初始化
        # 激活函数,既可以使用nn,又可以直接调用nn.functiona
        # 直接使用forward函数,对数据进行变换,forward已经在nn.module定义好了

    def forward(self, x):
        """
        In the forward function we accept a Variable of input data and we must return
        a Variable of output data.We can use Modules defined in the consturctor as well as arbitrary on Variables.
       """
        out1 = F.relu(self.l1(x))
        out2 = F.dropout(out1, p=0.5)
        out3 = F.relu(self.l2(out2))
        out4 = F.dropout(out3, p=0.5)
        y_pred = F.sigmoid(self.l3(out4))
        return y_pred

3.损失函数与优化器

def weights_init(m):
    classname = m.__class__.__name__
    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.BECLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)

4.开始训练模型


Loss = []
for epoch in range(2000):
    y_pred = model(x_data)
    loss = criterion(y_pred, y_data)
    if epoch % 100 == 0:
        print("epoch = ", epoch, "loss = ", loss.data[0])
        Loss.append(loss.data[0])
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

5.对训练的模型预测结果进行评估

hour_var = Variable(torch.randn(1, 8))
print("predict", model(hour_var).data[0] > 0.5)
plt.plot(Loss)

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值