alexnet pytorch
时间: 2023-08-21 16:14:14 浏览: 174
AlexNet是一个经典的深度学习模型,用于图像分类任务。下面是一个使用PyTorch实现的AlexNet模型的基本结构:
```python
import torch
import torch.nn as nn
class My_AlexNet(nn.Module):
def __init__(self):
super(My_AlexNet, self).__init__()
# 定义模型的基本结构
self.features = nn.Sequential(
nn.Conv2d(3, 48, kernel_size=11),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.Conv2d(48, 128, kernel_size=5, padding=2),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.Conv2d(128, 192, kernel_size=3, stride=1, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(192, 192, kernel_size=3, stride=1, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(192, 128, kernel_size=3, stride=1, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2)
)
self.classifier = nn.Sequential(
nn.Linear(128 * 6 * 6, 4096),
nn.ReLU(inplace=True),
nn.Dropout(),
nn.Linear(4096, 4096),
nn.ReLU(inplace=True),
nn.Dropout(),
nn.Linear(4096, 1000),
nn.ReLU(inplace=True),
nn.Dropout(),
nn.Linear(1000, num_classes)
)
def forward(self, x):
x = self.features(x)
x = torch.flatten(x, start_dim=1)
x = self.classifier(x)
return x
```
这个模型的前向传播过程中,首先通过一系列的卷积层和池化层提取图像的特征,然后通过全连接层进行分类。在每个卷积层之后,都使用了ReLU激活函数来引入非线性。在全连接层之间,还使用了Dropout层来防止过拟合。\[1\]\[2\]\[3\]
#### 引用[.reference_title]
- *1* [pytorch实战2:基于pytorch实现AlexNet模型](https://blog.csdn.net/weixin_46676835/article/details/128730161)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* *3* [pytorch实现AlexNet(含完整代码)](https://blog.csdn.net/weixin_45836809/article/details/121690604)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文