PyTorch实现AlexNet示例

yipeiwu_com6年前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入门教程 超详细1小时学会Python

Python入门教程 超详细1小时学会Python

为什么使用Python    假设我们有这么一项任务:简单测试局域网中的电脑是否连通.这些电脑的ip范围从192.168.0.101到192.168.0.200.&nbs...

疯狂上涨的Python 开发者应从2.x还是3.x着手?

疯狂上涨的Python 开发者应从2.x还是3.x着手?

纵观各大编程语言在 2017 年的发展情况,我们会发现涌现出诸如 Go、Swift 这类后起之秀,而其中最为耀眼的当属 Python。之所以 Python 如此受捧,不仅仅是人工智能、数...

Python 3.6 读取并操作文件内容的实例

所使用python环境为最新的3.6版本 Python中几种对文件的操作方法: 将A文件复制到B文件中去(保持原来格式) 读取文件中的内容,返回List列表 (加载本地词典库) 读取文件...

Python类方法__init__和__del__构造、析构过程分析

最近学习《Python参考手册》学到Class部分,遇到了类的构造析构部分的问题: 1、什么时候构造? 2、什么时候析构? 3、成员变量如何处理? 4、Python中的共享成员函数如何访...

浅谈python多进程共享变量Value的使用tips

浅谈python多进程共享变量Value的使用tips

前言: 在使用tornado的多进程时,需要多个进程共享一个状态变量,于是考虑使用multiprocessing.Value(对于该变量的具体细节请查阅相关资料)。在根据网上资料使用Va...