当前位置:网站首页>神经网络的基本骨架-nn.Moudle的使用
神经网络的基本骨架-nn.Moudle的使用
2022-07-01 04:35:00 【booze-J】
文章
Module是Contains中最常用的模块,Contains是用来构建神经网络架构的。
Contains官方文档
神经网络的基本骨架-nn.Moudle的使用官方文档
根据官方文档的示例:
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(1, 20, 5)
self.conv2 = nn.Conv2d(20, 20, 5)
def forward(self, x):
x = F.relu(self.conv1(x))
return F.relu(self.conv2(x))
步骤:使用神经网络的基本骨架-nn.Moudle,主要可以分为三步吧。
- 创建一个类继承nn.Module
- 继承nn.Module的初始化加上自己的初始化
- 重写forword方法
对官方文档这部分代码的解释:
def forward(self, x):
x = F.relu(self.conv1(x))
return F.relu(self.conv2(x))

看图,很形象的。
nn.Moudle使用示例:
import torch
from torch import nn
# 创建一个类继承nn.Module
class Tudui(nn.Module):
# 继承nn.Module的初始化
def __init__(self):
super().__init__()
# 重写forword方法
def forward(self,input):
output = input+1
return output
# 创建一个类对象
obj = Tudui()
x = torch.tensor(1.0)
# 往类对象中传入参数并接收结果
output = obj(x)
# 输出结果
print(output)
代码运行结果:
边栏推荐
- 2022 gas examination question bank and online simulation examination
- Annual inventory review of Alibaba cloud's observable practices in 2021
- [difficult] sqlserver2008r2, can you recover only some files when recovering the database?
- Extension fragment
- [2020 overview] overview of link prediction based on knowledge map embedding
- [human version] Web3 privacy game in the dark forest
- 尺取法:有效三角形的个数
- Task04 | statistiques mathématiques
- 2022年T电梯修理题库及模拟考试
- Common interview questions ①
猜你喜欢

Ten wastes of software research and development: the other side of research and development efficiency

Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor

Basic usage, principle and details of session

2022 Shanghai safety officer C certificate examination question simulation examination question bank and answers

Odeint et GPU

Applications and features of VR online exhibition

About the transmission pipeline of stage in spark

Simple implementation of slf4j

(12) Somersault cloud case (navigation bar highlights follow)

OdeInt与GPU
随机推荐
为什么香港服务器最适合海外建站使用
Kodori tree board
【深度学习】(4) Transformer 中的 Decoder 机制,附Pytorch完整代码
JVM栈和堆简介
Question bank and online simulation examination for special operation certificate of G1 industrial boiler stoker in 2022
Codeforces Round #771 (Div. 2) ABCD|E
细数软件研发效能的七宗罪
TASK04|數理統計
什么是权限?什么是角色?什么是用户?
I also gave you the MySQL interview questions of Boda factory. If you need to come in and take your own
测量三相永磁同步电机的交轴直轴电感
Rule method: number of effective triangles
总结全了,低代码还需要解决这4点问题
Difference between cookie and session
【LeetCode】100. Same tree
Cmake selecting compilers and setting compiler options
Concurrent mode of different performance testing tools
使用WinMTR软件简单分析跟踪检测网络路由情况
Registration of P cylinder filling examination in 2022 and analysis of P cylinder filling
2022 question bank and answers for safety production management personnel of hazardous chemical production units