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

网站项目策划大纲无锡网页网站制作公司

网站项目策划大纲,无锡网页网站制作公司,电子商城网站开发软件,亲水平台设计本文通过多个示例演示如何使用PyTorch构建不同类型的神经网络模型,涵盖基础多层感知机、自定义块、顺序块以及复杂组合模型。所有代码均附带输出结果,帮助读者直观理解模型结构。 1. 多层感知机(MLP) 使用nn.Sequential快速构建一…

本文通过多个示例演示如何使用PyTorch构建不同类型的神经网络模型,涵盖基础多层感知机、自定义块、顺序块以及复杂组合模型。所有代码均附带输出结果,帮助读者直观理解模型结构。


1. 多层感知机(MLP)

使用nn.Sequential快速构建一个包含隐藏层和ReLU激活函数的简单MLP。

import torch
from torch import nn
from torch.nn import functional as F# 定义模型
net = nn.Sequential(nn.Linear(in_features=20, out_features=256),nn.ReLU(),nn.Linear(in_features=256, out_features=10)
)# 随机输入(2个样本,每个样本20维)
x = torch.rand(2, 20)
output = net(x)
print(output)

输出结果

tensor([[ 0.0424, -0.0431,  0.0191, -0.0467, -0.1238,  0.0123,  0.0224, -0.0914,-0.0271, -0.0883],[ 0.1497,  0.0056,  0.1736, -0.0222, -0.1749,  0.0234,  0.1242, -0.1502,-0.0490, -0.1498]], grad_fn=<AddmmBackward0>)

2. 自定义块(Custom Block)

通过继承nn.Module实现自定义模型,灵活定义前向传播逻辑。

class Model1(nn.Module):def __init__(self):super().__init__()self.hidden = nn.Linear(20, 256)  # 隐藏层self.out = nn.Linear(256, 10)     # 输出层def forward(self, x):return self.out(F.relu(self.hidden(x)))# 实例化并推理
net = Model1()
output = net(x)
print(output)

输出结果

tensor([[-0.0344,  0.0446,  0.1053,  0.0658,  0.2332, -0.0105,  0.1963,  0.0181,0.1822, -0.1304],[-0.1953, -0.0464,  0.1120,  0.0082,  0.1906,  0.0503,  0.2968,  0.0132,0.2769, -0.1390]], grad_fn=<AddmmBackward0>)

3. 顺序块(Sequential Block)

nn.Sequential封装在自定义类中,简化模型定义。

class Model2(nn.Module):def __init__(self):super().__init__()self.se = nn.Sequential(nn.Linear(20, 256),nn.ReLU(),nn.Linear(256, 10))def forward(self, x):return self.se(x)# 实例化并推理
net = Model2()
output = net(x)
print(output)

输出结果

tensor([[ 0.2166, -0.0262, -0.0240, -0.0165,  0.0695, -0.2495,  0.0699, -0.2297,0.0436, -0.0792],[ 0.2417,  0.0458, -0.0206,  0.0546,  0.0468, -0.3599,  0.1273, -0.2373,0.0020, -0.1880]], grad_fn=<AddmmBackward0>)

4. 动态操作的正向传播

在正向传播中执行矩阵运算和条件判断,展示灵活的自定义逻辑。

class Model3(nn.Module):def __init__(self):super().__init__()self.rand_weight = torch.rand((20, 20), requires_grad=False)  # 固定权重self.linear = nn.Linear(20, 20)def forward(self, x):x = self.linear(x)x = F.relu(torch.mm(x, self.rand_weight) + 1x = self.linear(x)while x.abs().sum() > 1:  # 动态调整张量大小x /= 2return x.sum()# 实例化并推理
net = Model3()
output = net(x)
print(output)

输出结果

tensor(-0.1288, grad_fn=<SumBackward0>)

5. 混合组合模型

通过组合不同块构建复杂模型,实现层次化设计。

class Model4(nn.Module):def __init__(self):super().__init__()self.se = nn.Sequential(nn.Linear(20, 64), nn.ReLU(),nn.Linear(64, 32), nn.ReLU())self.linear = nn.Linear(32, 16)def forward(self, x):return self.linear(self.se(x))# 组合多个模型
net = nn.Sequential(Model4(),nn.Linear(16, 20),Model2()
)# 推理
output = net(x)
print(output)

输出结果

tensor([[ 0.0220,  0.0221, -0.0445,  0.0760, -0.0317,  0.1331,  0.0716, -0.0102,0.0294, -0.0422],[ 0.0170,  0.0196, -0.0564,  0.0732, -0.0360,  0.1253,  0.0783, -0.0079,0.0283, -0.0448]], grad_fn=<AddmmBackward0>)

总结

  • nn.Sequential:适合快速堆叠层,适用于简单模型。

  • 自定义类:通过继承nn.Module实现更灵活的前向传播逻辑。

  • 动态操作:可在正向传播中嵌入矩阵运算、循环等复杂操作。

  • 组合模型:通过混合不同块构建复杂网络,提升代码复用性。

完整代码已通过测试,建议结合实际任务调整模型结构和参数。欢迎在评论区讨论更多PyTorch技巧!


希望这篇文章能帮助你掌握PyTorch模型构造的核心方法!如果有其他问题,欢迎留言交流。


文章转载自:

http://IipfjOCi.qgmbx.cn
http://kpnpKMOh.qgmbx.cn
http://7JXl6Xvi.qgmbx.cn
http://GezZpkO6.qgmbx.cn
http://oXmVtSWM.qgmbx.cn
http://bMqH4HNW.qgmbx.cn
http://2c3DIcry.qgmbx.cn
http://AZ2Zjxth.qgmbx.cn
http://GuenxCh9.qgmbx.cn
http://lny3iivE.qgmbx.cn
http://tCeuXM07.qgmbx.cn
http://YUqXtA4i.qgmbx.cn
http://FahiHGqY.qgmbx.cn
http://aWdstYzt.qgmbx.cn
http://8ylX5WaS.qgmbx.cn
http://Io9vMNWq.qgmbx.cn
http://Bzqzd3UE.qgmbx.cn
http://A0zmZD9u.qgmbx.cn
http://KL2IjFbP.qgmbx.cn
http://ChKe3e87.qgmbx.cn
http://OJRnEmT5.qgmbx.cn
http://CJFYEbBp.qgmbx.cn
http://yGx9nOjZ.qgmbx.cn
http://Uzg6Gsnw.qgmbx.cn
http://ZLpHLl4x.qgmbx.cn
http://LaS3WNAF.qgmbx.cn
http://Ci49zlqt.qgmbx.cn
http://Hr84WTLM.qgmbx.cn
http://mlwyvf3k.qgmbx.cn
http://lZ4HbJLt.qgmbx.cn
http://www.dtcms.com/wzjs/646155.html

相关文章:

  • 企业网站 源码 开源优秀设计方案网站
  • 专注移动网站建设网站建设价格差异
  • 专做企业网站的石家庄营销型网站建设公司
  • 家装设计软件哪个好用长沙网站seo公司
  • python网站开发实例网站如何横屏
  • 江苏省城乡建设厅网站深圳高端家政服务公司
  • 做网站编辑工作累吗泉州做网站优化多少钱
  • 网站建设作用找做废薄膜网站
  • 大庆工程建设公司网站昆山市网站建设
  • 做购物网站的目的国外网页设计评论网站
  • 网站建设过程总结报告注册一个网站多少钱?
  • 国外有哪些设计网站广州网站开发设计公司
  • 如何建设个人的网站低成本做网站
  • 百怎么做网站电子商务网站建设类型
  • 视频互动网站建设手机p2p网站
  • 网站制作公司小邓怎么套用网站模板
  • 视频类网站建设的成果建设网站的结束语
  • 什么是新闻源网站国内软件公司排名
  • wordpress0商业网站做视频网站盈利模式
  • 公司后台网站怎么做wordpress无法写文章
  • html网站模板免费下载网站对接如何做
  • 计算机技术员网站建设怎么网络销售应该注册什么公司
  • 公司网站建设设计公司网站建设开发费怎么做账
  • 怎么优化一个网站软件定制开发服务收费多少
  • 用v9做网站优化广西建设学院网站
  • 做阀门的网站广州app开发公司排行十强
  • 建网站买什么主机如何优化网络延迟
  • flash企业网站与网站建设相关的论文题目
  • 小韩网站源码百度网盘搜索引擎入口在哪
  • 网站底部素材wordpress顶部菜单哪里设置