欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

AlexNetpytorch代码实现

时间:2023-08-11

import torchfrom torch import nnfrom d2l import torch as d2lnet=nn.Sequential( nn.Conv2d(1,96,kernel_size=11,stride=4,padding=1),nn.ReLU(), nn.MaxPool2d(kernel_size=3,stride=2), nn.Conv2d(96,128*2,kernel_size=5,padding=2),nn.ReLU(), nn.MaxPool2d(kernel_size=3,stride=2), nn.Conv2d(128*2,192*2,kernel_size=3,padding=1),nn.ReLU(), nn.Conv2d(192*2,192*2,kernel_size=3,padding=1),nn.ReLU(), nn.Conv2d(192*2,128*2,kernel_size=3,padding=1),nn.ReLU(), nn.MaxPool2d(kernel_size=3,stride=2), # 6*6*256 nn.Flatten(), nn.Linear(6400,2048*2),nn.ReLU(),nn.Dropout(p=0.5), nn.Linear(2048*2,2048*2),nn.ReLU(),nn.Dropout(p=0.5), nn.Linear(2048*2,10),nn.ReLU(), )# 看每个层输出得形状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=32train_iter,test_iter=d2l.load_data_fashion_mnist(batch_size=batch_size,resize=224)lr,num_epochs=0.01,5d2l.train_ch6(net,train_iter,test_iter,num_epochs,lr,d2l.try_gpu())

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。