PyTorch实现AlexNet示例

yipeiwu_com5年前Python基础

PyTorch: https://github.com/shanglianlm0525/PyTorch-Networks

import torch
import torch.nn as nn
import torchvision

class AlexNet(nn.Module):
  def __init__(self,num_classes=1000):
    super(AlexNet,self).__init__()
    self.feature_extraction = nn.Sequential(
      nn.Conv2d(in_channels=3,out_channels=96,kernel_size=11,stride=4,padding=2,bias=False),
      nn.ReLU(inplace=True),
      nn.MaxPool2d(kernel_size=3,stride=2,padding=0),
      nn.Conv2d(in_channels=96,out_channels=192,kernel_size=5,stride=1,padding=2,bias=False),
      nn.ReLU(inplace=True),
      nn.MaxPool2d(kernel_size=3,stride=2,padding=0),
      nn.Conv2d(in_channels=192,out_channels=384,kernel_size=3,stride=1,padding=1,bias=False),
      nn.ReLU(inplace=True),
      nn.Conv2d(in_channels=384,out_channels=256,kernel_size=3,stride=1,padding=1,bias=False),
      nn.ReLU(inplace=True),
      nn.Conv2d(in_channels=256,out_channels=256,kernel_size=3,stride=1,padding=1,bias=False),
      nn.ReLU(inplace=True),
      nn.MaxPool2d(kernel_size=3, stride=2, padding=0),
    )
    self.classifier = nn.Sequential(
      nn.Dropout(p=0.5),
      nn.Linear(in_features=256*6*6,out_features=4096),
      nn.ReLU(inplace=True),
      nn.Dropout(p=0.5),
      nn.Linear(in_features=4096, out_features=4096),
      nn.ReLU(inplace=True),
      nn.Linear(in_features=4096, out_features=num_classes),
    )
  def forward(self,x):
    x = self.feature_extraction(x)
    x = x.view(x.size(0),256*6*6)
    x = self.classifier(x)
    return x


if __name__ =='__main__':
  # model = torchvision.models.AlexNet()
  model = AlexNet()
  print(model)

  input = torch.randn(8,3,224,224)
  out = model(input)
  print(out.shape)

以上这篇PyTorch实现AlexNet示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python 读取某个目录下所有的文件实例

在处理数据的时候,因为没有及时的去重,所以需要重新对生成txt进行去重。 可是一个文件夹下有很多txt,总不可能一个一个去操作,这样效率太低了。这里我们需要用到 os 这个包 关键的代码...

python pyenv多版本管理工具的使用

python pyenv多版本管理工具的使用

项目地址github pyenv does... 改变每个用户系统级别的 python 版本 为每个项目提供不同的 python 版本 安装 克隆到本地即为安装,默认目录是...

详解从Django Allauth中进行登录改造小结

大概来介绍一下 Django Allauth 改造的期间遇到的一些问题和改造方法,在此之前我只想说——Django Allauth 是屑。 为什么我说 Django Allauth 是屑...

Python中的异常处理学习笔记

Python 是面向对象的语言,所以程序抛出的异常也是类。 常见的异常类 1.NameError:尝试访问一个没有申明的变量 2.ZeroDivisionError:除数为 0 3.Sy...

Django rest framework基本介绍与代码示例

本文研究的主要是Django rest framework的相关内容,分享了example,具体如下。 Django REST框架是构建Web API的强大而灵活的工具包。 您可能希望使...