-
Notifications
You must be signed in to change notification settings - Fork 0
/
Layers.py
137 lines (99 loc) · 3.64 KB
/
Layers.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
# -*- coding: utf-8 -*-
"""
Created on Sun Sep 20 00:30:18 2020
@author: Bautista
"""
import numpy as np
import Activations
def add_ones(x):
# adds a column of ones to the right of x (for bias)
return np.hstack((np.ones((x.shape[0],1)), x))
class Layer():
def __init__(self):
self.w = None
self.regularizer = None
pass
def __call__(self):
pass
class WLayer(Layer):
def __init__(self, units = 2, w_std = 1e-3, activation = Activations.ReLU(), input_dim = None, regularizer = None):
self.activation = activation
self.units = units
self.input_dim = input_dim
self.w_std = w_std
if input_dim is not None:
self.w = np.random.normal(0, np.sqrt(2/(self.input_dim+1+self.units)), (input_dim+1, self.units))
self.regularizer = regularizer
def set_input_dim(self, input_dim):
self.input_dim = input_dim
self.w = np.random.normal(0, np.sqrt(2/(self.input_dim+1+self.units)), (input_dim+1, self.units))
def get_y(self, x):
return add_ones(x).dot(self.w)
def update_w(self, dw):
self.w += dw
def get_W(self):
return self.w
def output_dim(self):
return self.units
class Dense(WLayer):
# def __init__(self, units = 2, w_std = 1e-4, activation = Activations.Tanh(), input_dim = None, regularizador = None):
# super().__init__(self, units = 2, w_std = 1e-4, activation = Activations.Tanh(), input_dim = None, regularizador = None)
def __call__(self, x):
self.input = x
return self.activation(self.get_y(x))
def get_type(self):
return "Dense"
def get_input(self):
return self.input
def get_gradient(self, local_grad, x = None):
if x is None:
grad = np.copy(local_grad)
grad *= self.activation.gradient()
grad = np.dot(grad, self.w.T)
return grad[:,1:]
grad = np.copy(local_grad)
grad *= self.activation.gradient(x)
grad = np.dot(grad, self.w.T)
return grad[:, 1:]
def inverse(self, s):
inv = self.activation.inverse(s)
inv = inv.dot(np.linalg.pinv(self.w))
inv = inv[:,1:]
return inv
class ConcatInput(Layer):
def __init__(self, concat, input_dim = None):
self.concat_dim = concat.output_dim()
self.concat = concat
self.input_dim = input_dim
self.w =None
self.regularizer = None
def set_input_dim(self, input_dim):
self.input_dim = input_dim
def output_dim(self):
return self.concat_dim + self.input_dim
def __call__(self, s):
return np.hstack((s, self.concat(self.concat.get_input())))
def get_gradient(self, local_grad, x = None):
if x is None:
return local_grad[:, :self.input_dim]
return local_grad[:, :x.shape[1]]
def get_type(self):
return "ConcatInput"
class InputLayer(Layer):
def __init__(self, input_dim):
self.input_dim = input_dim
self.w = None
self.regularizer = None
def output_dim(self):
return self.input_dim
def __call__(self, x):
self.x = x
return self.x
def get_input(self):
return self.x
def get_type(self):
return "Input"
def get_gradient(self, local_grad, x = None):
return local_grad
def inverse(self, s):
return s