用pytorch搭建AlexNet

2018年10月6日10:49:42用pytorch搭建AlexNet已关闭评论 310

编译环境为Jupyter,代码如下:

  1. #AlexNet 搭建
  2. import torch
  3. import torch.nn as nn
  4. class AlexNet(nn.Module):
  5.     def __init__(self):
  6.         super(AlexNet,self).__init__()
  7.         self.features=nn.Sequential(
  8.             nn.Conv2d(3,96,11,4,0),
  9.             nn.ReLU(True),
  10.             nn.MaxPool2d(3,2),
  11.             nn.Conv2d(96,256,5,1,2),
  12.             nn.ReLU(True),
  13.             nn.MaxPool2d(3,2),
  14.             nn.Conv2d(256,384,3,1,1),
  15.             nn.ReLU(True),
  16.             nn.Conv2d(384,384,3,1,1),
  17.             nn.ReLU(True),
  18.             nn.Conv2d(384,256,3,1,1),
  19.             nn.ReLU(True),
  20.             nn.MaxPool2d(3,2)
  21.         )
  22.         self.classifier=nn.Sequential(
  23.             nn.Linear(9216,4096),
  24.             nn.ReLU(True),
  25.             nn.Dropout(0.5),
  26.             nn.Linear(4096,4096),
  27.             nn.ReLU(True),
  28.             nn.Dropout(0.5),
  29.             nn.Linear(4096,1000)
  30.         )
  31.     def forward(self,x):
  32.         x=self.features(x)
  33.         x=x.view(x.size(0),-1)
  34.         x=self.classifier(x)
  35.         return x
  36. alexnet=AlexNet()
  37. print(alexnet)

运行后网络结构:
用pytorch搭建AlexNet

历史上的今天: