Pytorch实现AlexNet

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import torch.nn as nn


class AlexNet(nn.Module):
def __init__(self):
super(AlexNet, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(in_channels=3,
out_channels=96,
kernel_size=2,
stride=1,
padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2)
)
self.conv2 = nn.Sequential(
nn.Conv2d(in_channels=96,
out_channels=256,
kernel_size=2,
stride=1,
padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2)
)

self.conv3 = nn.Sequential(
nn.Conv2d(in_channels=256,
out_channels=384,
kernel_size=3,
stride=1,
padding=1),
nn.ReLU(),
)
self.conv4 = nn.Sequential(
nn.Conv2d(in_channels=384,
out_channels=384,
kernel_size=3,
stride=1,
padding=1),
nn.ReLU(),
)
self.conv5 = nn.Sequential(
nn.Conv2d(in_channels=384,
out_channels=256,
kernel_size=2,
stride=1,
padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2)
)

self.fc1 = nn.Sequential(
nn.Linear(4 * 4 * 256, 4096),
nn.ReLU(),
nn.Dropout(p=0.8)
)

self.fc2 = nn.Sequential(
nn.Linear(4096, 1024),
nn.ReLU(),
nn.Dropout(p=0.8)
)

self.out = nn.Linear(1024, 10)

def forward(self, inputs):
network = self.conv1(inputs)
network = self.conv2(network)
network = self.conv3(network)
network = self.conv4(network)
network = self.conv5(network)
network = network.view(network.size(0), -1)
network = self.fc1(network)
network = self.fc2(network)
out = self.out(network)
return out, network

转载请注明:Seven的博客

本文标题:Pytorch实现AlexNet

文章作者:Seven

发布时间:2018年09月15日 - 00:00:00

最后更新:2018年12月11日 - 22:14:21

原始链接:http://yoursite.com/2018/09/15/2018-09-15-Pytorch-AlexNet/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

------ 本文结束------
坚持原创技术分享,您的支持将鼓励我继续创作!
0%