您现在的位置是:热点 >>正文
Pytorch中的NN模块并实现第一个神经网络模型
热点5086人已围观
简介...
在PyTorch建立模型,主要是NN模块。
nn.Linear
nn.Linear是创建一个线性层。这里需要将输入和输出维度作为参数传递。
linear = nn.Linear(10, 2) example_input = torch.randn(3, 10) example_output = linear(example_input) example_output
上面代码linear接受nx10的输入并返回nx2的输出。
print(example_input) print(example_output) tensor([[ 1.1122, -0.1381, 0.5547, -0.3326, Py-0.5676, 0.2810, -0.5521, -0.8729, -0.6627, 0.8729], [ 1.9134, 0.2397, -0.8340, 1.1532, -1.6725, 0.6171, -0.0357, -1.6848, -0.8454, 0.3876], &ntobsp; [-0.0786, -0.1541, -0.8385, -0.1587, -0.0121, 1.4457, -0.0132, 1.5653, -1.6954, -0.9350]]) # 输出如下 tensor([[-0.1249, -0.8002], [-1.0945, -0.2297], rc [-0.3558, 0.8439]], grad_fn=) nn.Relu
nn.Relu对线性的给定输出执行 relu 激活函数操作。
relu = nn.ReLU() relu_output = relu(example_output) relu_output # 输出如下 tensor([[0.0000, 0.0000], [0.0000, 0.0000], [0.0000, 0.843h中9]], grad_fn=) nn.BatchNorm1d
nn.BatchNorm1d是一种标准化技术,用于在不同批次的输入中保持一致的均值和标准偏差。
batchnorm = nn.BatchNorm1d(2) batchnorm_output = batchnorm(relu_output) batchnorm_output # 输出如下 tensor([[ 0.0000, -0.7071], [ 0.0000, -0.7071], &nb的Nsp;[ 0.0000, 1.4142]], grad_fn=) nn.Sequential
nn.Sequential一次性创建一系列操作。和tensorflow中的Sequential完全一样。
mlp_layer = nn.Sequential( nn.Linear(5, 2), nn.BatchNorm1d(2), nn.ReLU() ) test_example = torch.randn(5,5) + 1 print("input: ") print(test_exampN模le) print("output: ") print(mlp_layer(test_example)) # 输出如下 input: tensor([[ 1.4617, 1.2446, 1.4919, 1.5978, -0.3410], [-0.2819, 0.5567, 1.0113, 1.8053, -0.0833], [ 0.2830, 块并 1.0857, 1.2258, 2.6602, 0.1339], [ 0.8682, 0.9344, 1.3715, 0.0279, 1.8011], [ 0.6172, 1.1414, 0.6030, 0.3876, 1.3653]]) output: tensor([[0.0000, 0.0000],&n实现bsp; [0.0000, 1.3722], [0.0000, 0.8861], [1.0895, 0.0000], [1.3047, 0.0000]], grad_fn=)
在上面的模型中缺少了优化器,我们无法得到对应损失。
import torch.optim as optim adam_opt = 第一;optim.Adam(mlp_layer.parameters(), lr=1e-1) # 这里lr表示学习率,1e-1表示0.1 train_example = torch.randn(100,5) + 1 adam_opt.zero_grad() # 我们将使用1减去平均值,作为简单损失函数 cur_loss = torch.abs(1 - mlp_layer(train_example)).mean() cur_loss.backward() # 更新参数 adam_opt.step() print(cur_loss.data) # 输出如下 tensor(0.7467)
虽然个神上面只是用了一个epoch,训练线性模型得到loss为0.7467,上面就是NN模型建立model的整个流程,
第一个神经网络模型
下面实现第一个分类神经网络,其中一个隐藏层用于开发单个输出单元。
首先,使用以下命令导入 PyTorch 库 -
import torch import torch.nn as nn
定义所有层和批量大小以开始执行神经网络,如下所示 -
n_in, n_h, n_out, batch_size = 10, 5, 1, 10
由于神经网络包括输入数据的组合以获得相应的输出数据,我们将遵循以下相同的程序 -
x = torch.randn(batch_size, n_in) y = torch.tens经网or([[1.0], [0.0], [0.0], [1.0], [1.0], [1.0], [0.0], [0.0], [1.0], [1.0]])
创建顺序模型。使用下面代码,创建一个顺序模型 -
model = nn.Sequential(nn.Linear(n_in, n_h), nn.ReLU(), nn.Linear(n_h, n_out), nn.Sigmoid())
借助梯度下降优化器构建损失函数,如下所示 -
# 构造损失函数 criterion = torch.nn.MSELoss()&n络模bsp;# 构造优化器 optimizer = torch.optim.SGD(model.parameters(), lr = 0.01)
使用给定代码行的迭代循环实现梯度下降模型 -
# 梯度下降 for epoch in range(50): # 正向传递:通过将x传递给模型来计算预测的y y_pred = model(x) # 计算loss loss = criterion(y_pred, y) Py # 梯度清0 optimizer.zero_grad() # 反向传播,求解梯度 loss.backward() # 更新模型参数 optimizer.step() if epoch % 10 == 0: print('epoch: ', epoch,' loss: ', loss.item()) to;
输出如下
epoch: 0 loss: 0.2508794665336609 epoch: 10 loss: 0.24847669899463654 epoch: 20 loss: 0.24615907669067383 epoch: 30 loss: 0.24392127990722656 epoch: 40 loss: 0.24175791442394257
Tags:
相关文章
谷歌 Gemini 新功能上线:一键生成 PPT,轻松搞定演示文稿!
热点繁琐的演示文稿制作时代或将终结。谷歌近日为其 AI 助手 Gemini 推出一项极具实用价值的新功能:只需输入一句话提示,即可自动生成结构完整、视觉美观的专业 PPT。这项能力已集成至 Gemini...
【热点】
阅读更多标签: 马斯克 SpaceX 粉笔科技 张小龙
热点Hello,大家早上好,又是元气满满的一天,先来浏览新鲜的早报吧~ 【马斯克有望成为首个万亿美元富豪】 SpaceX 即将于下周上市,若按 1.77 万亿美元估值,马斯克所持股权价值将达 8665 亿...
【热点】
阅读更多Facebook旗下数字货币项目Diem放弃申请瑞士支付牌照:专注美国市场
热点新浪科技讯 北京时间5月13日早间消息,据报道,数字加密货币集团Diem Association,即原先的Facebook旗下的“天秤币”(Libra)项目,周三宣布推出一种美元稳定币。从原先的放眼全...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://www.lauben.com/de/?s=谷歌霸屏业务【tg:seogoogle987】,ZKk
- https://www.wikiart.org/pt/Search/谷歌霸屏业务【tg:seogoogle987】,oem
- https://www.abbreviations.com/谷歌霸屏业务【tg:seogoogle987】6wr
- https://www.kissthekangaroo.com.au/index.php?route=product/search&tag=谷歌霸屏业务【tg:seogoogle987】,e9t
- https://www.totolink.vn/search/谷歌霸屏业务【tg:seogoogle987】,IEF
- https://www.merchbar.com/search?q=谷歌霸屏业务【tg:seogoogle987】,tag
- https://www.zutari.com/search/谷歌霸屏业务【tg:seogoogle987】,gMF
- https://www.theaerogroup.net/?cat=28&s=谷歌霸屏业务【tg:seogoogle987】,JE8
- https://www.paxnet.co.kr/search/jongmok?kwd=谷歌霸屏业务【tg:seogoogle987】THe
- https://www.roberthalf.com/us/en/jobs/all/谷歌霸屏业务【tg:seogoogle987】9Nq
- https://www.southmeadows.co.uk/2019/07/?s=谷歌霸屏业务【tg:seogoogle987】,Ncu
- https://www.therapeuticassociates.com/?cat=5&s=谷歌霸屏业务【tg:seogoogle987】,7B4
- https://www.lindseymedicalsupply.com/search/谷歌霸屏业务【tg:seogoogle987】,zaF
- https://www.paxnet.co.kr/search/jongmok?kwd=谷歌霸屏业务【tg:seogoogle987】kTD
- https://yokohama-oht.com/?action=CHECK&s=谷歌霸屏业务【tg:seogoogle987】,tGq
- https://www.youtube.com/results?search_query=谷歌霸屏业务【tg:seogoogle987】,1XR
- https://www.woorden.org/spreekwoord.php?woord=谷歌霸屏业务【tg:seogoogle987】,fYg
- https://www.vocabulary.com/dictionary/谷歌霸屏业务【tg:seogoogle987】,voK
- https://www.teamlab.art/?submit=谷歌霸屏业务【tg:seogoogle987】,tP1
- https://www.ntv.co.jp/3min/search/?action=QUERY&q=谷歌霸屏业务【tg:seogoogle987】,RCB
- https://www.realsporting.com/buscador?buscar=谷歌霸屏业务【tg:seogoogle987】,2aj
- https://www.wilsonart.com/catalogsearch/result/?action=CHECK&q=谷歌霸屏业务【tg:seogoogle987】,ARa
- https://www.paulaschoice.hk/zh_hant/catalogsearch/result/?q=谷歌霸屏业务【tg:seogoogle987】,mJf
- https://www.writingforward.com/?action=SEARCH&s=谷歌霸屏业务【tg:seogoogle987】,z5C
- https://www.shacknews.com/editorial-search?q=谷歌霸屏业务【tg:seogoogle987】,fYK
- https://www.metaltix.com/?view=tours&filter=search&q=谷歌霸屏业务【tg:seogoogle987】,ojy
- https://www.oikos-tecnics.com/?s=谷歌霸屏业务【tg:seogoogle987】,sFZ
- https://m.shoppinghow.kakao.com/m/search/q/谷歌霸屏业务【tg:seogoogle987】Jao
- https://www.silentiumpc.com/en/search/谷歌霸屏业务【tg:seogoogle987】,PVL
- https://www.usslexington.com/?action=QUERY&s=谷歌霸屏业务【tg:seogoogle987】,ash
- https://www.rea.govt.nz/search?searchterm=谷歌霸屏业务【tg:seogoogle987】,4DA
- https://www.l-and-b.jp/?page=1&s=谷歌霸屏业务【tg:seogoogle987】,N7J
- https://www.pmindia.gov.in/en/?s=谷歌霸屏业务【tg:seogoogle987】,Fhi
- https://www.remit.co.jp/vi/?s=谷歌霸屏业务【tg:seogoogle987】,9sK
- https://www.ushmm.org/search/results/?ID=8s0587Oi8UTv74zPPM4fuhwjNpe2hsEE&q=谷歌霸屏业务【tg:seogoogle987】,Ef6
- https://www.ushmm.org/search/results.php?q=谷歌霸屏业务【tg:seogoogle987】,Cs3
- https://www.roberthalf.com/us/en/jobs/all/谷歌霸屏业务【tg:seogoogle987】Wd3
- https://www.target.com/s?searchTerm=谷歌霸屏业务【tg:seogoogle987】,umr
- https://www.lee.com/search?q=谷歌霸屏业务【tg:seogoogle987】,oBa
- https://www.seagrant.wisc.edu/?page=63&s=谷歌霸屏业务【tg:seogoogle987】,ly2
- https://www.teamlab.art/zh-hans/?submit=谷歌霸屏业务【tg:seogoogle987】,0in
- https://www.impactpool.org/search/?type=79&q=谷歌霸屏业务【tg:seogoogle987】8pL
- https://www.pearson.com/en-us/search.html?aq=谷歌霸屏业务【tg:seogoogle987】,2L1
- https://www.yli.ro/catalogsearch/result/?q=谷歌霸屏业务【tg:seogoogle987】,rkG
- https://www.revere.org/search?action=SEARCH&q=谷歌霸屏业务【tg:seogoogle987】,leK
- https://www.londontheatredirect.com/search/谷歌霸屏业务【tg:seogoogle987】,d2t
- https://www.sbaad.no/?s=谷歌霸屏业务【tg:seogoogle987】,IFt
- https://www.temu.com/search_result.html?search_key=谷歌霸屏业务【tg:seogoogle987】PG5
- https://www.valuecolleges.com/?action=QUERY&s=谷歌霸屏业务【tg:seogoogle987】,e78
- https://www.teamlab.art/pt/?submit=谷歌霸屏业务【tg:seogoogle987】,onw