@@ -141,11 +141,7 @@ public override void GetHiddenLayer(Matrix<double> m, int curStatus)
141
141
142
142
public override void initMem ( )
143
143
{
144
- for ( int i = 0 ; i < MAX_RNN_HIST ; i ++ )
145
- {
146
- m_Diff [ i ] = new double [ L2 ] ;
147
- }
148
-
144
+ m_Diff = new Matrix < double > ( MAX_RNN_HIST , L2 ) ;
149
145
m_tagBigramTransition = new Matrix < double > ( L2 , L2 ) ;
150
146
m_DeltaBigramLM = new Matrix < double > ( L2 , L2 ) ;
151
147
@@ -166,27 +162,6 @@ public override void initMem()
166
162
}
167
163
}
168
164
169
-
170
-
171
- public override Matrix < double > InnerDecode ( Sequence pSequence )
172
- {
173
- Matrix < neuron > mHiddenLayer = null ;
174
- Matrix < double > mRawOutputLayer = null ;
175
- neuron [ ] [ ] outputLayer = InnerDecode ( pSequence , out mHiddenLayer , out mRawOutputLayer ) ;
176
- int numStates = pSequence . GetSize ( ) ;
177
-
178
- Matrix < double > m = new Matrix < double > ( numStates , L2 ) ;
179
- for ( int currState = 0 ; currState < numStates ; currState ++ )
180
- {
181
- for ( int i = 0 ; i < L2 ; i ++ )
182
- {
183
- m [ currState ] [ i ] = outputLayer [ currState ] [ i ] . cellOutput ;
184
- }
185
- }
186
-
187
- return m ;
188
- }
189
-
190
165
int [ ] predicted_fnn ;
191
166
int [ ] predicted_bnn ;
192
167
public neuron [ ] [ ] InnerDecode ( Sequence pSequence , out Matrix < neuron > outputHiddenLayer , out Matrix < double > rawOutputLayer )
@@ -267,12 +242,10 @@ public override void netFlush()
267
242
backwardRNN . netFlush ( ) ;
268
243
}
269
244
270
- public override int [ ] learnSentenceForRNNCRF ( Sequence pSequence )
245
+ public override Matrix < double > learnSentenceForRNNCRF ( Sequence pSequence , RunningMode runningMode )
271
246
{
272
247
//Reset the network
273
248
int numStates = pSequence . GetSize ( ) ;
274
- int [ ] predicted = new int [ numStates ] ;
275
-
276
249
//Predict output
277
250
Matrix < neuron > mergedHiddenLayer = null ;
278
251
Matrix < double > rawOutputLayer = null ;
@@ -281,12 +254,10 @@ public override int[] learnSentenceForRNNCRF(Sequence pSequence)
281
254
ForwardBackward ( numStates , rawOutputLayer ) ;
282
255
283
256
//Get the best result
284
- predicted = new int [ numStates ] ;
285
257
for ( int i = 0 ; i < numStates ; i ++ )
286
258
{
287
259
State state = pSequence . Get ( i ) ;
288
260
logp += Math . Log10 ( m_Diff [ i ] [ state . GetLabel ( ) ] ) ;
289
- predicted [ i ] = GetBestZIndex ( i ) ;
290
261
}
291
262
292
263
UpdateBigramTransition ( pSequence ) ;
@@ -305,44 +276,48 @@ public override int[] learnSentenceForRNNCRF(Sequence pSequence)
305
276
306
277
LearnTwoRNN ( pSequence , mergedHiddenLayer , seqOutput ) ;
307
278
308
- return predicted ;
279
+ return m_Diff ;
309
280
}
310
281
311
- public override int [ ] PredictSentence ( Sequence pSequence )
282
+ public override Matrix < double > PredictSentence ( Sequence pSequence , RunningMode runningMode )
312
283
{
313
284
//Reset the network
314
285
int numStates = pSequence . GetSize ( ) ;
315
- int [ ] predicted = new int [ numStates ] ;
316
286
317
287
//Predict output
318
288
Matrix < neuron > mergedHiddenLayer = null ;
319
289
Matrix < double > rawOutputLayer = null ;
320
290
neuron [ ] [ ] seqOutput = InnerDecode ( pSequence , out mergedHiddenLayer , out rawOutputLayer ) ;
321
291
322
- //Merge forward and backward
323
- for ( int curState = 0 ; curState < numStates ; curState ++ )
292
+ if ( runningMode != RunningMode . Test )
324
293
{
325
- State state = pSequence . Get ( curState ) ;
326
- logp += Math . Log10 ( seqOutput [ curState ] [ state . GetLabel ( ) ] . cellOutput ) ;
327
-
328
- predicted [ curState ] = GetBestOutputIndex ( seqOutput , curState , L2 ) ;
294
+ //Merge forward and backward
295
+ for ( int curState = 0 ; curState < numStates ; curState ++ )
296
+ {
297
+ State state = pSequence . Get ( curState ) ;
298
+ logp += Math . Log10 ( seqOutput [ curState ] [ state . GetLabel ( ) ] . cellOutput ) ;
299
+ counter ++ ;
300
+ }
329
301
}
330
302
331
- //Update hidden-output layer weights
332
- for ( int curState = 0 ; curState < numStates ; curState ++ )
303
+ if ( runningMode == RunningMode . Train )
333
304
{
334
- State state = pSequence . Get ( curState ) ;
335
- //For standard RNN
336
- for ( int c = 0 ; c < L2 ; c ++ )
305
+ //Update hidden-output layer weights
306
+ for ( int curState = 0 ; curState < numStates ; curState ++ )
337
307
{
338
- seqOutput [ curState ] [ c ] . er = - seqOutput [ curState ] [ c ] . cellOutput ;
308
+ State state = pSequence . Get ( curState ) ;
309
+ //For standard RNN
310
+ for ( int c = 0 ; c < L2 ; c ++ )
311
+ {
312
+ seqOutput [ curState ] [ c ] . er = - seqOutput [ curState ] [ c ] . cellOutput ;
313
+ }
314
+ seqOutput [ curState ] [ state . GetLabel ( ) ] . er = 1 - seqOutput [ curState ] [ state . GetLabel ( ) ] . cellOutput ;
339
315
}
340
- seqOutput [ curState ] [ state . GetLabel ( ) ] . er = 1 - seqOutput [ curState ] [ state . GetLabel ( ) ] . cellOutput ;
341
- }
342
316
343
- LearnTwoRNN ( pSequence , mergedHiddenLayer , seqOutput ) ;
317
+ LearnTwoRNN ( pSequence , mergedHiddenLayer , seqOutput ) ;
318
+ }
344
319
345
- return predicted ;
320
+ return rawOutputLayer ;
346
321
}
347
322
348
323
private void LearnTwoRNN ( Sequence pSequence , Matrix < neuron > mergedHiddenLayer , neuron [ ] [ ] seqOutput )
@@ -353,8 +328,6 @@ private void LearnTwoRNN(Sequence pSequence, Matrix<neuron> mergedHiddenLayer, n
353
328
forwardRNN . mat_hidden2output = mat_hidden2output . CopyTo ( ) ;
354
329
backwardRNN . mat_hidden2output = mat_hidden2output . CopyTo ( ) ;
355
330
356
-
357
-
358
331
Parallel . Invoke ( ( ) =>
359
332
{
360
333
for ( int curState = 0 ; curState < numStates ; curState ++ )
@@ -377,8 +350,6 @@ private void LearnTwoRNN(Sequence pSequence, Matrix<neuron> mergedHiddenLayer, n
377
350
//Learn forward network
378
351
for ( int curState = 0 ; curState < numStates ; curState ++ )
379
352
{
380
- System . Threading . Interlocked . Increment ( ref counter ) ;
381
-
382
353
// error propogation
383
354
State state = pSequence . Get ( curState ) ;
384
355
forwardRNN . setInputLayer ( state , curState , numStates , predicted_fnn ) ;
@@ -396,8 +367,6 @@ private void LearnTwoRNN(Sequence pSequence, Matrix<neuron> mergedHiddenLayer, n
396
367
397
368
for ( int curState = 0 ; curState < numStates ; curState ++ )
398
369
{
399
- System . Threading . Interlocked . Increment ( ref counter ) ;
400
-
401
370
int curState2 = numStates - 1 - curState ;
402
371
403
372
// error propogation
0 commit comments