-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgru.py
More file actions
76 lines (58 loc) · 2.04 KB
/
gru.py
File metadata and controls
76 lines (58 loc) · 2.04 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
import numpy as np
from .base_layer import BaseLayer
from ..activation import Linear
def sgm(x):
return 1/(1+np.exp(-x))
class LSTM(BaseLayer):
def __init__(self,units,activation=Linear(),return_all=False):
self.units = units
self.activation = activation
self.return_all = return_all
def plug(self,inputlayer):
self.input_shape = inputlayer.output_shape
self.input_unit = inputlayer
inputlayer.output_unit = self
self.zin = 0
self.zout = 0
### Init Weights
# Update Gate
self.wr = np.zeros(1)
self.br = np.zeros(self.units) # To init with large values
# Reset Gate
self.wu = 0
self.bu = 0
# Innov Gate
self.winnov = 0
self.binnov = 0
# Init Deriv
self.dwr = np.zeros(self.wr.shape)
self.dbr = np.zeros(self.br.shape)
self.dwu = np.zeros(self.wu.shape)
self.dbu = np.zeros(self.bu.shape)
self.dwinnov = np.zeros(self.winnov.shape)
self.dbinnov = np.zeros(self.binnov.shape)
@property
def nparams(self):
return self.wr.size + self.br.size + self.wu.size + self.bu.size + self.winnov.size + self.binnov.size
def forward(self,X):
# Concatenate
s = np.concatenate((self.zout,X))
u = sgm(self.wu @ s + self.bu) # Update gate
r = sgm(self.wr @ s + self.br) # Reset Gate
sp = np.concatenate((X,r*self.zout))
zp = np.tanh(self.winnov @ sp + self.binnov) # Innov Gate
self.zout = (1-u)*zp + self.zout*u
return self.zout
def get_gradients(self):
return self.dwr, self.dbr, self.dwu, self.dbu, self.dwinnov, self.dbinnov
def update_weights(self,weights_diff):
uwr,ubr,uwu,ubu,uwinnov,ubinnov = weights_diff
self.wr += uwr
self.br += ubr
self.wu += uwu
self.bu += ubu
self.winnov += uwinnov
self.binnov += ubinnov
def backprop(self,delta):
raise NotImplementedError
# return delta