/// <summary> /// Construct the trainer for a self organizing map. /// </summary> /// <param name="som">The self organizing map.</param> /// <param name="train">The training method.</param> /// <param name="learnMethod">The learning method.</param> /// <param name="learnRate">The learning rate.</param> public TrainSelfOrganizingMap(SelfOrganizingMap som, double[][] train, LearningMethod learnMethod, double learnRate) { this.som = som; this.train = train; this.totalError = 1.0; this.learnMethod = learnMethod; this.learnRate = learnRate; this.outputNeuronCount = som.OutputNeuronCount; this.inputNeuronCount = som.InputNeuronCount; this.totalError = 1.0; for (int tset = 0; tset < train.Length; tset++) { Matrix.Matrix dptr = Matrix.Matrix.CreateColumnMatrix(train[tset]); if (MatrixMath.vectorLength(dptr) < VERYSMALL) { throw (new System.Exception( "Multiplicative normalization has null training case")); } } this.bestnet = new SelfOrganizingMap(this.inputNeuronCount, this.outputNeuronCount, this.som.NormalizationType); this.won = new int[this.outputNeuronCount]; this.correc = new Matrix.Matrix(this.outputNeuronCount, this.inputNeuronCount + 1); if (this.learnMethod == LearningMethod.ADDITIVE) { this.work = new Matrix.Matrix(1, this.inputNeuronCount + 1); } else { this.work = null; } Initialize(); this.bestError = Double.MaxValue; }
/// <summary> /// Copy the weights from one matrix to another. /// </summary> /// <param name="source">The source SOM.</param> /// <param name="target">The target SOM.</param> private void CopyWeights(SelfOrganizingMap source, SelfOrganizingMap target) { MatrixMath.Copy(source.OutputWeights, target .OutputWeights); }
/// <summary> /// Construct the trainer for a self organizing map. /// </summary> /// <param name="som">The self organizing map.</param> /// <param name="train">The training method.</param> /// <param name="learnMethod">The learning method.</param> /// <param name="learnRate">The learning rate.</param> public TrainSelfOrganizingMap(SelfOrganizingMap som, double[][] train, LearningMethod learnMethod, double learnRate) { this.som = som; this.train = train; this.totalError = 1.0; this.learnMethod = learnMethod; this.learnRate = learnRate; this.outputNeuronCount = som.OutputNeuronCount; this.inputNeuronCount = som.InputNeuronCount; this.totalError = 1.0; for (int tset = 0; tset < train.Length; tset++) { Matrix.Matrix dptr = Matrix.Matrix.CreateColumnMatrix(train[tset]); if (MatrixMath.vectorLength(dptr) < VERYSMALL) { throw (new System.Exception( "Multiplicative normalization has null training case")); } } this.bestnet = new SelfOrganizingMap(this.inputNeuronCount, this.outputNeuronCount, this.som.NormalizationType); this.won = new int[this.outputNeuronCount]; this.correc = new Matrix.Matrix(this.outputNeuronCount, this.inputNeuronCount + 1); if (this.learnMethod == LearningMethod.ADDITIVE) { this.work = new Matrix.Matrix(1, this.inputNeuronCount + 1); } else { this.work = null; } Initialize(); this.bestError = Double.MaxValue; }
private void btnBeginTraining_Click(object sender, EventArgs e) { int inputCount = OCRForm.DOWNSAMPLE_HEIGHT * OCRForm.DOWNSAMPLE_WIDTH; int letterCount = this.letters.Items.Count; this.trainingSet = new double[letterCount][]; int index = 0; foreach (char ch in this.letterData.Keys) { this.trainingSet[index] = new double[inputCount]; bool[] data = this.letterData[ch]; for (int i = 0; i < inputCount; i++) { this.trainingSet[index][i] = data[i] ? 0.5 : -0.5; } index++; } network = new SelfOrganizingMap(inputCount, letterCount, NormalizationType.Z_AXIS); this.ThreadProc(); //ThreadStart ts = new ThreadStart(ThreadProc); //Thread thread = new Thread(ts); //thread.Start(); }
void ThreadProc() { Random rand = new Random(); // build the training set this.input = new double[SAMPLE_COUNT][]; for (int i = 0; i < SAMPLE_COUNT; i++) { this.input[i] = new double[INPUT_COUNT]; for (int j = 0; j < INPUT_COUNT; j++) { this.input[i][j] = rand.NextDouble(); } } // build and train the neural network this.net = new SelfOrganizingMap(INPUT_COUNT, OUTPUT_COUNT, NormalizationType.MULTIPLICATIVE); TrainSelfOrganizingMap train = new TrainSelfOrganizingMap( this.net, this.input, TrainSelfOrganizingMap.LearningMethod.SUBTRACTIVE, 0.5); train.Initialize(); double lastError = Double.MaxValue; int errorCount = 0; while (errorCount < 10) { train.Iteration(); this.retry++; this.totalError = train.TotalError; this.bestError = train.BestError; this.Invalidate(); if (this.bestError < lastError) { lastError = this.bestError; errorCount = 0; } else { errorCount++; } } }
/// <summary> /// Copy the weights from one matrix to another. /// </summary> /// <param name="source">The source SOM.</param> /// <param name="target">The target SOM.</param> private void CopyWeights(SelfOrganizingMap source, SelfOrganizingMap target) { MatrixMath.Copy(source.OutputWeights, target .OutputWeights); }