搜索
您的当前位置:首页正文

深度学习入门:pytorch实现鸢尾花分类

来源:哗拓教育
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler  # 特征预处理
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as opt
import random


# 构建网络结构
class MLP(nn.Module):
    def __init__(self):
        super(MLP, self).__init__()
        self.fp1 = nn.Linear(4, 20)
        self.f1 = nn.ReLU()
        # fp1 是输入层
        self.fp2 = nn.Linear(20, 20)
        # fp2 是隐藏层
        self.f2 = nn.ReLU()
        self.fp3 = nn.Linear(20, 3)

    def forward(self, x):
        x = self.fp1(x)
        x = self.f1(x)
        x = self.fp2(x)
        x = self.f2(x)
        x = self.fp3(x)
        return x


def data_loder(data, label, batch_size=12, mode="train"):
    # 本来这里还有数据读取的操作 和 数据预处理的操作
    # 但是考虑到已经在函数外读取数据了 所以就省略了
    # 而且数据是干净的
    def gen_loder():
        x_list = []
        # x_list 就是一个batch组
        y_list = []
        index_list = list(range(len(data)))
        # index_list 里面放的是 数据的索引
        if mode == "train":
            random.shuffle(index_list)
        # 如果他是要训练集的话 那么就打乱数据
        for j in index_list:
            x_list.append(data[j].astype("float32"))
            y_list.append(label[j].astype("int64"))
            if len(x_list) == batch_size:
                yield np.array(x_list), np.array(y_list)
                x_list = []
                y_list = []
        #         清空x_list y_list
        if len(x_list) > 0:
            yield np.array(x_list), np.array(y_list)
    return gen_loder


def acc(pres, labels):
    """用来计算正确率的"""
    result = pres == labels
    result = list(result.numpy())
    t = result.count(True)
    return t / len(labels)


# 获取数据集
iris = load_iris()
x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=6)
# 特征工程
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)
# [120, 4] 有120个数据 4个特征
# numpy 一个值 print ->[8]
# tensor 一个值 print ->tensor([8], dtype = int64)
# 创建模型对象

net = MLP()
# 网络的算子
# 网络的损失函数应该选择什么? 如果是分类问题就选择交叉熵损失函数,如果是回归问题就选择MSE
# 网络的优化器该选择什么?
optm = opt.Adam(net.parameters(), lr=0.0005, weight_decay=2e-5)
epoch = 500
train_loder = data_loder(x_train, y_train)
test_loder = data_loder(x_test, y_test)
best = 0

for i in range(epoch):
    net.train()
    # 确保模型的参数能够进行更新
    for bid, data in enumerate(train_loder()):
        input = torch.tensor(data[0])
        label = torch.tensor(data[1])
        pre = net(input)
        loss = F.cross_entropy(pre, label)
        pre = torch.argmax(pre, dim=1)
        ac = acc(pre, label)
        optm.zero_grad()
        loss.backward()
        optm.step()
        print("epoch:{}, bid:{}, loss:{},ac:{}".format(i, bid, loss.cpu().detach().numpy(), ac))
    with torch.no_grad():
        accd = []
        net.eval()
        for bid, (data, label) in enumerate(test_loder()):
            data = torch.tensor(data)
            label = torch.tensor(label)
            pre = net(data)
            pre = torch.argmax(pre, dim=1)
            accd.append(acc(pre, label))
        ac = np.array(accd).mean()
        print("epoch:{}, ac:{}".format(i, ac))
        if ac > best:
            best = ac
            # torch.save(net.state_dict(), "path")
print("best:{}".format(best))

因篇幅问题不能全部显示,请点此查看更多更全内容

Top