当前位置:网站首页>pytorch基本操作:使用神经网络进行分类任务
pytorch基本操作:使用神经网络进行分类任务
2022-08-02 05:11:00 【樱花的浪漫】
1.读取Mnist数据
首先,读取Mnist数据,在深度学习框架中,数据的基本结构是tensor,据需转换成tensor才能参与后续建模训练,可用map函数将数据转换为tensor格式
import torch
x_train, y_train, x_valid, y_valid = map(
torch.tensor, (x_train, y_train, x_valid, y_valid)
)
n, c = x_train.shape
x_train, x_train.shape, y_train.min(), y_train.max()
print(x_train, y_train)
print(x_train.shape)
print(y_train.min(), y_train.max())
2.torch.nn.functional
torch.nn.functional中有很多功能, 比如,常见的激活函数、损失函数,一般情况下,如果模型有可学习的参数,最好用nn.Module,其他情况nn.functional相对更简单一些
3.创建一个model
- 必须继承nn.Module且在其构造函数中需调用nn.Module的构造函数
- 无需写反向传播函数,nn.Module能够利用autograd自动实现反向传播
- Module中的可学习参数可以通过named_parameters()或者parameters()返回迭代器
from torch import nn
class Mnist_NN(nn.Module):
def __init__(self):
super().__init__()
self.hidden1 = nn.Linear(784, 128)
self.hidden2 = nn.Linear(128, 256)
self.out = nn.Linear(256, 10)
def forward(self, x):
x = F.relu(self.hidden1(x))
x = F.relu(self.hidden2(x))
x = self.out(x)
return x
打印出来:
通过named_parameters()或者parameters()返回迭代器
4.使用TensorDataset和DataLoader加载数据
TensorDataset:将训练数据的特征和标签组合
DataLoader:随机读取小批量
5.训练模块
梯度下降方法和损失函数
torch默认会叠加梯度,所以结束后需要将梯度置零
- 一般在训练模型时加上model.train(),这样会正常使用Batch Normalization和 Dropout
- 测试的时候一般选择model.eval(),这样就不会使用Batch Normalization和 Dropout
import numpy as np
def fit(steps, model, loss_func, opt, train_dl, valid_dl):
for step in range(steps):
model.train()
for xb, yb in train_dl:
loss_batch(model, loss_func, xb, yb, opt)
model.eval()
with torch.no_grad(): # 验证时不进行梯度下降
losses, nums = zip(
*[loss_batch(model, loss_func, xb, yb) for xb, yb in valid_dl]
)
val_loss = np.sum(np.multiply(losses, nums)) / np.sum(nums) # 平均损失
print('当前step:'+str(step), '验证集损失:'+str(val_loss))
边栏推荐
- 面试官:设计“抖音”直播功能测试用例吧
- 51单片机外设篇:ADC
- MySQL implements sorting according to custom (specified order)
- navicat connects to MySQL and reports an error: 1045 - Access denied for user 'root'@'localhost' (using password YES)
- nacos注册中心
- 25K测试老鸟6年经验的面试心得,四种公司、四种问题…
- 分布式文件存储服务器之Minio对象存储技术参考指南
- 本周大新闻|苹果MR已进行Pre-EVT测试,Quest 2涨价100美元
- 跨桌面端Web容器演进
- Matlab论文插图绘制模板第41期—气泡图(bubblechart)
猜你喜欢
随机推荐
MySQL implements sorting according to custom (specified order)
Cyber Security Learning - Intranet Penetration 4
提高软件测试能力的方法有哪些?看完这篇文章让你提升一个档次
Meta公司新探索 | 利用Alluxio数据缓存降低Presto延迟
Review: image saturation calculation formula and image signal-to-noise (PSNR) ratio calculation formula
MYSQL unique constraint
nacos注册中心
kubernetes affinity, anti-affinity, taint, tolerance
Redis-cluster mode (master-slave replication mode, sentinel mode, clustering mode)
51单片机外设篇:DS18B20
el-input can only input integers (including positive numbers, negative numbers, 0) or only integers (including positive numbers, negative numbers, 0) and decimals
Redis-----非关系数据库
腾讯大咖分享 | 腾讯Alluxio(DOP)在金融场景的落地与优化实践
【漫画】2021满分程序员行为对照表(最新版)
利用浏览器本地存储 实现记住用户名的功能
上海交大牵手淘宝成立媒体计算实验室:推动视频超分等关键技术发展
The Go language learning notes - dealing with timeout - use the language from scratch from Context
高防服务器防御的原理是什么
ApiPost 真香真强大,是时候丢掉 Postman、Swagger 了
Mysql common commands