forked from swyoon/pytorch-energy-based-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
81 lines (71 loc) · 2.61 KB
/
models.py
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
77
78
79
80
81
import torch
import torch.nn as nn
import torch.nn.functional as F
class ConvNet(nn.Module):
def __init__(self, in_chan=1, out_chan=64, nh=8, out_activation=None):
"""
ConvNet tailored for MNIST (28x28 images)
nh: multiplier for the number of filters
"""
super(ConvNet, self).__init__()
self.conv1 = nn.Conv2d(in_chan, nh * 4, kernel_size=3, bias=True)
self.conv2 = nn.Conv2d(nh * 4, nh * 8, kernel_size=3, bias=True)
self.max1 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv3 = nn.Conv2d(nh * 8, nh * 8, kernel_size=3, bias=True)
self.conv4 = nn.Conv2d(nh * 8, nh * 16, kernel_size=3, bias=True)
self.max2 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv5 = nn.Conv2d(nh * 16, out_chan, kernel_size=4, bias=True)
self.in_chan, self.out_chan = in_chan, out_chan
self.out_activation = out_activation
def forward(self, x):
x = self.conv1(x)
x = F.relu(x)
x = self.conv2(x)
x = F.relu(x)
x = self.max1(x)
x = self.conv3(x)
x = F.relu(x)
x = self.conv4(x)
x = F.relu(x)
x = self.max2(x)
x = self.conv5(x)
return x
# Fully Connected Network
def get_activation(s_act):
if s_act == 'relu':
return nn.ReLU(inplace=True)
elif s_act == 'sigmoid':
return nn.Sigmoid()
elif s_act == 'softplus':
return nn.Softplus()
elif s_act == 'linear':
return None
elif s_act == 'tanh':
return nn.Tanh()
elif s_act == 'leakyrelu':
return nn.LeakyReLU(0.2, inplace=True)
elif s_act == 'softmax':
return nn.Softmax(dim=1)
else:
raise ValueError(f'Unexpected activation: {s_act}')
class FCNet(nn.Module):
"""fully-connected network"""
def __init__(self, in_dim, out_dim, l_hidden=(50,), activation='sigmoid', out_activation='linear'):
super(FCNet, self).__init__()
l_neurons = tuple(l_hidden) + (out_dim,)
if isinstance(activation, str):
activation = (activation,) * len(l_hidden)
activation = tuple(activation) + (out_activation,)
l_layer = []
prev_dim = in_dim
for i_layer, (n_hidden, act) in enumerate(zip(l_neurons, activation)):
l_layer.append(nn.Linear(prev_dim, n_hidden))
act_fn = get_activation(act)
if act_fn is not None:
l_layer.append(act_fn)
prev_dim = n_hidden
self.net = nn.Sequential(*l_layer)
self.in_dim = in_dim
self.out_shape = (out_dim,)
def forward(self, x):
return self.net(x)