-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
76 lines (59 loc) · 2.46 KB
/
models.py
File metadata and controls
76 lines (59 loc) · 2.46 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
69
70
71
72
73
74
75
76
"""
Author: Benjamin Garrard
Conform every model that should be made to accept input_neurons, hidden_neurons,
and output_neurons. That way the pytorch pipeline won't break and whomever
is running the experiments will have an easier time.
"""
import torch
import torch.nn as nn
class hdf_Dataset(torch.utils.data.Dataset):
"""
Just the dataset class to batch more easily for pytorch.
Honestly, this coud be given any two numpy arrays corresponding to
X and Y data and this dataset would work for them.
"""
def __init__(self, x_data, y_data):
self.x_data = x_data
self.y_data = y_data
def __getitem__(self, index):
return (self.x_data[index], self.y_data[index])
def __len__(self):
return self.y_data.shape[0]
class logic_net(nn.Module):
def __init__(self, input_neurons, hidden, output_neurons): # all models must conform to input, hidden, and output to be used.
# interestingly as soon as ReLU is used the network immediately finds
# a local minimum
super(logic_net, self).__init__()
self.fc1 = nn.Linear(input_neurons, output_neurons)
self.sig = nn.Sigmoid()
self.relu = nn.ReLU()
torch.manual_seed(1)
nn.init.uniform_(self.fc1.weight, -1, 1)
nn.init.uniform_(self.fc1.bias, -1, 1)
def forward(self, x):
out = self.fc1(x)
out = self.sig(out)
return out
class deep_logic_net(nn.Module): # Relus will cause the network to find a local minimum
def __init__(self, input_neurons, hidden_neurons, output_neurons):
super(deep_logic_net, self).__init__()
self.fc1 = nn.Linear(input_neurons, hidden_neurons)
self.sig = nn.Sigmoid()
self.fc2 = nn.Linear(hidden_neurons, hidden_neurons)
self.fc3 = nn.Linear(hidden_neurons, output_neurons)
torch.manual_seed(1)
nn.init.uniform_(self.fc1.weight, -1, 1)
nn.init.uniform_(self.fc1.bias, -1, 1)
nn.init.uniform_(self.fc2.weight, -1, 1)
nn.init.uniform_(self.fc2.bias, -1, 1)
nn.init.uniform_(self.fc3.weight, -1, 1)
nn.init.uniform_(self.fc3.bias, -1, 1)
def forward(self, x):
out = self.fc1(x)
out = self.sig(out)
# out += x
out = self.fc2(out)
out = self.sig(out)
out = self.fc3(out)
out = self.sig(out)
return out