Pytorch基础之——(四)用PyTorch实现多层网络

1.引入模块,读取数据

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.init as init
import pandas as pd 
import numpy as np
import random
from sklearn.preprocessing import StandardScaler

# 参数
ratio = 0.8
epochs = 500
learning_rate = 0.1

# 加载数据,划分数据集
datas = torch.from_numpy(pd.read_csv('diabetes.csv').values)
length = len(datas)
indices = list(range(length))
random.shuffle(indices)
train_idx, test_idx = indices[:int(ratio*length)], indices[int(ratio*length):]
train, test = datas[train_idx], datas[test_idx]
x_train, y_train = train[:,:-1], train[:,-1]
x_test, y_test = test[:,:-1], test[:,-1]
# 数据归一化
# m = nn.BatchNorm1d(8, affine=False)
# x_train, x_test = m(x_train), m(x_test)
ss = StandardScaler()
x_train = torch.tensor(ss.fit_transform(x_train))
x_test = torch.tensor(ss.transform(x_test))

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

搭建网络

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.linear1 = torch.nn.Linear(8, 10)
        self.linear2 = torch.nn.Linear(10, 20)
        self.linear3 = torch.nn.Linear(20, 10)
        self.linear4 = torch.nn.Linear(10, 1)
    def forward(self, x):
        x = F.tanh(self.linear1(x))
        x = F.tanh(self.linear2(x))
        x = F.tanh(self.linear3(x))
        return self.linear4(x)

3.损失函数与优化器

model = Model()
criterion = torch.nn.BCEWithLogitsLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate, momentum=0.9)

4.开始训练模型

# 训练
for epoch in range(epochs):
    y_pred = model(x_train)
    loss = criterion(y_pred, y_train)
    preds = torch.tensor(y_pred>=0)
    corrects = torch.sum(preds.byte() == y_train.view(-1,1).byte())
    acc = corrects.item()/len(x_train)
    print(acc)

    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

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

# 测试
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(acc)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值