-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodel.py
More file actions
39 lines (33 loc) · 1.24 KB
/
model.py
File metadata and controls
39 lines (33 loc) · 1.24 KB
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
from __future__ import print_function
from __future__ import division
import torch
import torch.nn as nn
class ConvNet(nn.Module):
def __init__(self):
super(ConvNet, self).__init__()
self.conv1 = nn.Conv2d(1, 20, kernel_size=5)
self.conv2 = nn.Conv2d(20, 50, kernel_size=5)
self.fc1 = nn.Linear(800, 500)
self.fc2 = nn.Linear(500, 10)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(2)
self._init_weights()
def _init_weights(self):
for m in self.modules():
if isinstance(m, (nn.Conv2d)):
nn.init.xavier_normal_(m.weight)
nn.init.constant_(m.bias, 1 / m.bias.numel())
if isinstance(m, (nn.Linear)):
nn.init.xavier_normal_(m.weight)
nn.init.constant_(m.bias, 1 / m.bias.numel())
# print(m)
# print(type(m))
# nn.init.xavier_normal_(m.weight)
# nn.init.constant_(m.bias, 1 / m.bias.numel())
def forward(self, x):
x = self.maxpool(self.relu(self.conv1(x)))
x = self.maxpool(self.relu(self.conv2(x)))
x = x.view(-1, 800)
x = self.relu(self.fc1(x))
x = self.fc2(x)
return x