-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworks.py
139 lines (98 loc) · 4.25 KB
/
Networks.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import torch
import torch.nn as nn
import torch.nn.init as init
import torch.nn.functional as F
class Action_Conditioned_combined_FF(nn.Module):
def __init__(self):
super(Action_Conditioned_FF, self).__init__()
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.sensor_fc1 = nn.Linear(5, 64)
self.sensor_bn1 = nn.BatchNorm1d(64)
self.sensor_fc2 = nn.Linear(64, 128)
self.sensor_bn2 = nn.BatchNorm1d(128)
self.sensor_dropout2 = nn.Dropout(0.5)
self.action_fc1 = nn.Linear(1, 16)
self.combined_fc1 = nn.Linear(128 + 16, 64)
self.combined_bn1 = nn.BatchNorm1d(64)
self.combined_dropout1 = nn.Dropout(0.5)
self.combined_fc2 = nn.Linear(64, 32)
self.combined_bn2 = nn.BatchNorm1d(32)
self.combined_dropout2 = nn.Dropout(0.5)
self.fc_out = nn.Linear(32, 1)
for m in self.modules():
if isinstance(m, nn.Linear):
init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
if m.bias is not None:
init.constant_(m.bias, 0)
def forward(self, input):
if input.dim() == 1:
input = input.unsqueeze(0)
sensor_data = input[:, 0:5]
action_data = input[:, 5].unsqueeze(1)
sensor_data = F.relu(self.sensor_bn1(self.sensor_fc1(sensor_data)))
sensor_data = F.relu(self.sensor_bn2(self.sensor_fc2(sensor_data)))
sensor_data = self.sensor_dropout2(sensor_data)
action_data = F.relu(self.action_fc1(action_data))
combined_data = torch.cat((sensor_data, action_data), dim=1)
combined_data = F.relu(self.combined_bn1(self.combined_fc1(combined_data)))
combined_data = self.combined_dropout1(combined_data)
combined_data = F.relu(self.combined_bn2(self.combined_fc2(combined_data)))
combined_data = self.combined_dropout2(combined_data)
output = torch.sigmoid(self.fc_out(combined_data))
return output.squeeze()
def evaluate(self, model, test_loader, loss_function):
model.eval()
total_loss = 0.0
with torch.no_grad():
for data in test_loader:
inputs = data['input'].to(self.device)
labels = data['label'].to(self.device)
outputs = model(inputs)
loss = loss_function(outputs, labels)
total_loss += loss.item()
average_loss = total_loss / len(test_loader)
return average_loss
class Action_Conditioned_FF(nn.Module):
def __init__(self):
super(Action_Conditioned_FF, self).__init__()
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.fc1 = nn.Linear(6, 64)
self.bn1 = nn.BatchNorm1d(64)
self.fc2 = nn.Linear(64, 128)
self.bn2 = nn.BatchNorm1d(128)
self.dropout2 = nn.Dropout(0.5)
self.fc3 = nn.Linear(128, 64)
self.bn3 = nn.BatchNorm1d(64)
self.dropout3 = nn.Dropout(0.5)
self.fc_out = nn.Linear(64, 1)
for m in self.modules():
if isinstance(m, nn.Linear):
init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
if m.bias is not None:
init.constant_(m.bias, 0)
def forward(self, input):
if input.dim() == 1:
input = input.unsqueeze(0)
x = F.relu(self.bn1(self.fc1(input)))
x = F.relu(self.bn2(self.fc2(x)))
x = self.dropout2(x)
x = F.relu(self.bn3(self.fc3(x)))
x = self.dropout3(x)
output = torch.sigmoid(self.fc_out(x))
return output.squeeze()
def evaluate(self, model, test_loader, loss_function):
model.eval()
total_loss = 0.0
with torch.no_grad():
for data in test_loader:
inputs = data['input'].to(self.device)
labels = data['label'].to(self.device)
outputs = model(inputs)
loss = loss_function(outputs, labels)
total_loss += loss.item()
average_loss = total_loss / len(test_loader)
return average_loss
def main():
model = Action_Conditioned_FF()
if __name__ == '__main__':
main()