@@ -54,29 +54,19 @@ abstract public class RNN
54
54
public int L1 ;
55
55
public int L2 ;
56
56
protected int fea_size ;
57
-
58
57
protected float randrng ;
59
-
60
58
protected int final_stop ;
61
59
62
- protected neuron [ ] neuInput ; //neurons in input layer
63
- protected neuron [ ] neuFeatures ; //features in input layer
64
- protected neuron [ ] neuHidden ; //neurons in hidden layer
65
- public neuron [ ] neuOutput ; //neurons in output layer
66
60
61
+ protected double [ ] neuFeatures ; //features in input layer
62
+ public neuron [ ] neuOutput ; //neurons in output layer
63
+ public Matrix mat_hidden2output ;
67
64
68
65
protected const int MAX_RNN_HIST = 512 ;
69
66
70
67
protected Matrix m_RawOutput ;
71
68
protected int counterTokenForLM ;
72
69
73
- protected Matrix mat_input2hidden = new Matrix ( ) ;
74
- public Matrix mat_hidden2output = new Matrix ( ) ;
75
- protected Matrix mat_feature2hidden = new Matrix ( ) ;
76
-
77
-
78
- protected double md_beta = 1.0 ;
79
-
80
70
protected DataSet m_TrainingSet ;
81
71
protected DataSet m_ValidationSet ;
82
72
@@ -163,7 +153,7 @@ public void setInputLayer(State state, int curState, int numStates, int[] predic
163
153
var dense = state . GetDenseData ( ) ;
164
154
for ( int i = 0 ; i < dense . GetDimension ( ) ; i ++ )
165
155
{
166
- neuFeatures [ i ] . cellOutput = dense [ i ] ;
156
+ neuFeatures [ i ] = dense [ i ] ;
167
157
}
168
158
}
169
159
@@ -198,9 +188,7 @@ public RNN()
198
188
199
189
fea_size = 0 ;
200
190
201
- neuInput = null ;
202
191
neuFeatures = null ;
203
- neuHidden = null ;
204
192
neuOutput = null ;
205
193
}
206
194
@@ -492,45 +480,9 @@ public void ForwardBackward(int numStates, Matrix m_RawOutput)
492
480
}
493
481
}
494
482
495
- public void computeHiddenActivity ( )
496
- {
497
- for ( int a = 0 ; a < L1 ; a ++ )
498
- {
499
- if ( neuHidden [ a ] . cellOutput > 50 ) neuHidden [ a ] . cellOutput = 50 ; //for numerical stability
500
- if ( neuHidden [ a ] . cellOutput < - 50 ) neuHidden [ a ] . cellOutput = - 50 ; //for numerical stability
501
- neuHidden [ a ] . cellOutput = 1.0 / ( 1.0 + Math . Exp ( - neuHidden [ a ] . cellOutput ) ) ;
502
- }
503
- }
504
-
505
- public virtual void initWeights ( )
506
- {
507
- int b , a ;
508
- for ( b = 0 ; b < L1 ; b ++ )
509
- {
510
- for ( a = 0 ; a < L0 - L1 ; a ++ )
511
- {
512
- mat_input2hidden [ b ] [ a ] = random ( - randrng , randrng ) + random ( - randrng , randrng ) + random ( - randrng , randrng ) ;
513
- }
514
- }
515
483
516
484
517
- for ( b = 0 ; b < L1 ; b ++ )
518
- {
519
- for ( a = 0 ; a < fea_size ; a ++ )
520
- {
521
- mat_feature2hidden [ b ] [ a ] = random ( - randrng , randrng ) + random ( - randrng , randrng ) + random ( - randrng , randrng ) ;
522
-
523
- }
524
- }
525
-
526
- for ( b = 0 ; b < mat_hidden2output . GetHeight ( ) ; b ++ )
527
- {
528
- for ( a = 0 ; a < L1 ; a ++ )
529
- {
530
- mat_hidden2output [ b ] [ a ] = random ( - randrng , randrng ) + random ( - randrng , randrng ) + random ( - randrng , randrng ) ;
531
- }
532
- }
533
- }
485
+ public abstract void initWeights ( ) ;
534
486
535
487
public double random ( double min , double max )
536
488
{
@@ -616,60 +568,7 @@ public int randNext()
616
568
return rand . Next ( ) ;
617
569
}
618
570
619
- public virtual void initMem ( )
620
- {
621
- CreateCells ( ) ;
622
-
623
- mat_input2hidden = new Matrix ( L1 , L0 - L1 ) ;
624
- mat_feature2hidden = new Matrix ( L1 , fea_size ) ;
625
- mat_hidden2output = new Matrix ( L2 , L1 ) ;
626
-
627
- Console . WriteLine ( "[TRACE] Initializing weights, random value is {0}" , random ( - 1.0 , 1.0 ) ) ; // yy debug
628
- initWeights ( ) ;
629
-
630
- for ( int i = 0 ; i < MAX_RNN_HIST ; i ++ )
631
- {
632
- m_Diff [ i ] = new double [ L2 ] ;
633
- }
634
-
635
- m_tagBigramTransition = new Matrix ( L2 , L2 ) ;
636
- m_DeltaBigramLM = new Matrix ( L2 , L2 ) ;
637
-
638
- }
639
-
640
- protected void CreateCells ( )
641
- {
642
-
643
- neuInput = new neuron [ L0 ] ;
644
- neuFeatures = new neuron [ fea_size ] ;
645
- neuHidden = new neuron [ L1 ] ;
646
- neuOutput = new neuron [ L2 ] ;
647
-
648
- for ( int a = 0 ; a < L0 ; a ++ )
649
- {
650
- neuInput [ a ] . cellOutput = 0 ;
651
- neuInput [ a ] . er = 0 ;
652
- }
653
-
654
- for ( int a = 0 ; a < fea_size ; a ++ )
655
- {
656
- neuFeatures [ a ] . cellOutput = 0 ;
657
- neuFeatures [ a ] . er = 0 ;
658
- }
659
-
660
- for ( int a = 0 ; a < L1 ; a ++ )
661
- {
662
- neuHidden [ a ] . cellOutput = 0 ;
663
- neuHidden [ a ] . er = 0 ;
664
- }
665
-
666
- for ( int a = 0 ; a < L2 ; a ++ )
667
- {
668
- neuOutput [ a ] . cellOutput = 0 ;
669
- neuOutput [ a ] . er = 0 ;
670
- }
671
- }
672
-
571
+ public abstract void initMem ( ) ;
673
572
674
573
public abstract void saveNetBin ( string filename ) ;
675
574
public abstract void loadNetBin ( string filename ) ;
@@ -686,6 +585,18 @@ public static MODELTYPE CheckModelFileType(string filename)
686
585
return type ;
687
586
}
688
587
588
+ public void matrixXvectorADD ( neuron [ ] dest , double [ ] srcvec , Matrix srcmatrix , int from , int to , int from2 , int to2 )
589
+ {
590
+ //ac mod
591
+ Parallel . For ( 0 , ( to - from ) , parallelOption , i =>
592
+ {
593
+ for ( int j = 0 ; j < to2 - from2 ; j ++ )
594
+ {
595
+ dest [ i + from ] . cellOutput += srcvec [ j + from2 ] * srcmatrix [ i ] [ j ] ;
596
+ }
597
+ } ) ;
598
+ }
599
+
689
600
public void matrixXvectorADD ( neuron [ ] dest , neuron [ ] srcvec , Matrix srcmatrix , int from , int to , int from2 , int to2 , int type )
690
601
{
691
602
if ( type == 0 )
@@ -723,34 +634,7 @@ public void matrixXvectorADD(neuron[] dest, neuron[] srcvec, Matrix srcmatrix, i
723
634
}
724
635
}
725
636
726
- public virtual void netFlush ( ) //cleans all activations and error vectors
727
- {
728
- int a ;
729
- for ( a = 0 ; a < L0 - L1 ; a ++ )
730
- {
731
- neuInput [ a ] . cellOutput = 0 ;
732
- neuInput [ a ] . er = 0 ;
733
- }
734
-
735
- for ( a = L0 - L1 ; a < L0 ; a ++ )
736
- {
737
- //last hidden layer is initialized to vector of 0.1 values to prevent unstability
738
- neuInput [ a ] . cellOutput = 0.1 ;
739
- neuInput [ a ] . er = 0 ;
740
- }
741
-
742
- for ( a = 0 ; a < L1 ; a ++ )
743
- {
744
- neuHidden [ a ] . cellOutput = 0 ;
745
- neuHidden [ a ] . er = 0 ;
746
- }
747
-
748
- for ( a = 0 ; a < L2 ; a ++ )
749
- {
750
- neuOutput [ a ] . cellOutput = 0 ;
751
- neuOutput [ a ] . er = 0 ;
752
- }
753
- }
637
+ public abstract void netFlush ( ) ;
754
638
755
639
public int [ ] DecodeNN ( Sequence seq )
756
640
{
0 commit comments