您现在的位置是:热点 >>正文
Pytorch中的NN模块并实现第一个神经网络模型
热点912753人已围观
简介...
在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], &nbtosp; [-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], &nrcbsp; [-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.h中8439]], 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], &n的Nbsp; [ 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(teN模st_example) 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,&块并nbsp; 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.000实现0], [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 =&第一nbsp;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)&个神nbsp;
虽然上面只是用了一个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 = torc经网h.tensor([[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.MSE络模Loss() # 构造优化器 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) &nbPysp; # 梯度清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:
相关文章
微软将以197亿美元收购语音识别企业纽安斯
热点来源:经济参考报 原标题:微软将以197亿美元收购语音识别企业纽安斯 据路透社报道,当地时间4月12日,美国科技公司微软和人工智能及语音技术公司纽安斯共同发布公告称,双方已达成最终协议,微软将以每股5...
【热点】
阅读更多云计算核心技术Docker教程:Docker Swarm 模式管理将节点作为管理节点加入
热点当您第一次创建集群时,您将单个 Docker 引擎置于集群模式中。要充分利用群模式,您可以向群中添加节点: 添加工作节点会增加容量。当您将服务部署到 swarm 时,引擎会在可用节点上调度任务,无论它...
【热点】
阅读更多Google BigQuery是大数据分析的未来吗?
热点考虑到Google BigQuery提高效率以及轻松存储大量信息的能力,它可能是大数据分析的未来方向。 如果企业未能实施正确的业务管理工具,那么在经营业务方面可能会很棘手。如果企业与数以千计的客户打交...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://blvdseafood.com/?s=谷歌自动批量发帖软件【tg:seogoogle987】oEz
- https://www.soccerbible.com/search?q=谷歌自动批量发帖软件【tg:seogoogle987】,zKK
- https://www.reallywildbirdfood.co.uk/store/search?q=谷歌自动批量发帖软件【tg:seogoogle987】,p7e
- https://www.nbm.org/?action=QUERY&s=谷歌自动批量发帖软件【tg:seogoogle987】,9Vc
- https://www.lenovo.com/gb/en/search?action=SEARCH&text=谷歌自动批量发帖软件【tg:seogoogle987】,KcU
- https://www.rookwinkel.nl/catalogsearch/result/index/?q=谷歌自动批量发帖软件【tg:seogoogle987】,AD0
- https://search.danawa.com/dsearch.php?query=谷歌自动批量发帖软件【tg:seogoogle987】PlS
- https://www.kores.com/?s=谷歌自动批量发帖软件【tg:seogoogle987】,SmY
- https://www.zutari.com/?s=谷歌自动批量发帖软件【tg:seogoogle987】,z4t
- https://www.teamlab.art/es/?submit=谷歌自动批量发帖软件【tg:seogoogle987】,ot4
- https://www.military-history.org/?s=谷歌自动批量发帖软件【tg:seogoogle987】,l23
- https://www.remit.co.jp/vi/?s=谷歌自动批量发帖软件【tg:seogoogle987】a2m
- https://www.remit.co.jp/zh/?s=谷歌自动批量发帖软件【tg:seogoogle987】,9uS
- https://www.qidulp.com/search?query=谷歌自动批量发帖软件【tg:seogoogle987】,FbP
- https://www.pearlstonecenter.org/search/谷歌自动批量发帖软件【tg:seogoogle987】,9qv
- https://www.trockenmittel.ch/catalogsearch/result/?iC=ZM3&q=谷歌自动批量发帖软件【tg:seogoogle987】,ueK
- https://hsmoa.com/search?query=谷歌自动批量发帖软件【tg:seogoogle987】dgu
- https://www.remit.co.jp/id/?s=谷歌自动批量发帖软件【tg:seogoogle987】,CTX
- https://www.kaiyueleather.com/search/谷歌自动批量发帖软件【tg:seogoogle987】,kNT
- https://www.l-and-b.jp/?page=1&s=谷歌自动批量发帖软件【tg:seogoogle987】,mhY
- https://yscc1986.net/search/谷歌自动批量发帖软件【tg:seogoogle987】TnM
- https://www.safetv.go.kr/base/search/view?searchWord=谷歌自动批量发帖软件【tg:seogoogle987】5D7
- https://www.wou.edu.my/?s=谷歌自动批量发帖软件【tg:seogoogle987】,4Ex
- https://www.posten-portal.de/search?query=谷歌自动批量发帖软件【tg:seogoogle987】,A3F
- https://www.tarleton.edu/?s=谷歌自动批量发帖软件【tg:seogoogle987】,f6z
- https://yokohama-oht.com/?action=CHECK&s=谷歌自动批量发帖软件【tg:seogoogle987】,s5X
- https://www.temu.com/search_result.html?search_key=谷歌自动批量发帖软件【tg:seogoogle987】nrF
- https://www.symatoys.com/search.html?ID=UyMD6aO58bPrJvQAB1I0N69KzJBZeNPC&keyword=谷歌自动批量发帖软件【tg:seogoogle987】,ruN
- https://www.goodreads.com/quotes/tag/谷歌自动批量发帖软件【tg:seogoogle987】iBr
- https://www.realsporting.com/buscador?buscar=谷歌自动批量发帖软件【tg:seogoogle987】,ge6
- https://www.kelsa.co.uk/catalogsearch/result/index/?T=1686132119291&q=谷歌自动批量发帖软件【tg:seogoogle987】,Z9O
- https://www.duksung.ac.kr/search/front/Search.jsp?qt=谷歌自动批量发帖软件【tg:seogoogle987】br2
- https://www.silentiumpc.com/en/search/谷歌自动批量发帖软件【tg:seogoogle987】,ySl
- https://xn--lgernevedlystskoven-lxb.dk/search/谷歌自动批量发帖软件【tg:seogoogle987】,Jy9
- https://www.wilsonart.com/catalogsearch/result/?action=CHECK&q=谷歌自动批量发帖软件【tg:seogoogle987】,D0t
- https://www.navylookout.com/?ID=nSBR6Ivdu6lZg1yr2SDs30Lo5sTs76yJ&s=谷歌自动批量发帖软件【tg:seogoogle987】,ruf
- https://www.shacknews.com/editorial-search?q=谷歌自动批量发帖软件【tg:seogoogle987】,6BY
- https://www.sic-info.org/?s=谷歌自动批量发帖软件【tg:seogoogle987】,VMi
- https://m.shoppinghow.kakao.com/m/search/q/谷歌自动批量发帖软件【tg:seogoogle987】i3Z
- https://www.tahiti-infos.com/search/谷歌自动批量发帖软件【tg:seogoogle987】,7Sk
- https://www.mesec.cz/vyhledavani/?qs=谷歌自动批量发帖软件【tg:seogoogle987】,Zs2
- https://www.kingston.gov.uk/site-search/results/?q=谷歌自动批量发帖软件【tg:seogoogle987】,2iT
- https://www.whiterock2008.com.tw/search?q=谷歌自动批量发帖软件【tg:seogoogle987】,oDn
- https://www.le-trend.com/en/search/=谷歌自动批量发帖软件【tg:seogoogle987】,r6s
- https://jihoon471.cafe24.com/product/search.html?banner_action=&keyword=谷歌自动批量发帖软件【tg:seogoogle987】R3Y
- https://www.wattpad.com/search/谷歌自动批量发帖软件【tg:seogoogle987】iog
- https://www.tullygroup.us/?ID=hbkq8jFjrQlVRrwzEmvoUJJAY4e1Lfo5&s=谷歌自动批量发帖软件【tg:seogoogle987】,6Vp
- https://www.lyrics.com/lyrics/kkk3谷歌自动批量发帖软件【tg:seogoogle987】,UyN
- https://www.kubb-tool.bfs.admin.ch/en/search?action=CHECK&q=谷歌自动批量发帖软件【tg:seogoogle987】,J58
- https://www.yli.ro/catalogsearch/result/?q=谷歌自动批量发帖软件【tg:seogoogle987】,u4z