Class: LSTM_CELL

Inherits:
Object
  • Object
show all
Defined in:
lib/CELL.rb

Overview

██╗░░░░░░██████╗████████╗███╗░░░███╗ ░█████╗░███████╗██╗░░░░░██╗░░░░░██║░░░░░██╔════╝╚══██╔══╝████╗░████║ ██╔══██╗██╔════╝██║░░░░░██║░░░░░██║░░░░░╚█████╗░░░░██║░░░██╔████╔██║ ██║░░╚═╝█████╗░░██║░░░░░██║░░░░░██║░░░░░░╚═══██╗░░░██║░░░██║╚██╔╝██║ ██║░░██╗██╔══╝░░██║░░░░░██║░░░░░███████╗██████╔╝░░░██║░░░██║░╚═╝░██║ ╚█████╔╝███████╗███████╗███████╗╚══════╝╚═════╝░░░░╚═╝░░░╚═╝░░░░░╚═╝ ░╚════╝░╚══════╝╚══════╝╚══════╝

Instance Method Summary collapse

Instance Method Details

#applyWeightChangeObject



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
# File 'lib/CELL.rb', line 144

def applyWeightChange()
  #puts ">> " + @dWg[0,0].to_s
  ### Apply input weight changes
  @Wi += @@Alpha * @dWi #@@Alpha
  @Wf += @@Alpha * @dWf
  @Wo += @@Alpha * @dWo
  @Wg += @@Alpha * @dWg
  ### Apply Cell output weight changes
  @Ui += @@Alpha * @dUi
  @Uf += @@Alpha * @dUf
  @Uo += @@Alpha * @dUo
  @Ug += @@Alpha * @dUg
  ### Apply bias changes
  @Bi += @@Alpha * @dBi
  @Bf += @@Alpha * @dBf
  @Bo += @@Alpha * @dBo
  @Bg += @@Alpha * @dBg
  #puts "<< " + @Wg[0,0].to_s
  ### Change related to cell input
  @dWi = DFloat.zeros(1, @sz)
  @dWf = DFloat.zeros(1, @sz)
  @dWo = DFloat.zeros(1, @sz)
  @dWg = DFloat.zeros(1, @sz)
  ### Change related to cell state
  @dUi = DFloat.zeros(1, @sz)
  @dUf = DFloat.zeros(1, @sz)
  @dUo = DFloat.zeros(1, @sz)
  @dUg = DFloat.zeros(1, @sz)
  ### Change required to Bias 
  @dBi = DFloat.zeros(1, @sz)
  @dBf = DFloat.zeros(1, @sz)
  @dBo = DFloat.zeros(1, @sz)
  @dBg = DFloat.zeros(1, @sz)
end

#backwardPropagation(top_diff_h, top_diff_c) ⇒ Object

Back propagation



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
# File 'lib/CELL.rb', line 107

def backwardPropagation(top_diff_h, top_diff_c)
  dC = hp(@O, top_diff_h) + top_diff_c
  dO = hp(@Ct, top_diff_h)
  dI = hp(@G, dC)
  dG = hp(@I, dC)
  dF = hp(@Cprev, dC)
  
  dIinput = hp(sigmoidDer(@I), dI)
  dFinput = hp(sigmoidDer(@F), dF)
  dOinput = hp(sigmoidDer(@O), dO)
  dGinput = hp(tanhDer(@G), dG)
  ### Calculating change required to input weight matrices
  @dWi -= hp(dIinput, @Xt)
  @dWf -= hp(dFinput, @Xt)
  @dWo -= hp(dOinput, @Xt)
  @dWg -= hp(dGinput, @Xt)
  #p @dWi.to_a
  ### Calculating change required to cell state weight matrices
  @dUi += hp(dIinput, @Hprev)
  @dUf += hp(dFinput, @Hprev)
  @dUo += hp(dOinput, @Hprev)
  @dUg += hp(dGinput, @Hprev)
  ### Calculating change required to bias vectors
  @dBi += dIinput
  @dBf += dFinput
  @dBo += dOinput
  @dBg += dGinput
  ### Calculating change required to HPrev
  dHprev = DFloat.zeros(1, @sz)
  dHprev += @Wi.transpose.dot(dIinput)
  dHprev += @Wf.transpose.dot(dFinput)
  dHprev += @Wo.transpose.dot(dOinput)
  dHprev += @Wg.transpose.dot(dGinput)
  ### Calculating and returning bottoms
  @bottom_diff_c = hp(dC, @F)
  @bottom_diff_h = dHprev 
end

#forwardPropagationObject

Forward Propagation



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/CELL.rb', line 92

def forwardPropagation()
  ### Calculating gate values
  @Zf = @Wf.dot(@Xt) + @Uf.dot(@Hprev) + @Bf
  @F = sigmoidVector(@Zf)
  @Zi = @Wi.dot(@Xt) + @Ui.dot(@Hprev) + @Bi
  @I = sigmoidVector(@Zi)
  @Zo = @Wo.dot(@Xt) + @Uo.dot(@Hprev) + @Bo
  @O = sigmoidVector(@Zo)
  @Zg = @Wg.dot(@Xt) + @Ug.dot(@Hprev) + @Bg
  @G = NMath.tanh(@Zg)  
  ### Calculating cell states 
  @Ct = hp(@F, @Cprev) + hp(@I, @G)
  @Ht = hp(@O,  NMath.tanh(@Ct))
end

#getBottomDeltaCtObject



249
250
251
# File 'lib/CELL.rb', line 249

def getBottomDeltaCt()
  return @bottom_diff_c
end

#getBottomDeltaHtObject



246
247
248
# File 'lib/CELL.rb', line 246

def getBottomDeltaHt()
  return @bottom_diff_h
end

#getCtObject



258
259
260
# File 'lib/CELL.rb', line 258

def getCt
  return @Ct
end

#getHtObject



255
256
257
# File 'lib/CELL.rb', line 255

def getHt()
  return @Ht
end

#getYtObject



252
253
254
# File 'lib/CELL.rb', line 252

def getYt()
  return @Yt
end

#hp(nArr1, nArr2) ⇒ Object

Hadamard product



81
82
83
84
85
86
87
88
89
90
# File 'lib/CELL.rb', line 81

def hp(nArr1, nArr2)
  nArr3 = DFloat.zeros(nArr1.shape())
  dims = nArr1.shape()
  for i in 0...(dims[0])
    for j in 0...(dims[1])
      nArr3[i,j] = (nArr1[i,j] * nArr2[i,j])
    end
  end
  return nArr3
end

#init(alpha, sz, terminal_output = nil) ⇒ Object

constructing the required arrays (Numo::NArrays)



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
# File 'lib/CELL.rb', line 32

def init(alpha, sz, terminal_output=nil)
  @sz = sz
  @@Alpha = alpha
  ### Weight matrices with regard to inputs
  @Wg = DFloat.new(1, @sz).rand
  @Wi = DFloat.new(1, @sz).rand
  @Wf = DFloat.new(1, @sz).rand
  @Wo = DFloat.new(1, @sz).rand
  ### Weight matrices with regard to cell previous cell output
  @Ug = DFloat.new(1, @sz).rand
  @Ui = DFloat.new(1, @sz).rand
  @Uf = DFloat.new(1, @sz).rand
  @Uo = DFloat.new(1, @sz).rand
  ### Bias
  @Bg = DFloat.new(1, @sz).rand
  @Bi = DFloat.new(1, @sz).rand
  @Bf = DFloat.new(1, @sz).rand
  @Bo = DFloat.new(1, @sz).rand
  ### Cell input
  @Xt = DFloat.zeros(1, @sz)
  @Yt = DFloat.zeros(1, @sz)
  ### Cell state
  @Hprev   = DFloat.zeros(1, @sz) # Previous cell output
  @Cprev  = DFloat.zeros(1, @sz) # Previous cell state
  @Ct  = DFloat.zeros(1, @sz) # Cell state
  @Ht  = DFloat.zeros(1, @sz) # Cell output    
  ### Internal Cell gates
  @F = DFloat.new(1, @sz).rand
  @I = DFloat.new(1, @sz).rand
  @O = DFloat.new(1, @sz).rand
  @G = DFloat.new(1, @sz).rand
  ### BPTT variables 
  ### Change related to cell input
  @dWi = DFloat.zeros(1, @sz)
  @dWf = DFloat.zeros(1, @sz)
  @dWo = DFloat.zeros(1, @sz)
  @dWg = DFloat.zeros(1, @sz)
  ### Change related to cell state
  @dUi = DFloat.zeros(1, @sz)
  @dUf = DFloat.zeros(1, @sz)
  @dUo = DFloat.zeros(1, @sz)
  @dUg = DFloat.zeros(1, @sz)
  ### Change required to Bias 
  @dBi = DFloat.zeros(1, @sz)
  @dBf = DFloat.zeros(1, @sz)
  @dBo = DFloat.zeros(1, @sz)
  @dBg = DFloat.zeros(1, @sz)
end

#setCprev(cp) ⇒ Object



238
239
240
241
242
243
244
245
# File 'lib/CELL.rb', line 238

def setCprev(cp)
  @Cprev = cp
  if cp.shape()[0] != 1
    puts "Size of cp: " + cp.shape().to_s
    raise "Setting @Cprev of LSTM cell raised an incorrect dimension error"
    return
  end
end

#setHprev(hp) ⇒ Object



230
231
232
233
234
235
236
237
# File 'lib/CELL.rb', line 230

def setHprev(hp)
  if hp.shape()[0] != 1
    puts "Size of hp: " + hp.shape().to_s
    raise "Setting @Hprev of LSTM cell raised an incorrect dimension error"
    return
  end
  @Hprev = hp
end

#setXt(xt) ⇒ Object

Getters and Setters



214
215
216
217
218
219
220
221
# File 'lib/CELL.rb', line 214

def setXt(xt)
  if xt.shape()[0] != 1
    puts "Size of xt: " + xt.shape().to_s
    raise "Setting @Xt of LSTM cell raised an incorrect dimension error"
    return
  end
  @Xt = xt
end

#setYt(yt) ⇒ Object



222
223
224
225
226
227
228
229
# File 'lib/CELL.rb', line 222

def setYt(yt)
  if yt.shape()[0] != 1
    puts "Size of yt: " + yt.shape().to_s
    raise "Setting @Yt of LSTM cell raised an incorrect dimension error"
    return
  end
  @Yt = yt
end

#sigmoid(value_) ⇒ Object



209
210
211
212
# File 'lib/CELL.rb', line 209

def sigmoid(value_)
  output_value =  1 / (1 + Math.exp(-1 * value_))
  return output_value
end

#sigmoidDer(v) ⇒ Object



189
190
191
192
193
194
195
196
197
198
# File 'lib/CELL.rb', line 189

def sigmoidDer(v)
  #input vector must be a horisontal vector
  output_vector = DFloat.zeros(v.shape[0], v.shape[1])
  for i in 0...v.shape[0]
    for j in 0...v.shape[1]
      output_vector[i,j] = v[i,j] * (1 - v[i,j])
    end
  end
  return output_vector
end

#sigmoidVector(v) ⇒ Object



199
200
201
202
203
204
205
206
207
208
# File 'lib/CELL.rb', line 199

def sigmoidVector(v)
  #input vector must be a horisontal vector
  output_vector = DFloat.zeros(v.shape[0], v.shape[1])
  for i in 0...v.shape[0]
    for j in 0...v.shape[1]
      output_vector[i,j] = sigmoid(v[i,j])
    end
  end
  return output_vector
end

#tanhDer(v) ⇒ Object

Transfer functions



179
180
181
182
183
184
185
186
187
188
# File 'lib/CELL.rb', line 179

def tanhDer(v)
  #input vector must be a horisontal vector
  output_vector = DFloat.zeros(v.shape[0], v.shape[1])
  for i in 0...v.shape[0]
    for j in 0...v.shape[1]
      output_vector[i,j] = 1 - (v[i,j]**2)
    end
  end
  return output_vector
end