AlexNet
In [ ]:
Copied!
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch
import torch.nn as nn
import torch.nn.functional as F
In [ ]:
Copied!
class AlexNet(nn.Module):
def __init__(self, dropout_p=0.5, num_label=10):
super(AlexNet, self).__init__()
self.features = nn.Sequential(
# conv1
nn.Conv2d(in_channels=1,out_channels=96,kernel_size=11,stride=4,padding=0),
# input_size 224*224 padding=2
# input_size 227*227 padding=0
nn.ReLU(),
# pool1
nn.MaxPool2d(kernel_size=3, stride=2),
# conv2
nn.Conv2d(in_channels=96,out_channels=256,kernel_size=5,stride=1,padding=2),
nn.ReLU(),
# pool2
nn.MaxPool2d(kernel_size=3, stride=2),
# conv3
nn.Conv2d(in_channels=256,out_channels=384,kernel_size=3,stride=1,padding=1),
nn.ReLU(),
# conv4
nn.Conv2d(in_channels=384,out_channels=384,kernel_size=3,stride=1,padding=1),
nn.ReLU(),
# conv5
nn.Conv2d(in_channels=384,out_channels=256,kernel_size=3,stride=1,padding=1),
nn.ReLU(),
# pool3
nn.MaxPool2d(kernel_size=3, stride=2),
)
self.fc = nn.Sequential(
nn.Flatten(),
nn.Linear(in_features=256*6*6, out_features=4096),
nn.ReLU(),
nn.Dropout(dropout_p),
nn.Linear(in_features=4096, out_features=4096),
nn.ReLU(),
nn.Dropout(dropout_p),
nn.Linear(in_features=4096, out_features=num_label)
)
def forward(self, x):
x = self.features(x)
x = self.fc(x)
return x
class AlexNet(nn.Module):
def __init__(self, dropout_p=0.5, num_label=10):
super(AlexNet, self).__init__()
self.features = nn.Sequential(
# conv1
nn.Conv2d(in_channels=1,out_channels=96,kernel_size=11,stride=4,padding=0),
# input_size 224*224 padding=2
# input_size 227*227 padding=0
nn.ReLU(),
# pool1
nn.MaxPool2d(kernel_size=3, stride=2),
# conv2
nn.Conv2d(in_channels=96,out_channels=256,kernel_size=5,stride=1,padding=2),
nn.ReLU(),
# pool2
nn.MaxPool2d(kernel_size=3, stride=2),
# conv3
nn.Conv2d(in_channels=256,out_channels=384,kernel_size=3,stride=1,padding=1),
nn.ReLU(),
# conv4
nn.Conv2d(in_channels=384,out_channels=384,kernel_size=3,stride=1,padding=1),
nn.ReLU(),
# conv5
nn.Conv2d(in_channels=384,out_channels=256,kernel_size=3,stride=1,padding=1),
nn.ReLU(),
# pool3
nn.MaxPool2d(kernel_size=3, stride=2),
)
self.fc = nn.Sequential(
nn.Flatten(),
nn.Linear(in_features=256*6*6, out_features=4096),
nn.ReLU(),
nn.Dropout(dropout_p),
nn.Linear(in_features=4096, out_features=4096),
nn.ReLU(),
nn.Dropout(dropout_p),
nn.Linear(in_features=4096, out_features=num_label)
)
def forward(self, x):
x = self.features(x)
x = self.fc(x)
return x