本文主要是介绍神经网络学习1—nn.Module,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
nn.module 为所有神经网络提供了一个模板
import torch.nn as nn
import torch.nn.functional as Fclass Model(nn.Module):def __init__(self):super(Model, self).__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))
forward即前向传播
relu(x)是分段函数0,x<0 ; 1,x>=0,有点类似于深度学习中的感知机
这个x先经过一次卷积,再经过以此非线性,再经过一次卷积,再经过一次非线性,最后才能得出输出
卷积就是提取特征,上面的conv1和2就是卷积操作
alt+insert可以自动补全子类
import torch
from torch import nn
class Tudui(nn.Module):def __init__(self) -> None:#-> None 并不会改变函数的实际行为,它仅仅是一个提示,表明该函数不应该返回值super().__init__()def forward(self,input):output=input+1return outputtudui=Tudui()
x=torch.tensor(1.0)
output=tudui(x)
print(output)
这篇关于神经网络学习1—nn.Module的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!