从下面的例子可以看出,在 Pytorch 中应用深度学习结构非常容易
执行多类分类任务。 在 iris 数据集的训练表现几乎是完美的。

import torch.nn as nn
import torch
#import matplotlib.pyplot as plt
import pandas as pdfrom sklearn.datasets import load_iris
import numpy as np
torch.manual_seed(1121)
<torch._C.Generator at 0x19734b24a70>

下载 iris数据集

iris = load_iris()
X = iris.data
Y = iris.target
X=pd.DataFrame(X)
Y=pd.DataFrame(Y)
display(X)
display(Y)
0 1 2 3
0 5.1 3.5 1.4 0.2
1 4.9 3.0 1.4 0.2
2 4.7 3.2 1.3 0.2
3 4.6 3.1 1.5 0.2
4 5.0 3.6 1.4 0.2
... ... ... ... ...
145 6.7 3.0 5.2 2.3
146 6.3 2.5 5.0 1.9
147 6.5 3.0 5.2 2.0
148 6.2 3.4 5.4 2.3
149 5.9 3.0 5.1 1.8

150 rows × 4 columns

0
0 0
1 0
2 0
3 0
4 0
... ...
145 2
146 2
147 2
148 2
149 2

150 rows × 1 columns

测试和验证数据分离

X=X.values
Y=Y.valuesfrom sklearn.model_selection import train_test_split
x, x_val, y, y_val = train_test_split(X, Y, test_size=0.33, random_state=42)
x.shape, y.shape, x_val.shape, y_val.shape
((100, 4), (100, 1), (50, 4), (50, 1))

注意:标签y变量是一维数据,表示每个分类的编号,而不是编码向量 notice that: y needs to be one dimension indicating the class type, not encoded vectors

x_train = x.reshape(-1, x.shape[1]).astype('float32')
y_train = y.reshape(-1)x_val = x_val.reshape(-1, x_val.shape[1]).astype('float32')
y_val = y_val.reshape(-1)


利用dataloader打包数据,方便batch训练

from torch.utils.data import Dataset, DataLoader
class Data(Dataset):def __init__(self):self.x=torch.from_numpy(x_train)self.y=torch.from_numpy(y_train).long()self.len=self.x.shape[0]def __getitem__(self,index):      return self.x[index], self.y[index]def __len__(self):return self.len
data_set=Data()# define batch sizes here
batch_size = 16
trainloader=DataLoader(dataset=data_set,batch_size=batch_size)

建立softmax 分类器

# D_in, dimension of input layer
# H , dimension of hidden layer
# D_out, dimension of output layer
class Net(nn.Module):def __init__(self,D_in,H,D_out):super(Net,self).__init__()self.linear1=nn.Linear(D_in,H)self.linear2=nn.Linear(H,D_out)def forward(self,x):x=torch.sigmoid(self.linear1(x))  x=self.linear2(x)return x
input_dim=4     # how many features are in the dataset or how many input nodes in the input layer
hidden_dim = 20 # hidden layer size
output_dim=3    # number of classes
print(input_dim,hidden_dim,output_dim)# Instantiate model
model=Net(input_dim,hidden_dim,output_dim)print('W:',list(model.parameters())[0].size())
print('b',list(model.parameters())[1].size())# loss function
criterion=nn.CrossEntropyLoss()
4 20 3
W: torch.Size([20, 4])
b torch.Size([20])

定义优化方法

learning_rate=0.05
optimizer=torch.optim.SGD(model.parameters(), lr=learning_rate)
n_epochs=1000
loss_list=[]#n_epochs
for epoch in range(n_epochs):if epoch %100==0:print(epoch)for x, y in trainloader:#clear gradient optimizer.zero_grad()#make a prediction z=model(x)#print(z)# calculate loss, da Cross Entropy benutzt wird muss ich in den loss Klassen vorhersagen, # also Wahrscheinlichkeit pro Klasse. Das mach torch.max(y,1)[1])loss=criterion(z,y)# calculate gradients of parameters loss.backward()# update parameters optimizer.step()loss_list.append(loss.data)#print('epoch {}, loss {}'.format(epoch, loss.item()))
0
100
200
300
400
500
600
700
800
900

利用验证数据检验训练效果

# predict the class and give probablity for each class label
x_val = torch.from_numpy(x_val)
z=model(x_val)
display(z)
tensor([[-3.1493e+00,  3.6905e+00, -5.0090e-01],[ 8.6639e+00,  2.5913e+00, -1.1538e+01],[-7.7513e+00,  4.1365e-01,  7.2997e+00],[-3.1786e+00,  3.3443e+00, -1.6125e-01],[-3.0974e+00,  3.8265e+00, -6.9203e-01],[ 8.4413e+00,  2.6384e+00, -1.1355e+01],[-1.4718e-02,  4.3311e+00, -4.3462e+00],[-5.4910e+00,  1.5831e+00,  3.8781e+00],[-4.7908e+00,  2.2980e+00,  2.5141e+00],[-1.2132e+00,  4.3453e+00, -3.1134e+00],[-5.0060e+00,  1.9947e+00,  2.9958e+00],[ 8.1200e+00,  2.7792e+00, -1.1151e+01],[ 8.8828e+00,  2.5214e+00, -1.1683e+01],[ 8.0960e+00,  2.7942e+00, -1.1141e+01],[ 8.8283e+00,  2.5107e+00, -1.1654e+01],[-2.5941e+00,  3.7417e+00, -1.1520e+00],[-7.0112e+00,  6.8188e-01,  6.2887e+00],[-1.7457e+00,  4.1789e+00, -2.4035e+00],[-3.3078e+00,  3.3204e+00,  6.4886e-03],[-7.0187e+00,  6.5605e-01,  6.3214e+00],[ 7.9682e+00,  2.8094e+00, -1.1046e+01],[-4.8998e+00,  2.0493e+00,  2.8430e+00],[ 8.2123e+00,  2.6980e+00, -1.1198e+01],[-6.9197e+00,  7.3409e-01,  6.1537e+00],[-5.4359e+00,  2.1912e+00,  3.2544e+00],[-6.1038e+00,  1.1452e+00,  4.9261e+00],[-6.9835e+00,  8.1716e-01,  6.1667e+00],[-6.8937e+00,  7.5489e-01,  6.1001e+00],[ 8.0317e+00,  2.7680e+00, -1.1061e+01],[ 7.8392e+00,  2.8607e+00, -1.0952e+01],[ 9.1619e+00,  2.3973e+00, -1.1890e+01],[ 9.2721e+00,  2.3691e+00, -1.1973e+01],[-1.2764e+00,  4.5603e+00, -3.2536e+00],[ 8.2595e+00,  2.7126e+00, -1.1257e+01],[ 8.4267e+00,  2.6317e+00, -1.1359e+01],[-6.1753e+00,  1.1836e+00,  4.9843e+00],[-1.8449e+00,  4.1870e+00, -2.3373e+00],[ 8.5922e+00,  2.6133e+00, -1.1488e+01],[ 8.7905e+00,  2.5362e+00, -1.1630e+01],[ 9.1236e+00,  2.4451e+00, -1.1888e+01],[-6.4186e+00,  9.5598e-01,  5.4381e+00],[-1.9872e+00,  3.8494e+00, -1.8866e+00],[-2.3989e+00,  4.0929e+00, -1.6734e+00],[ 9.1132e+00,  2.4113e+00, -1.1844e+01],[ 8.8116e+00,  2.5441e+00, -1.1646e+01],[-1.2458e+00,  4.3741e+00, -3.0913e+00],[-4.9320e+00,  2.3103e+00,  2.6452e+00],[-5.8854e+00,  1.5035e+00,  4.3829e+00],[-1.6208e+00,  4.4212e+00, -2.7724e+00],[-6.7249e+00,  8.5383e-01,  5.8288e+00]], grad_fn=<AddmmBackward>)
# choose the predicted class to be the one with maximum probablity
yhat=torch.max(z.data,1)
display(yhat)
torch.return_types.max(
values=tensor([3.6905, 8.6639, 7.2997, 3.3443, 3.8265, 8.4413, 4.3311, 3.8781, 2.5141,4.3453, 2.9958, 8.1200, 8.8828, 8.0960, 8.8283, 3.7417, 6.2887, 4.1789,3.3204, 6.3214, 7.9682, 2.8430, 8.2123, 6.1537, 3.2544, 4.9261, 6.1667,6.1001, 8.0317, 7.8392, 9.1619, 9.2721, 4.5603, 8.2595, 8.4267, 4.9843,4.1870, 8.5922, 8.7905, 9.1236, 5.4381, 3.8494, 4.0929, 9.1132, 8.8116,4.3741, 2.6452, 4.3829, 4.4212, 5.8288]),
indices=tensor([1, 0, 2, 1, 1, 0, 1, 2, 2, 1, 2, 0, 0, 0, 0, 1, 2, 1, 1, 2, 0, 2, 0, 2,2, 2, 2, 2, 0, 0, 0, 0, 1, 0, 0, 2, 1, 0, 0, 0, 2, 1, 1, 0, 0, 1, 2, 2,1, 2]))
# get the indicies
y_pred=yhat.indices.detach().numpy()
display(y_pred)
array([1, 0, 2, 1, 1, 0, 1, 2, 2, 1, 2, 0, 0, 0, 0, 1, 2, 1, 1, 2, 0, 2,0, 2, 2, 2, 2, 2, 0, 0, 0, 0, 1, 0, 0, 2, 1, 0, 0, 0, 2, 1, 1, 0,0, 1, 2, 2, 1, 2], dtype=int64)
display(y_val)
array([1, 0, 2, 1, 1, 0, 1, 2, 1, 1, 2, 0, 0, 0, 0, 1, 2, 1, 1, 2, 0, 2,0, 2, 2, 2, 2, 2, 0, 0, 0, 0, 1, 0, 0, 2, 1, 0, 0, 0, 2, 1, 1, 0,0, 1, 2, 2, 1, 2])

多分类混淆矩阵核验

from sklearn.metrics import multilabel_confusion_matrix
display(multilabel_confusion_matrix(y_val, y_pred))
array([[[31,  0],[ 0, 19]],[[35,  0],[ 1, 14]],[[33,  1],[ 0, 16]]], dtype=int64)

更多关注:
https://www.datasciencebyexample.com/2021/12/26/2021-12-26-1/
datasciencebyexample

利用Pytorch中深度学习网络进行多分类预测(multi-class classification)相关推荐

  1. 如何利用扬声器构建深度学习网络?

    简 介: 来自于康纳尔大学的这篇研究论文给出了 一个利用物理系统实现深层网络学习和推理的框架.本文对于文章举例的三个系统不属于线性时不变系统进行分析.除了其中SHG系统比较复杂之外,其它两个系统(三极 ...

  2. 【机器学习实战】利用sklearn中的逻辑回归对癌症分类预测-良/恶性乳腺癌肿瘤预测

    1. 数据集 数据下载地址:https://archive.ics.uci.edu/ml/machine-learning-databases/ 数据描述 (1)699条样本,共11列数据,第一列用语 ...

  3. 图像超分中的深度学习网络

    图像超分中的深度学习网络 质量评估 操作通道 有监督算法 预上采样 后采样超分 逐步上采样 迭代上下采样 上采样的学习方式 残差块 递归学习 多路径学习 密集连接 通道注意力机制 其他卷积 像素递归网 ...

  4. Pytorch:深度学习中pytorch/torchvision版本和CUDA版本最正确版本匹配、对应版本安装之详细攻略

    Pytorch:深度学习中pytorch/torchvision版本和CUDA版本最正确版本匹配.对应版本安装之详细攻略 目录 深度学习中pytorch/torchvision版本和CUDA版本最正确 ...

  5. 深度学习网络训练中出现nan的原因分析

    报错: nan:Not a Number 该错误导致的后果:造成训练准确率的断崖式下跌 错误原因分析: 1)在loss函数中出现nan 出现原因:一般是因为tf中的log函数输入了'负数'或'0'值( ...

  6. 计算机视觉 | 面试题:06、ReLU函数在0处不可导,为什么在深度学习网络中还这么常用?

    问题 ReLU函数在0处不可导,为什么在深度学习网络中还这么常用? 问题背景 这是在阿里的机器学习岗一面的时候问的一个问题,最开始的问题是"为什么机器学习中解决回归问题的时候一般使用平方损失 ...

  7. 深度学习网络backbone?head、neck、bottleneck、GAP、Embedding、pretext task、downstream task、temperature parameter

    一些术语: backbone这个单词原意指的是人的脊梁骨,后来引申为支柱,核心的意思.在神经网络中,尤其是CV领域,一般先对图像进行特征提取(常见的有vggnet,resnet,谷歌的inceptio ...

  8. 轻量级深度学习网络——ESPNet v2

    ESPNet v2 1 背景介绍 2 相关工作 2.1 轻量化的CNN架构 3 ESPNetv2 3.1 传统方法:2D卷积(看似3D实际是2D因为只在平面移动) 3.2 MoblieNet 深度可分 ...

  9. PyTorch核心贡献者开源书:《使用PyTorch进行深度学习》完整版现已发布!

    来源|新智元 [导读]<使用PyTorch进行深度学习>一书的完整版现已发布!教你如何使用PyTorch创建神经网络和深度学习系统,内含图解与代码,操作易上手. 由Luca Antiga. ...

最新文章

  1. 洛谷P1417 烹调方案
  2. apk反编译工具-apktool
  3. 用友U8 归纳采购退货结算三种情况
  4. 【Python】简单而不简约:一份Python小抄奉上
  5. opengl加载显示3D模型nff类型文件
  6. linux(centos)搭建SVN服务器
  7. 全球500强企业人力资源管理之道
  8. 《活法》中一个故事--令托尔斯泰也折服的人性寓言
  9. #25 centos7(RHEL)系列操作系统的启动流程、systemd的特性、与命令systemctl的使用...
  10. Mybatis动态sql中的foreach标签的使用
  11. 【基于Proteus 8 Professional和Keil uVision5简单共阴极数码管点亮】
  12. No Assembler service found - please make sure that the right jars are in your classpath
  13. Commitizen 互联网公民的简单提交惯例
  14. 51单片机仿真例程-八段数码管
  15. RouterOS(ros)自动更新国内外IP以及端口扫描IP
  16. MaxENT完整操作
  17. 乐视账号服务器关闭,乐视手机重置后无法登录账号 官方给出解决方案
  18. soot的配置以及常见问题的记录
  19. BeanDefinition使用方式
  20. 棱镜门事件的思考:黑客是什么,如何成为黑客?[更新:2015-05-09]

热门文章

  1. 某Xavier载板硬件方案
  2. Alist+KODI打造免费家庭影院
  3. python gevent async_谈谈Python协程技术的演进
  4. 基于罗格里德矩阵的坐标转换7参数求取
  5. 皮卡车行业调研报告 - 市场现状分析与发展前景预测
  6. phython在file同时写入两个_Flink集成数据湖之实时数据写入iceberg
  7. 凯特勒通道(backtrader)
  8. CODESYS领导到访创龙科技,共同助力工业控制软硬件技术发展
  9. mac电脑无法识别移动硬盘怎么解决?看过来!
  10. Selenium学习 - WebElement接口