nn-neural network
module的使用
import torch.nn as nn
import torch.nn.functional as F
class 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
x = F.relu(self.conv1(x))
#经过一次卷积(conv1)
#经过一次非线性(relu)
return F.relu(self.conv2(x))
#再经过一次卷积
#再经过一次非线性
#输出
forward 主要定义了我们的一个计算,应该再所有子类中进行一个重写
import torch
from torch import nn
#先搭建自己的一个神经网络
class Wangluo(nn.Module):#继承pytorch.nn给我们提供的一个module
#重写
def __init__(self):
super().__init__()
#自己的
def forward(self, input):
output = input + 1
return output
wangluo = Wangluo()
x = torch.tensor(3,0)
output = wangluo(x)
print(output)