微网站 建设,dw软件做的网站怎么发到网上,wordpress查看文章id,网站备案图标1.Mnist分类任务 网络基本构建与训练方法#xff0c;常用函数解析 torch.nn.functional模块 nn.Module模块
学习方法#xff1a;边用边查#xff0c;多打印#xff0c;duogua
使用jupyter的优点#xff0c;可以打印出每一个步骤。
2.读取数据集
自动下载
%matplotl…1.Mnist分类任务 网络基本构建与训练方法常用函数解析 torch.nn.functional模块 nn.Module模块
学习方法边用边查多打印duogua
使用jupyter的优点可以打印出每一个步骤。
2.读取数据集
自动下载
%matplotlib inline
#查看本机torch的版本
import torch
print(torch.__version__)#打印torch的版本
加载并读取数据集
from pathlib import Path
import requestsDATA_PATH Path(data)
PATH DATA_PATH / mnistPATH.mkdir(parentsTrue, exist_okTrue)URL http://deeplearning.net/data/mnist/
FILENAME mnist.pkl.gzif not (PATH / FILENAME).exists():content requests.get(URL FILENAME).content(PATH / FILENAME).open(wb).write(content)import pickle
import gzipwith gzip.open((PATH / FILENAME).as_posix(), rb) as f:((x_train, y_train), (x_valid, y_valid), _) pickle.load(f, encodinglatin-1)
观察数据的结构
784是mnist数据集每个样本的像素点个数
print(x_train[0].shape)
print(x_train.shape)
y_train
显示一个记录的灰度图
from matplotlib import pyplot
import numpy as nppyplot.imshow(x_train[0].reshape((28, 28)), cmapgray)
print(x_train.shape) numpy和torch的区别 torch-gpu-tensor numpy-cpu-ndarray
数据转化
import torchx_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())
3 torch.nn.functional 很多层和函数在这里都会见到
torch.nn.functional中有很多功能后续会常用的。那什么时候使用nn.Module什么时候使用nn.functional呢一般情况下如果模型有可学习的参数最好用nn.Module其他情nn.functional相对更简单一些
import torch.nn.functional as Floss_func F.cross_entropydef model(xb):return xb.mm(weights) biasbs 64
xb x_train[0:bs] # a mini-batch from x
yb y_train[0:bs]
weights torch.randn([784, 10], dtype torch.float, requires_grad True)
#线性代数的相关知识weights与输入想乘之后需要输出的格式为10分类所以的weights的矩阵为(784,10)
bs 64
bias torch.zeros(10, requires_gradTrue)#偏执的设置常数值作为初始化因为这个东西对模型的影###响不是很大。print(loss_func(model(xb), yb))#计算真实值和预测值之间的误差
4 创建一个model来更简化代码
必须继承nn.Module且在其构造函数中需调用nn.Module的构造函数无需写反向传播函数nn.Module能够利用autograd自动实现反向传播Module中的可学习参数可以通过named_parameters()或者parameters()返回迭代器
from torch import nnclass 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)#定义前向传播torch有一个优点前向传播自己定义反向传播自动实现。def forward(self, x):x F.relu(self.hidden1(x))x F.relu(self.hidden2(x))x self.out(x)return x
net Mnist_NN()
print(net)可以打印我们定义好名字里的权重和偏置项
for name, parameter in net.named_parameters():print(name, parameter,parameter.size())
5 使用TensorDataset和DataLoader来简化
from torch.utils.data import TensorDataset
from torch.utils.data import DataLoadertrain_ds TensorDataset(x_train, y_train)
train_dl DataLoader(train_ds, batch_sizebs, shuffleTrue)#shuffleTrue洗牌的操作。
valid_ds TensorDataset(x_valid, y_valid)
valid_dl DataLoader(valid_ds, batch_sizebs * 2)def get_data(train_ds, valid_ds, bs):return (DataLoader(train_ds, batch_sizebs, shuffleTrue),DataLoader(valid_ds, batch_sizebs * 2),)
一般在训练模型时加上model.train()这样会正常使用Batch Normalization和 Dropout测试的时候一般选择model.eval()这样就不会使用Batch Normalization和 Dropout