-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcnn_mlp.py
More file actions
68 lines (57 loc) · 2.06 KB
/
cnn_mlp.py
File metadata and controls
68 lines (57 loc) · 2.06 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
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
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from basic_model import BasicModel
class conv_module(nn.Module):
def __init__(self):
super(conv_module, self).__init__()
self.conv1 = nn.Conv2d(16, 32, kernel_size=3, stride=2)
self.batch_norm1 = nn.BatchNorm2d(32)
self.relu1 = nn.ReLU()
self.conv2 = nn.Conv2d(32, 32, kernel_size=3, stride=2)
self.batch_norm2 = nn.BatchNorm2d(32)
self.relu2 = nn.ReLU()
self.conv3 = nn.Conv2d(32, 32, kernel_size=3, stride=2)
self.batch_norm3 = nn.BatchNorm2d(32)
self.relu3 = nn.ReLU()
self.conv4 = nn.Conv2d(32, 32, kernel_size=3, stride=2)
self.batch_norm4 = nn.BatchNorm2d(32)
self.relu4 = nn.ReLU()
def forward(self, x):
x = self.conv1(x)
x = self.relu1(self.batch_norm1(x))
x = self.conv2(x)
x = self.relu2(self.batch_norm2(x))
x = self.conv3(x)
x = self.relu3(self.batch_norm3(x))
x = self.conv4(x)
x = self.relu4(self.batch_norm4(x))
return x.view(-1, 32*4*4)
class mlp_module(nn.Module):
def __init__(self):
super(mlp_module, self).__init__()
self.fc1 = nn.Linear(32*4*4, 512)
self.relu1 = nn.ReLU()
self.fc2 = nn.Linear(512, 8)
self.dropout = nn.Dropout(0.5)
def forward(self, x):
x = self.relu1(self.fc1(x))
x = self.dropout(x)
x = self.fc2(x)
return x
class CNN_MLP(BasicModel):
def __init__(self, args):
super(CNN_MLP, self).__init__(args)
self.conv = conv_module()
self.mlp = mlp_module()
self.optimizer = optim.Adam(self.parameters(), lr=args.lr, betas=(args.beta1, args.beta2), eps=args.epsilon)
def compute_loss(self, output, target, _):
pred = output[0]
loss = F.cross_entropy(pred, target)
return loss
def forward(self, x):
features = self.conv(x.view(-1, 16, 80, 80))
score = self.mlp(features)
return score, None