Skip to content

Commit 7dc851e

Browse files
committed
#1. Refactoring code
Signed-off-by: Zhongkai Fu <[email protected]>
1 parent 464553b commit 7dc851e

File tree

4 files changed

+188
-173
lines changed

4 files changed

+188
-173
lines changed

RNNSharp/BiRNN.cs

+6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ public override void SetTrainingSet(DataSet train)
4646
backwardRNN.SetTrainingSet(train);
4747
}
4848

49+
public override void initWeights()
50+
{
51+
forwardRNN.initWeights();
52+
backwardRNN.initWeights();
53+
54+
}
4955
public override void SetValidationSet(DataSet validation)
5056
{
5157
m_ValidationSet = validation;

RNNSharp/LSTMRNN.cs

+36-27
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ public struct LSTMWeight
5757

5858
public class LSTMRNN : RNN
5959
{
60-
public new LSTMCell[] neuHidden; //neurons in hidden layer
61-
protected new LSTMWeight[][] mat_input2hidden;
62-
protected new LSTMWeight[][] mat_feature2hidden;
60+
public LSTMCell[] neuHidden; //neurons in hidden layer
61+
protected LSTMWeight[][] mat_input2hidden;
62+
protected LSTMWeight[][] mat_feature2hidden;
6363

6464
//for LSTM layer
6565
const bool NORMAL = true;
@@ -157,8 +157,7 @@ public override void loadNetBin(string filename)
157157
fea_size = br.ReadInt32();
158158

159159
//Create cells of each layer
160-
CreateCells();
161-
CreateHiddenLayerCells(br);
160+
CreateCell(br);
162161

163162
//Load weight matrix between each two layer pairs
164163
//weight input->hidden
@@ -369,17 +368,34 @@ public void LSTMCellInit(bool type, LSTMCell c)
369368

370369
public override void initMem()
371370
{
372-
base.initMem();
371+
CreateCell(null);
373372

374-
CreateHiddenLayerCells(null);
373+
mat_hidden2output = new Matrix(L2, L1);
375374

375+
for (int i = 0; i < MAX_RNN_HIST; i++)
376+
{
377+
m_Diff[i] = new double[L2];
378+
}
379+
380+
m_tagBigramTransition = new Matrix(L2, L2);
381+
m_DeltaBigramLM = new Matrix(L2, L2);
382+
383+
Console.WriteLine("[TRACE] Initializing weights, random value is {0}", random(-1.0, 1.0));// yy debug
376384
initWeights();
377385
}
378386

379-
private void CreateHiddenLayerCells(BinaryReader br)
387+
private void CreateCell(BinaryReader br)
380388
{
381-
neuHidden = new LSTMCell[L1 + 1];
389+
neuFeatures = new double[fea_size];
390+
neuOutput = new neuron[L2];
391+
392+
for (int a = 0; a < L2; a++)
393+
{
394+
neuOutput[a].cellOutput = 0;
395+
neuOutput[a].er = 0;
396+
}
382397

398+
neuHidden = new LSTMCell[L1 + 1];
383399
for (int i = 0; i < L1; i++)
384400
{
385401
neuHidden[i] = new LSTMCell();
@@ -429,14 +445,14 @@ public void matrixXvectorADD(neuron[] dest, LSTMCell[] srcvec, Matrix srcmatrix,
429445
});
430446
}
431447

432-
public void matrixXvectorADD(LSTMCell[] dest, neuron[] srcvec, LSTMWeight[][] srcmatrix, int from, int to, int from2, int to2)
448+
public void matrixXvectorADD(LSTMCell[] dest, double[] srcvec, LSTMWeight[][] srcmatrix, int from, int to, int from2, int to2)
433449
{
434450
//ac mod
435451
Parallel.For(0, (to - from), parallelOption, i =>
436452
{
437453
for (int j = 0; j < to2 - from2; j++)
438454
{
439-
dest[i + from].netIn += srcvec[j + from2].cellOutput * srcmatrix[i][j].wInputInputGate;
455+
dest[i + from].netIn += srcvec[j + from2] * srcmatrix[i][j].wInputInputGate;
440456
}
441457
});
442458
}
@@ -485,9 +501,9 @@ public override void learnNet(State state, int timeat, bool biRNN = false)
485501
for (int j = 0; j < fea_size; j++)
486502
{
487503
LSTMWeight w = w_i[j];
488-
w_i[j].dSInputCell = w.dSInputCell * c.yForget + gPrime(c.netCellState) * c.yIn * neuFeatures[j].cellOutput;
489-
w_i[j].dSInputInputGate = w.dSInputInputGate * c.yForget + activationFunctionG(c.netCellState) * fPrime(c.netIn) * neuFeatures[j].cellOutput;
490-
w_i[j].dSInputForgetGate = w.dSInputForgetGate * c.yForget + c.previousCellState * fPrime(c.netForget) * neuFeatures[j].cellOutput;
504+
w_i[j].dSInputCell = w.dSInputCell * c.yForget + gPrime(c.netCellState) * c.yIn * neuFeatures[j];
505+
w_i[j].dSInputInputGate = w.dSInputInputGate * c.yForget + activationFunctionG(c.netCellState) * fPrime(c.netIn) * neuFeatures[j];
506+
w_i[j].dSInputForgetGate = w.dSInputForgetGate * c.yForget + c.previousCellState * fPrime(c.netForget) * neuFeatures[j];
491507

492508
}
493509
}
@@ -560,14 +576,14 @@ public override void learnNet(State state, int timeat, bool biRNN = false)
560576
w_i[j].wInputCell += alpha * cellStateError * w_i[j].dSInputCell - w_i[j].wInputCell * beta2;
561577
w_i[j].wInputInputGate += alpha * cellStateError * w_i[j].dSInputInputGate - w_i[j].wInputInputGate * beta2;
562578
w_i[j].wInputForgetGate += alpha * cellStateError * w_i[j].dSInputForgetGate - w_i[j].wInputForgetGate * beta2;
563-
w_i[j].wInputOutputGate += alpha * gradientOutputGate * neuFeatures[j].cellOutput - w_i[j].wInputOutputGate * beta2;
579+
w_i[j].wInputOutputGate += alpha * gradientOutputGate * neuFeatures[j] - w_i[j].wInputOutputGate * beta2;
564580
}
565581
else
566582
{
567583
w_i[j].wInputCell += alpha * cellStateError * w_i[j].dSInputCell;
568584
w_i[j].wInputInputGate += alpha * cellStateError * w_i[j].dSInputInputGate;
569585
w_i[j].wInputForgetGate += alpha * cellStateError * w_i[j].dSInputForgetGate;
570-
w_i[j].wInputOutputGate += alpha * gradientOutputGate * neuFeatures[j].cellOutput;
586+
w_i[j].wInputOutputGate += alpha * gradientOutputGate * neuFeatures[j];
571587
}
572588

573589
}
@@ -683,9 +699,9 @@ public override void computeNet(State state, double[] doutput)
683699
for (int i = 0; i < fea_size; i++)
684700
{
685701
LSTMWeight w = mat_feature2hidden[j][i];
686-
cell_j.netForget += neuFeatures[i].cellOutput * w.wInputForgetGate;
687-
cell_j.netCellState += neuFeatures[i].cellOutput * w.wInputCell;
688-
cell_j.netOut += neuFeatures[i].cellOutput * w.wInputOutputGate;
702+
cell_j.netForget += neuFeatures[i] * w.wInputForgetGate;
703+
cell_j.netCellState += neuFeatures[i] * w.wInputCell;
704+
cell_j.netOut += neuFeatures[i] * w.wInputOutputGate;
689705
}
690706
}
691707

@@ -731,16 +747,9 @@ public override void computeNet(State state, double[] doutput)
731747
public override void netFlush() //cleans all activations and error vectors
732748
{
733749
int a;
734-
for (a = 0; a < L0; a++)
735-
{
736-
neuInput[a].cellOutput = 0;
737-
neuInput[a].er = 0;
738-
}
739-
740750
for (a = 0; a < fea_size; a++)
741751
{
742-
neuFeatures[a].cellOutput = 0;
743-
neuFeatures[a].er = 0;
752+
neuFeatures[a] = 0;
744753
}
745754

746755
for (int i = 0; i < L1; i++)

RNNSharp/RNN.cs

+19-135
Original file line numberDiff line numberDiff line change
@@ -54,29 +54,19 @@ abstract public class RNN
5454
public int L1;
5555
public int L2;
5656
protected int fea_size;
57-
5857
protected float randrng;
59-
6058
protected int final_stop;
6159

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
6660

61+
protected double[] neuFeatures; //features in input layer
62+
public neuron[] neuOutput; //neurons in output layer
63+
public Matrix mat_hidden2output;
6764

6865
protected const int MAX_RNN_HIST = 512;
6966

7067
protected Matrix m_RawOutput;
7168
protected int counterTokenForLM;
7269

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-
8070
protected DataSet m_TrainingSet;
8171
protected DataSet m_ValidationSet;
8272

@@ -163,7 +153,7 @@ public void setInputLayer(State state, int curState, int numStates, int[] predic
163153
var dense = state.GetDenseData();
164154
for (int i = 0; i < dense.GetDimension(); i++)
165155
{
166-
neuFeatures[i].cellOutput = dense[i];
156+
neuFeatures[i] = dense[i];
167157
}
168158
}
169159

@@ -198,9 +188,7 @@ public RNN()
198188

199189
fea_size = 0;
200190

201-
neuInput = null;
202191
neuFeatures = null;
203-
neuHidden = null;
204192
neuOutput = null;
205193
}
206194

@@ -492,45 +480,9 @@ public void ForwardBackward(int numStates, Matrix m_RawOutput)
492480
}
493481
}
494482

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-
}
515483

516484

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();
534486

535487
public double random(double min, double max)
536488
{
@@ -616,60 +568,7 @@ public int randNext()
616568
return rand.Next();
617569
}
618570

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();
673572

674573
public abstract void saveNetBin(string filename);
675574
public abstract void loadNetBin(string filename);
@@ -686,6 +585,18 @@ public static MODELTYPE CheckModelFileType(string filename)
686585
return type;
687586
}
688587

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+
689600
public void matrixXvectorADD(neuron[] dest, neuron[] srcvec, Matrix srcmatrix, int from, int to, int from2, int to2, int type)
690601
{
691602
if (type == 0)
@@ -723,34 +634,7 @@ public void matrixXvectorADD(neuron[] dest, neuron[] srcvec, Matrix srcmatrix, i
723634
}
724635
}
725636

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();
754638

755639
public int[] DecodeNN(Sequence seq)
756640
{

0 commit comments

Comments
 (0)