当前位置:网站首页>神经网络的基本骨架-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)
代码运行结果:
边栏推荐
- VIM简易使用教程
- 离线安装wireshark2.6.10
- Pytest automated testing - compare robotframework framework
- [human version] Web3 privacy game in the dark forest
- [2020 overview] overview of link prediction based on knowledge map embedding
- [pat (basic level) practice] - [simple simulation] 1064 friends
- Web server: how to choose a good web server these five aspects should be paid attention to
- 2022年煤气考试题库及在线模拟考试
- Some small knowledge points
- OdeInt与GPU
猜你喜欢
![Ospfb notes - five messages [ultra detailed] [Hello message, DD message, LSR message, LSU message, lsack message]](/img/aa/a255d225d71e6ba2b497f8d59f5f11.jpg)
Ospfb notes - five messages [ultra detailed] [Hello message, DD message, LSR message, LSU message, lsack message]

Obtain detailed ideas for ABCDEF questions of 2022 American Games

软件研发的十大浪费:研发效能的另一面

Tencent has five years of testing experience. It came to the interview to ask for 30K, and saw the so-called software testing ceiling

Internet winter, how to spend three months to make a comeback
![[recommended algorithm] C interview question of a small factory](/img/ae/9c83efe86c03763710ba5e4a2eea33.jpg)
[recommended algorithm] C interview question of a small factory

Daily question - line 10

神经网络-使用Sequential搭建神经网络

【硬十宝典】——2.【基础知识】开关电源各种拓扑结构的特点
![[leetcode skimming] February summary (updating)](/img/62/0d0d9f11434e49d33754a2e4f2ea65.jpg)
[leetcode skimming] February summary (updating)
随机推荐
slf4j 简单实现
Internet winter, how to spend three months to make a comeback
Talk about testdeploy
Tencent has five years of testing experience. It came to the interview to ask for 30K, and saw the so-called software testing ceiling
Maixll-Dock 快速上手
1. Mobile terminal touch screen event
Codeworks round 449 (Div. 1) C. Kodori tree template
2022-02-15 (399. Division evaluation)
2022 t elevator repair question bank and simulation test
【硬十宝典目录】——转载自“硬件十万个为什么”(持续更新中~~)
AssertionError assert I.ndim == 4 and I.shape[1] == 3
Codeforces Round #721 (Div. 2)B1. Palindrome Game (easy version)B2. Palindrome game (hard version)
Execution failed for task ‘:app:processDebugResources‘. > A failure occurred while executing com. and
Dual Contrastive Learning: Text Classification via Label-Aware Data Augmentation 阅读笔记
Advanced application of ES6 modular and asynchronous programming
How do I sort a list of strings in dart- How can I sort a list of strings in Dart?
pytorch神经网络搭建 模板
Daily question - line 10
尺取法:有效三角形的个数
RuntimeError: mean(): input dtype should be either floating point or complex dtypes.Got Long instead