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设计】。

相关文章

TensorFlow saver指定变量的存取

TensorFlow saver指定变量的存取

今天和大家分享一下用TensorFlow的saver存取训练好的模型那点事。 1. 用saver存取变量; 2. 用saver存取指定变量。 用saver存取变量。 话不多说,先上...

python计算auc指标实例

1、安装scikit-learn 1.1Scikit-learn 依赖 Python (>= 2.6 or >= 3.3), NumPy (>= 1.6.1), Sci...

Python实现二叉树的最小深度的两种方法

找到给定二叉树的最小深度 最小深度是从根节点到最近叶子节点的最短路径上的节点数量 注意:叶子节点没有子树 Example: Given binary tree [3,9,20,null...

pytorch 常用线性函数详解

Pytorch的线性函数主要封装了Blas和Lapack,其用法和接口都与之类似。 常用的线性函数如下: 函数 功能...

python接口自动化测试之接口数据依赖的实现方法

在做自动化测试时,经常会对一整套业务流程进行一组接口上的测试,这时候接口之间经常会有数据依赖,那么具体要怎么实现这个依赖呢。 思路如下: 抽取之前接口的返回值存储到全局变量字典中...