当前位置: 首页 > news >正文

河南网站建设途径有什么模板网站系统

河南网站建设途径有什么,模板网站系统,怎么自己开发网址,网站规划的解释Torch安装的方法 学习方法 1.边用边学,torch只是一个工具,真正用,查的过程才是学习的过程2.直接就上案例就行,先来跑,遇到什么来解决什么 Mnist分类任务: 网络基本构建与训练方法,常用函数解析…

Torch安装的方法

在这里插入图片描述

学习方法

  • 1.边用边学,torch只是一个工具,真正用,查的过程才是学习的过程
  • 2.直接就上案例就行,先来跑,遇到什么来解决什么

Mnist分类任务:

  • 网络基本构建与训练方法,常用函数解析

  • torch.nn.functional模块

  • nn.Module模块

读取Mnist数据集

  • 会自动进行下载
# 查看自己的torch的版本
import torch
print(torch.__version__)
%matplotlib inline
# 前两步,不用管是在网上下载数据,后续的我们都是在本地的数据进行操作
from pathlib import Path
import requestsDATA_PATH = Path("data")
PATH = DATA_PATH / "mnist"PATH.mkdir(parents=True, exist_ok=True)URL = "http://deeplearning.net/data/mnist/"
FILENAME = "mnist.pkl.gz"if 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, encoding="latin-1")

784是mnist数据集每个样本的像素点个数

from matplotlib import pyplot
import numpy as nppyplot.imshow(x_train[0].reshape((28, 28)), cmap="gray")
print(x_train.shape)

在这里插入图片描述
全连接神经网络的结构
在这里插入图片描述在这里插入图片描述注意数据需转换成tensor才能参与后续建模训练

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())

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) + bias
bs = 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) 
bs = 64
bias = torch.zeros(10, requires_grad=True)print(loss_func(model(xb), yb))

创建一个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)self.dropout = nn.Dropout(0.5)#前向传播自己定义,反向传播是自动进行的def forward(self, x):x = F.relu(self.hidden1(x))x = self.dropout(x)x = F.relu(self.hidden2(x))x = self.dropout(x)#x = F.relu(self.hidden3(x))x = self.out(x)return x

在这里插入图片描述

net = Mnist_NN()
print(net)

在这里插入图片描述
可以打印我们定义好名字里的权重和偏置项

for name,parameter in net.named_parameters():print(name, parameter,parameter.size())

在这里插入图片描述

使用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_size=bs, shuffle=True)valid_ds = TensorDataset(x_valid, y_valid)
valid_dl = DataLoader(valid_ds, batch_size=bs * 2)
def get_data(train_ds, valid_ds, bs):return (DataLoader(train_ds, batch_size=bs, shuffle=True),DataLoader(valid_ds, batch_size=bs * 2),)
  • 一般在训练模型时加上model.train(),这样会正常使用Batch Normalization和 Dropout
  • 测试的时候一般选择model.eval(),这样就不会使用Batch Normalization和 Dropout
import numpy as npdef 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))

zip的用法

a = [1,2,3]
b = [4,5,6]
zipped = zip(a,b)
print(list(zipped))
a2,b2 = zip(*zip(a,b))
print(a2)
print(b2)
from torch import optim
def get_model():model = Mnist_NN()return model, optim.SGD(model.parameters(), lr=0.001)
def loss_batch(model, loss_func, xb, yb, opt=None):loss = loss_func(model(xb), yb)if opt is not None:loss.backward()opt.step()opt.zero_grad()return loss.item(), len(xb)

三行搞定!

train_dl,valid_dl = get_data(train_ds, valid_ds, bs)
model, opt = get_model()
fit(100, model, loss_func, opt, train_dl, valid_dl)

在这里插入图片描述

correct = 0
total = 0
for xb,yb in valid_dl:outputs = model(xb)_,predicted = torch.max(outputs.data,1)total += yb.size(0)correct += (predicted == yb).sum().item()
print(f"Accuracy of the network the 10000 test imgaes {100*correct/total}")

![在这里插入图片描述](https://img-blog.csdnimg.cn/89e5e749b680426c9700aac9f93bf76a.png

后期有兴趣的小伙伴们可以比较SGD和Adam两种优化器,哪个效果更好一点

-SGD 20epoch 85%
-Adam 20epoch 85%

http://www.15wanjia.com/news/173711.html

相关文章:

  • 陕西天工建设有限公司官方网站wordpress 分页数
  • 建设银行手机官方网站下载手机网站制作平台免费
  • 学校网站设计图片秀山网站建设端午节手抄报获奖
  • 自己做国外网站买衣服涿州网站制作
  • 餐饮公司网站建设策划书网络营销是什么大类
  • 兰州网站建设尚美如何在网上挣钱
  • 设计教程网站phpcms做汽车网站
  • 工装效果图网站百度关键词工具
  • 青海西宁网站建设免费logo设计生成器下载
  • 静态网站制作流程新手seo要学多久
  • 团队建设 深度好文分享的网站ps抠图教程
  • 成都新线加做网站高端
  • wordpress返回页头seochan是什么意思
  • 国外教育网站模板黄页88官网
  • 慈溪开发小学网站建设信誉好的高密网站建设
  • 大连cms建站模板濮阳网站建设哪家好
  • 手机有些网站打不开怎么解决整合营销的特点
  • 做分类信息网站代码网站费用标准
  • 企业建网站哪家好怎么建设一个属于自己的网站
  • 网站优化和提升网站排名怎么做网站设计思路
  • 建设网站的市场环境怎么样杭州最新消息
  • 做网站的成本费用网站建设采购项目
  • 德惠网站建设益阳网站设计公司
  • 登陆江西建设厅三类人员的网站浅谈wordpress接入熊掌号
  • 天津网站推广¥做下拉去118cr网络工程专业毕业设计
  • 建立网站的关键是定位技智网站建设小编
  • 找效果图去哪个网站个人如何申请开公司
  • 自己怎么建个网站赚钱上海企业管理咨询
  • 做网站公司东莞顺德网站建设价格
  • 成都建站价格网络舆情