forked from natolambert/dynamicslearn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_split_nn.py
70 lines (58 loc) · 2.61 KB
/
model_split_nn.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
import torch
import torch.nn as nn
import copy
from torch.autograd import Variable
# from utils import *
# module that sends the [0, nn_in - split] inputs through one network and the [split, nn_in] inputs through another
class SplitModel(nn.Module):
def __init__(self, nn_in, nn_out, width, prob=True, activation = 'swish', dropout = 0.02):
super(SplitModel, self).__init__()
self.nn_in = nn_in
self.nn_out = nn_out
self.prob = prob
self.dropout = dropout
self.width = width
if activation.lower() == 'swish':
self.activation = Swish(B = 1.0)
elif activation.lower() == 'relu':
self.activation = nn.ReLU()
# Common input layer:
self.main = nn.Sequential(nn.Linear(self.nn_in, self.width),
copy.deepcopy(self.activation),
nn.Dropout(p=self.dropout))
# Angular accel model
self.angular = nn.Sequential(nn.Linear(self.width, self.width),
copy.deepcopy(self.activation),
nn.Dropout(p=self.dropout),
nn.Linear(self.width, self.width),
copy.deepcopy(self.activation),
nn.Dropout(p=self.dropout),
nn.Linear(self.width, int(nn_out/3)))
# Euler Angles Model accel model
self.euler = nn.Sequential(nn.Linear(self.width, self.width),
copy.deepcopy(self.activation),
nn.Dropout(p=self.dropout),
nn.Linear(self.width, self.width),
copy.deepcopy(self.activation),
nn.Dropout(p=self.dropout),
nn.Linear(self.width, int(nn_out/3)))
# Linear accel model
self.linear = nn.Sequential(nn.Linear(self.width, self.width),
copy.deepcopy(self.activation),
nn.Dropout(p=self.dropout),
nn.Linear(self.width, self.width),
copy.deepcopy(self.activation),
nn.Dropout(p=self.dropout),
nn.Linear(self.width, int(nn_out/3)))
def forward(self, x):
x = self.main(x)
angular = self.angular(x)
euler = self.euler(x)
linear = self.linear(x)
if self.prob:
means = torch.cat((angular[:,:3], euler[:,:3], linear[:,:3]), 1)
variances = torch.cat((angular[:,3:], euler[:,3:], linear[:,3:]), 1)
x = torch.cat((means, variances), 1)
else:
x = torch.cat((angular, euler, linear), 1)
return x