《PyTorch深度学习实践》完结合集 · Hongpu Liu · 多维特征输入与多层神经网络(+测试集验证)(5)

目录

7、处理多维特征的输入与多层神经网络


7、处理多维特征的输入与多层神经网络

 

 

 

 

 

 

 

 

 果然,ReLU好用!!!

#! /usr/bin/env python
# -*- coding: utf-8 -*-

"""
============================================
时间:2021.8.
作者:手可摘星辰不去高声语
文件名:.py
功能:
1、Ctrl + Enter      在下方新建行但不移动光标;
2、Shift + Enter     在下方新建行并移到新行行首;
3、Shift + Enter     任意位置换行
4、Ctrl + D          向下复制当前行
5、Ctrl + Y         删除当前行
6、Ctrl + Shift + V  打开剪切板
7、Ctrl + /          注释(取消注释)选择的行;
8、Ctrl + E       可打开最近访问过的文件
9、Double Shift + /  万能搜索
============================================
"""

import matplotlib.pyplot as plt
import numpy as np
import torch


# 这里一共有759个数据, 我将700个数据作为train, 59个数据作为test
xy = np.loadtxt("E:/AI学习/PyTouch/刘普洪:《PyTorch深度学习实践》完结合集/Code/DataSet/diabetes.csv"
                , delimiter=','
                , dtype=np.float32
                )
x_train_data = torch.from_numpy(xy[0:700, :-1])
y_train_data = torch.from_numpy(xy[0:700, [-1]])
x_test_data = torch.from_numpy(xy[700:, :-1])
y_test_data = torch.from_numpy(xy[700:, [-1]])


class Model(torch.nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.linear1 = torch.nn.Linear(8, 6)
        self.linear2 = torch.nn.Linear(6, 4)
        self.linear3 = torch.nn.Linear(4, 1)
        self.sigmoid = torch.nn.Sigmoid()
        self.activate = torch.nn.ReLU()

    def forward(self, x):
        x = self.activate(self.linear1(x))
        x = self.activate(self.linear2(x))
        x = self.sigmoid(self.linear3(x))
        return x


model = Model()
criterion = torch.nn.BCELoss(size_average=True)
optimzer = torch.optim.SGD(model.parameters(), lr=0.1)

for epoch in range(1000):
    y_pred = model(x_train_data)
    loss = criterion(y_pred, y_train_data)
    print("Epoch:{}\tBCE:{}".format(epoch, loss))
    optimzer.zero_grad()
    loss.backward()
    optimzer.step()


#这里是测试集的验证
y_t = model(x_test_data)
y = y_t.data.numpy()
result = np.where(y > 0.5, 1, 0)  # 满足大于0.5的值保留,不满足的设为0
result = (result - y_test_data.numpy())  # 同真实值相减,如果一致,则为零,寻找0的个数即为测试正确的个数
result = np.count_nonzero(result < 0.1)  # 寻找矩阵中小于0.1的数的个数,就是寻找0个个数
result = result / 59
print("Test Result:", result)

 

 这次的测试集的验证处理还是不错的!!!

以后可以借鉴

猜你喜欢

转载自blog.csdn.net/weixin_44917390/article/details/119733996