PyTorch實現(xiàn)AlexNet示例
更新時間:2020年01月14日 14:18:19 作者:mingo_敏
今天小編就為大家分享一篇PyTorch實現(xiàn)AlexNet示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
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實現(xiàn)AlexNet示例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python完成FizzBuzzWhizz問題(拉勾網(wǎng)面試題)示例
這篇文章主要介紹了python完成FizzBuzzWhizz問題(拉勾網(wǎng)面試題)示例,需要的朋友可以參考下2014-05-05
老生常談python函數(shù)參數(shù)的區(qū)別(必看篇)
下面小編就為大家?guī)硪黄仙U刾ython函數(shù)參數(shù)的區(qū)別(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
詳解pandas庫pd.read_excel操作讀取excel文件參數(shù)整理與實例
這篇文章主要介紹了pandas庫pd.read_excel操作讀取excel文件參數(shù)整理與實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02
python爬蟲中g(shù)et和post方法介紹以及cookie作用
本篇文章通過爬取163郵箱實例介紹了python爬蟲中g(shù)et和post方法介紹以及cookie作用,對此有興趣的朋友學(xué)習(xí)下。2018-02-02
Python Matplotlib數(shù)據(jù)可視化模塊使用詳解
matplotlib是基建立在python之上,適用于創(chuàng)建靜態(tài),動畫和交互式可視化,通常與數(shù)據(jù)分析模塊pandas搭配使用,用于數(shù)據(jù)的分析和展示,適用于主流的操作系統(tǒng),如Linux、Win、Mac2022-11-11

