打赏

AlexNet深度卷积神经网络——pytorch版

import torch
from torch import nn
from d2l import torch as d2l

net = nn.Sequential(
    # (224-11+1+2)/4=54
    nn.Conv2d(1,96,kernel_size=11,stride=4,padding=1),nn.ReLU(),
    # (54-3+1)/2=26
    nn.MaxPool2d(kernel_size=3,stride=2),
    # (26+4-5+1)=26
    nn.Conv2d(96,256,kernel_size=5,padding=2),nn.ReLU(),
    # (26-3+1)/2=12
    nn.MaxPool2d(kernel_size=3,stride=2),
    # 12-3+1+2=12
    nn.Conv2d(256,384,kernel_size=3,padding=1),nn.ReLU(),
    # 12-3+1+2=12
    nn.Conv2d(384,384,kernel_size=3,padding=1),nn.ReLU(),
    # 12+2-3+1=12
    nn.Conv2d(384, 384, kernel_size=3, padding=1), nn.ReLU(),
    # 12-3+1+2=12
    nn.Conv2d(384,256,kernel_size=3,padding=1),nn.ReLU(),
    # (12-3+1)/2=5
    nn.MaxPool2d(kernel_size=3,stride=2),nn.Flatten(),
    # 256*5*5=6400
    nn.Linear(6400,4096),nn.ReLU(),nn.Dropout(p=0.5),
    nn.Linear(4096,4096),nn.ReLU(),nn.Dropout(p=0.5),
    nn.Linear(4096,10)
)

x=torch.randn(1,1,224,224)
for layer in net:
    x=layer(x)
    print(layer.__class__.__name__,'output shape:\t',x.shape)


batch_size=128
train_iter,test_iter = d2l.load_data_fashion_mnist(batch_size=batch_size,resize=224)

lr,num_epochs = 0.01,10
d2l.train_ch6(net,train_iter,test_iter,num_epochs,lr,d2l.try_gpu())

 

posted @ 2023-08-06 14:41  不像话  阅读(5)  评论(0编辑  收藏  举报