-
Notifications
You must be signed in to change notification settings - Fork 0
/
lstm_cell.rb
345 lines (339 loc) · 10.7 KB
/
lstm_cell.rb
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env ruby
require 'matrix' # https://www.rubyguides.com/2019/01/ruby-matrix/
require 'superators19' # https://stackoverflow.com/questions/11874579/define-custom-ruby-operator
require 'gnuplot'
require 'gnuplot/multiplot'
=begin
This is an LSTM cell programmed in RUBY.
=end
class Matrix
superator "*~" do |operand|
self.hadamard_product(operand)
end
end
class LSTM_CELL
=begin
This method initialises the matrices used in an LSTM Cell
For a more clear understanding of what each variable does,
please see the fully annotated image on GitHub.
In order for this implementation to work, input size to the
LSTM cell must equal the output size. ins = outs = "sx"
=end
def init(sz, alpha)
# Gate matrices
# Z states = SUM(Wxg*~, Whg*~, bg)
# meaning they are the same dimensions
# as the bias vectors
@Zg = Matrix.build(sz, 1) { 0 }
@Zi = Matrix.build(sz, 1) { 0 }
@Zf = Matrix.build(sz, 1) { 0 }
@Zo = Matrix.build(sz, 1) { 0 }
# Weight matrices (x)
@Wxg = Matrix.identity(sz) *~ (Matrix.build(sz, sz) { rand }) # Weight matrices are filled with
@Wxi = Matrix.identity(sz) *~ (Matrix.build(sz, sz) { rand }) # random values (in identity matrix)
@Wxf = Matrix.identity(sz) *~ (Matrix.build(sz, sz) { rand })
@Wxo = Matrix.identity(sz) *~ (Matrix.build(sz, sz) { rand })
# Weight matrices (h)
@Whg = Matrix.identity(sz) *~ (Matrix.build(sz, sz) { rand })
@Whi = Matrix.identity(sz) *~ (Matrix.build(sz, sz) { rand })
@Whf = Matrix.identity(sz) *~ (Matrix.build(sz, sz) { rand })
@Who = Matrix.identity(sz) *~ (Matrix.build(sz, sz) { rand })
# Bias matrices
@Bg = Matrix.build(sz, 1) { rand } # bias vectors are applied to the output
@Bi = Matrix.build(sz, 1) { rand } # hence they are len(out)
@Bf = Matrix.build(sz, 1) { rand } # BIAS WAS "rand"
# Gate vectors
# Gate vectors are the tanh or sigoid
# outputs of the Z states, and hence
# are the same dimension as the Z states
@Gt = Matrix.build(sz, 1) { 0 }
@It = Matrix.build(sz, 1) { 0 }
@Ft = Matrix.build(sz, 1) { 0 }
@Ot = Matrix.build(sz, 1) { 0 }
# gates
@Ogate = Matrix.build(sz, 1) { 0 }
@Igate = Matrix.build(sz, 1) { 0 }
@Fgate = Matrix.build(sz, 1) { 0 }
# Cell states
# Cell states are the only states that use
# the input size (aside from 1 dimension of
# the weight matrices)
@Ct = Matrix.build(sz, 1) { 0 }
@Ht = Matrix.build(sz, 1) { 0 }
# Previous cell states
@Ctm1 = Matrix.build(sz, 1) { 1 } # defaults are overridden when setting the
@Htm1 = Matrix.build(sz, 1) { 0 } # LSTM cell state
@Xtm1 = Matrix.build(sz, 1) { 1 }
# Cell properties
@sz = sz
@@U = Matrix.build(1, sz) { 1 }
@@Alpha = alpha
@Yt = Matrix.build(sz, 1) { 0 }
end
=begin
This method prints the current weights in a readable syntax.
useful for debugging to see how gradient descent is improving training.
An additional function (in future) would be to save the weights to a CSv file.
That way changes in weight matrices could be visualised in a 3D plot using MATLAB.
=end
def viewWeights()
puts "Wxg"
puts @Wxg.to_a.map(&:inspect)
puts "Wxi"
puts @Wxi.to_a.map(&:inspect)
puts "Wxf"
puts @Wxf.to_a.map(&:inspect)
puts "Wxo"
puts @Wxo.to_a.map(&:inspect)
puts "Whg"
puts @Whg.to_a.map(&:inspect)
puts "Whi"
puts @Whi.to_a.map(&:inspect)
puts "Whf"
puts @Whf.to_a.map(&:inspect)
puts "Who"
puts @Who.to_a.map(&:inspect)
end
def plotWeights()
Gnuplot.open do |gp|
Gnuplot::Multiplot.new(gp, layout: [2,4], title: "Weight matrices") do |mp|
Gnuplot::SPlot.new(mp) do |plot|
plot.title "Wxg"
plot.grid
plot.pm3d
plot.hidden3d
plot.palette 'defined ( 0 "black", 51 "blue", 102 "green", 153 "yellow", 204 "red", 255 "white" )'
plot.data << Gnuplot::DataSet.new( [(1..@sz), (1..@sz), @Wxg] ) do |ds|
ds.with = "pm3d"
ds.matrix = true
end
end
Gnuplot::SPlot.new(mp) do |plot|
plot.title "Wxi"
plot.data << Gnuplot::DataSet.new( [(1..@sz), (1..@sz), @Wxi] ) do |ds|
ds.with = "pm3d"
ds.matrix = true
end
end
Gnuplot::SPlot.new(mp) do |plot|
plot.title "Wxf"
plot.data << Gnuplot::DataSet.new( [(1..@sz), (1..@sz), @Wxf] ) do |ds|
ds.with = "pm3d"
ds.matrix = true
end
end
Gnuplot::SPlot.new(mp) do |plot|
plot.title "Wxo"
plot.data << Gnuplot::DataSet.new( [(1..@sz), (1..@sz), @Wxo] ) do |ds|
ds.with = "pm3d"
ds.matrix = true
end
end
Gnuplot::SPlot.new(mp) do |plot|
plot.title "Whg"
plot.data << Gnuplot::DataSet.new( [(1..@sz), (1..@sz), @Whg] ) do |ds|
ds.with = "pm3d"
ds.matrix = true
end
end
Gnuplot::SPlot.new(mp) do |plot|
plot.title "Whi"
plot.data << Gnuplot::DataSet.new( [(1..@sz), (1..@sz), @Whi] ) do |ds|
ds.with = "pm3d"
ds.matrix = true
end
end
Gnuplot::SPlot.new(mp) do |plot|
plot.title "Whf"
plot.data << Gnuplot::DataSet.new( [(1..@sz), (1..@sz), @Whf] ) do |ds|
ds.with = "pm3d"
ds.matrix = true
end
end
Gnuplot::SPlot.new(mp) do |plot|
plot.title "Who"
plot.data << Gnuplot::DataSet.new( [(1..@sz), (1..@sz), @Who] ) do |ds|
ds.with = "pm3d"
ds.matrix = true
end
end
end
end
end
=begin
This method prints the current state of the LSTM cell
Ht and Ct
=end
def viewState()
puts "Ct"
puts @Ct.to_a.map(&:inspect)
puts "Ht"
puts @Ht.to_a.map(&:inspect)
puts "Yt"
puts @Yt.to_a.map(&:inspect)
puts "Xtm1"
puts @Xtm1.to_a.map(&:inspect)
end
=begin
This sets the input state of the LSTM machine.
Done before forward propagation.
It also sets the output target @Yt
=end
def setState(ctm1_, htm1_, xtm1_, yt_)
@Ctm1 = ctm1_
@Htm1 = htm1_
@Xtm1 = xtm1_
@Yt = yt_
end
=begin
This function implements the forward propagation algorithm of
the LSTM cell.
For more context, and the full equations, see GitHub.
=end
def forwardPropagation()
# 'g' operations
@Zg = multiplyWithWeights(@Xtm1, @Wxg) + multiplyWithWeights(@Htm1, @Whg) + @Bg.transpose()
@Gt = tanhVector(@Zg)
# 'i' operations
@Zi = multiplyWithWeights(@Xtm1, @Wxi) + multiplyWithWeights(@Htm1, @Whi) + @Bi.transpose()
@It = sigmoidVector(@Zi)
@Igate = @Gt *~ @It
# 'f' operations
@Zf = multiplyWithWeights(@Xtm1, @Wxf) + multiplyWithWeights(@Htm1, @Whf) + @Bf.transpose()
@Ft = sigmoidVector(@Zf)
@Fgate = @Ft
# 'o' operations
@Zo = multiplyWithWeights(@Xtm1, @Wxo) + multiplyWithWeights(@Htm1, @Who) + @Bg.transpose()
@Ot = sigmoidVector(@Zo)
@Ogate = @Ot
# cell state operations
@Ct = (@Ctm1 *~ @Fgate) + @Igate
@Ht = @Ogate *~ tanhVector(@Ct)
end
=begin
This method implements gradient descent backward propagation.
This method also updates weight and bais matrices after calculating
gradient.
=end
def backwardPropagation()
# Calculating the gradients with respect to weights and output error
# (see github for explanation in C++)
# https://github.com/ryan-n-may/LSTM-Cpp/tree/main
dE = @Yt - @Ht
# Gradient with respect to gates and states
dE_dot = dE *~ tanhVector(@Ct)
dE_dct = dE *~ @Ot *~ invTanhVector(@Ct)
dE_dit = dE_dct *~ @It
dE_dft = dE_dct *~ @Ctm1
dE_dctm1 = dE_dct *~ @Ft
# Gradient with respect to output weights
dE_dbo = dE *~ tanhVector(@Ct) *~ sigmoidVector(@Zo) *~ invSigmoidVector(@Zo)
dE_dWxo = dE_dbo *~ @Xtm1
dE_dWho = dE_dbo *~ @Htm1
# Gradient with respect to forget weights
dE_dbf = dE *~ @Ot *~ invTanhVector(@Ct) *~ @Ctm1 *~ sigmoidVector(@Zf) *~ invSigmoidVector(@Zf)
dE_dWxf = dE_dbf *~ @Xtm1
dE_dWhf = dE_dbf *~ @Htm1
# Gradient with respect to input weights
dE_dbi = dE *~ @Ot *~ invTanhVector(@Ct) *~ @Gt *~ sigmoidVector(@Zi) *~ invSigmoidVector(@Zi)
dE_dWxi = dE_dbi *~ @Xtm1
dE_dWhi = dE_dbi *~ @Htm1
# Gradient with respect to cell states @Gt
dE_dbg = dE *~ @Ot *~ @Ot *~ invTanhVector(@Ct) *~ @It *~ invTanhVector(@Zg)
dE_dWxg = dE_dbg *~ @Xtm1
dE_dWhg = dE_dbg *~ @Htm1
# Now we update the weights using the calculated gradients.
# Modifying output weights
@Wxo = updateWeights(@Wxo, dE_dWxo, @@Alpha)
@Who = updateWeights(@Who, dE_dWho, @@Alpha)
# Modifying forget weights
@Wxf = updateWeights(@Wxf, dE_dWxf, @@Alpha)
@Whf = updateWeights(@Whf, dE_dWhf, @@Alpha)
# Modifying input weights
@Wxi = updateWeights(@Wxi, dE_dWxi, @@Alpha)
@Whi = updateWeights(@Whi, dE_dWhi, @@Alpha)
# Modifying g weights
@Wxg = updateWeights(@Wxg, dE_dWxg, @@Alpha)
@Whg = updateWeights(@Whg, dE_dWhg, @@Alpha)
# Modifying bias vectors
@Bf = @Bf + dE_dbf.transpose() # UNSURE IF THESE SHOULD ADD OR MINUS
@Bi = @Bi + dE_dbi.transpose()
@Bg = @Bg + dE_dbg.transpose()
end
=begin
This method updates the weight matrix using the gradients.
Itterate over diagonal matrix, append vector value to each diagonal value.
=end
def updateWeights(matrix_, vector_, alpha)
for index in 0...matrix_.column_count()
matrix_[index, index] = matrix_[index, index] + (vector_[0, index] * alpha) # ADD OR MINUS?
end
return matrix_
end
=begin
testing
=end
def multiplyWithWeights(matrix_, weight_)
out = Matrix.build(1, weight_.row_count()) { 0 }
for i in 0...weight_.row_count()
row_sum = 0
for j in 0...weight_.column_count()
row_sum = row_sum + (matrix_[0,j] * weight_[i,j])
end
out[0, i] = row_sum
end
return out
end
=begin
Performs sigmoid function on 1D matrix.
following function performs sigmoid on 1 value.
=end
def sigmoidVector(vector_)
output_vector = vector_
#input vector must be a horisontal vector
for index in 0...vector_.column_count()
output_vector[0, index] = sigmoid(vector_[0, index])
end
return output_vector
end
def sigmoid(value_)
output_value = 1 / (1 + Math.exp(-1 * value_))
return output_value
end
def invSigmoidVector(vector_)
output_vector = vector_
#input vector must be a horisontal vector
for index in 0...vector_.column_count()
output_vector[0, index] = 1 - sigmoid(vector_[0, index])
end
return output_vector
end
=begin
This method performs the tanh function on a 1D matrix.
=end
def tanhVector(vector_)
output_vector = vector_
#input vector must be a horisontal vector
for index in 0...vector_.column_count()
output_vector[0, index] = Math.tanh(vector_[0, index])
end
return output_vector
end
def invTanhVector(vector_)
output_vector = vector_
#input vector must be a horisontal vector
for index in 0...vector_.column_count()
output_vector[0, index] = 1 - (Math.tanh(vector_[0, index]) * Math.tanh(vector_[0, index]))
end
return output_vector
end
=begin
These are the accessor methods
=end
def getCt()
return @Ct
end
def getHt()
return @Ht
end
end