-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmodels.py
45 lines (38 loc) · 1.27 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
import torch
import torch.nn as nn
import torchvision.transforms as transforms
import torchvision.datasets as dsets
from torch.autograd import Variable
import torch.nn.functional as F
class linear_model(nn.Module):
def __init__(self, input_dim, output_dim):
super(linear_model, self).__init__()
self.linear = nn.Linear(input_dim, output_dim)
def forward(self, x):
out = x.view(-1, self.num_flat_features(x))
out = self.linear(out)
return out
def num_flat_features(self, x):
size = x.size()[1:]
num_features = 1
for s in size:
num_features *= s
return num_features
class mlp_model(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim):
super(mlp_model, self).__init__()
self.fc1 = nn.Linear(input_dim, hidden_dim)
self.relu1 = nn.ReLU()
self.fc2 = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
out = x.view(-1, self.num_flat_features(x))
out = self.fc1(out)
out = self.relu1(out)
out = self.fc2(out)
return out
def num_flat_features(self, x):
size = x.size()[1:]
num_features = 1
for s in size:
num_features *= s
return num_features