-
Notifications
You must be signed in to change notification settings - Fork 1
/
differential_network.py
186 lines (136 loc) · 6.43 KB
/
differential_network.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from deep_differential_network.activations import *
class DifferentialLayer(nn.Module):
def __init__(self, input_size, output_size, activation="ReLu"):
super(DifferentialLayer, self).__init__()
# Create layer weights and biases:
self.n_output = output_size
self.weight = nn.Parameter(torch.Tensor(output_size, input_size))
self.bias = nn.Parameter(torch.Tensor(output_size))
# Initialize activation function and its derivative:
if activation == "ReLu":
self.g = nn.ReLU()
self.g_prime = ReLUDer()
elif activation == "SoftPlus":
self.softplus_beta = 1.0
self.g = nn.Softplus(beta=self.softplus_beta)
self.g_prime = SoftplusDer(beta=self.softplus_beta)
elif activation == "Cos":
self.g = Cos()
self.g_prime = CosDer()
elif activation == "Linear":
self.g = Linear()
self.g_prime = LinearDer()
elif activation == "Tanh":
self.g = Tanh()
self.g_prime = TanhDer()
else:
raise ValueError("Activation Type must be in ['Linear', 'ReLu', 'SoftPlus', 'Cos'] but is {0}".format(self.activation))
def forward(self, x, der_prev):
# Apply Affine Transformation:
a = F.linear(x, self.weight, self.bias)
out = self.g(a)
der = torch.matmul(self.g_prime(a).view(-1, self.n_output, 1) * self.weight, der_prev)
return out, der
class DifferentialNetwork(nn.Module):
def __init__(self, n_input, **kwargs):
super(DifferentialNetwork, self).__init__()
# Read optional arguments:
self.n_input = n_input
self.n_width = kwargs.get("n_width", 128)
self.n_hidden = kwargs.get("n_depth", 1)
self.n_output = kwargs.get("n_output", 1)
non_linearity = kwargs.get("activation", "ReLu")
# Initialization of the layers:
self._w_init = kwargs.get("w_init", "xavier_normal")
self._b0 = kwargs.get("b_init", 0.1)
self._g_hidden = kwargs.get("g_hidden", np.sqrt(2.))
self._g_output = kwargs.get("g_output", 1.0)
self._p_sparse = kwargs.get("p_sparse", 0.2)
# Construct Weight Initialization:
if self._w_init == "xavier_normal":
# Construct initialization function:
def init_hidden(layer):
# Set the Hidden Gain:
if self._g_hidden <= 0.0: hidden_gain = torch.nn.init.calculate_gain('relu')
else: hidden_gain = self._g_hidden
torch.nn.init.constant_(layer.bias, self._b0)
torch.nn.init.xavier_normal_(layer.weight, hidden_gain)
with torch.no_grad():
layer.weight = torch.nn.Parameter(layer.weight)
def init_output(layer):
# Set Output Gain:
if self._g_output <= 0.0: output_gain = torch.nn.init.calculate_gain('linear')
else: output_gain = self._g_output
torch.nn.init.constant_(layer.bias, self._b0)
torch.nn.init.xavier_normal_(layer.weight, output_gain)
elif self._w_init == "orthogonal":
# Construct initialization function:
def init_hidden(layer):
# Set the Hidden Gain:
if self._g_hidden <= 0.0: hidden_gain = torch.nn.init.calculate_gain('relu')
else: hidden_gain = self._g_hidden
torch.nn.init.constant_(layer.bias, self._b0)
torch.nn.init.orthogonal_(layer.weight, hidden_gain)
def init_output(layer):
# Set Output Gain:
if self._g_output <= 0.0: output_gain = torch.nn.init.calculate_gain('linear')
else: output_gain = self._g_output
torch.nn.init.constant_(layer.bias, self._b0)
torch.nn.init.orthogonal_(layer.weight, output_gain)
elif self._w_init == "sparse":
assert self._p_sparse < 1. and self._p_sparse >= 0.0
# Construct initialization function:
def init_hidden(layer):
p_non_zero = self._p_sparse
hidden_std = self._g_hidden
torch.nn.init.constant_(layer.bias, self._b0)
torch.nn.init.sparse_(layer.weight, p_non_zero, hidden_std)
def init_output(layer):
p_non_zero = self._p_sparse
output_std = self._g_output
torch.nn.init.constant_(layer.bias, self._b0)
torch.nn.init.sparse_(layer.weight, p_non_zero, output_std)
else:
raise ValueError("Weight Initialization Type must be in ['xavier_normal', 'orthogonal', 'sparse'] "
"but is {0}".format(self._w_init))
# Create Network:
self.layers = nn.ModuleList()
# Create Input Layer:
self.layers.append(DifferentialLayer(self.n_input, self.n_width, activation=non_linearity))
init_hidden(self.layers[-1])
# Create Hidden Layer:
for _ in range(1, self.n_hidden):
self.layers.append(DifferentialLayer(self.n_width, self.n_width, activation=non_linearity))
init_hidden(self.layers[-1])
# Create output Layer:
self.layers.append(DifferentialLayer(self.n_width, self.n_output, activation="Linear"))
init_output(self.layers[-1])
self._eye = torch.eye(self.n_input).view(1, self.n_input, self.n_input)
self.device = self._eye.device
def forward(self, q):
# Create initial derivative of dq/ dq.
# qd_dq = self._eye.repeat(q.shape[0], 1, 1).type_as(q)
qd_dq = self._eye.repeat(q.shape[0], 1, 1)
# Compute the Network:
qd, qd_dq = self.layers[0](q, qd_dq)
for i in range(1, len(self.layers)):
qd, qd_dq = self.layers[i](qd, qd_dq)
return qd, qd_dq
def cuda(self, device=None):
# Move the Network to the GPU:
super(DifferentialNetwork, self).cuda(device=device)
# Move the eye matrix to the GPU:
self._eye = self._eye.cuda()
self.device = self._eye.device
return self
def cpu(self):
# Move the Network to the CPU:
super(DifferentialNetwork, self).cpu()
# Move the eye matrix to the CPU:
self._eye = self._eye.cpu()
self.device = self._eye.device
return self