from __future__ import print_function
import torch as t
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
class Net(nn.Module):
def __init__(self):#构造函数
#nn.Module子类的函数必须在构造函数中执行父类的构造函数
#下式等价于nn.Module.__init__(self)
super(Net,self).__init__()
#卷积层'1'表示输入图片为单通道,6表示输出通道数
#'5'表示卷积核为5*5
self.conv1=nn.Conv2d(1,6,5)
#卷积层
self.conv2=nn.Conv2d(6,16,5)
#仿射层/全连接层,y=Wx+b
self.fc1=nn.Linear(16*5*5,120)
self.fc2=nn.Linear(120,84)
self.fc3=nn.Linear(84,10)
#定义好forward函数,backward会被自动实现
def forward(self,x):
#卷积->激化->池化
x=F.max_pool2d(F.relu(self.conv1(x)),(2,2))
x=F.max_pool2d(F.relu(self.conv2(x)),2)
#reshape,'-1'表示自适应
x=x.view(x.size()[0],-1)
x=F.relu(self.fc1(x))
x=F.relu(self.fc2(x))
定义神经网络
最新推荐文章于 2023-11-10 22:46:12 发布