如何高效搭建针对南海佛山市场的营销型网站?
摘要:营销型网站搭建的工作,南海佛山网站建设,wordpress 会员,wordpress移动端页面模板1.Mnist分类任务 网络基本构建与训练方法,常用函数解析torch.nn.functional模块n
营销型网站搭建的工作,南海佛山网站建设,wordpress 会员,wordpress移动端页面模板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)#偏执的设置常数值作为初始化因为这个东西对模型的影###响不是很大。
