/// <summary> /// Performs training iteration. /// </summary> public void Iteration() { //Next epoch ++_epoch; //Noise intensity double intensity = _alphas[Math.Min(_maxEpoch, _epoch) - 1]; //Adjusted predictors Matrix predictors = PreparePredictors((double)intensity); //Decomposition QRD decomposition = new QRD(predictors); //New waights double[] newWaights = new double[_net.NumOfWeights]; //Regression for each output neuron for (int outputIdx = 0; outputIdx < _net.NumOfOutputValues; outputIdx++) { //Regression Matrix solution = decomposition.Solve(_outputSingleColMatrixCollection[outputIdx]); //Store weights //Input weights for (int i = 0; i < solution.NumOfRows - 1; i++) { newWaights[outputIdx * _net.NumOfInputValues + i] = solution.Data[i][0]; } //Bias weight newWaights[_net.NumOfOutputValues * _net.NumOfInputValues + outputIdx] = solution.Data[solution.NumOfRows - 1][0]; } //Set new weights and compute error _net.SetWeights(newWaights); _mse = _net.ComputeBatchErrorStat(_inputVectorCollection, _outputVectorCollection).MeanSquare; return; }
/// <summary> /// Performs training iteration. /// </summary> public void Iteration() { //Next epoch ++_epoch; //Noise intensity double intensity = _alphas[Math.Min(_maxEpoch, _epoch) - 1]; //Adjusted predictors Matrix predictors = PreparePredictors((double)intensity); //Decomposition QRD decomposition = null; bool useableQRD = true; try { //Try to create QRD. Any exception signals numerical unstability decomposition = new QRD(predictors); } catch { //Creation of QRD object throws exception. QRD object is not ready for use. useableQRD = false; if (_epoch == 1) { //No previous successful epoch so stop training throw; } } if (useableQRD) { //QRD is ready for use (low probability of numerical unstability) //New weights double[] newWeights = new double[_net.NumOfWeights]; //Regression for each output neuron for (int outputIdx = 0; outputIdx < _net.NumOfOutputValues; outputIdx++) { //Regression Matrix solution = decomposition.Solve(_outputSingleColMatrixCollection[outputIdx]); //Store weights //Input weights for (int i = 0; i < solution.NumOfRows - 1; i++) { newWeights[outputIdx * _net.NumOfInputValues + i] = solution.Data[i][0]; } //Bias weight newWeights[_net.NumOfOutputValues * _net.NumOfInputValues + outputIdx] = solution.Data[solution.NumOfRows - 1][0]; } //Set new weights and compute error _net.SetWeights(newWeights); _mse = _net.ComputeBatchErrorStat(_inputVectorCollection, _outputVectorCollection).MeanSquare; } return; }
/// <summary> /// Performs training iteration. /// </summary> public bool Iteration() { //Fetch next noise intensity double newMaxNoise = _maxNoiseSeeker.Next; //Check continue conditions if (AttemptEpoch == MaxAttemptEpoch || Math.Abs(_currMaxNoise - newMaxNoise) < StopNoiseDifference) { //Try new attempt if (!NextAttempt()) { //Next attempt is not available return(false); } else { newMaxNoise = _maxNoiseSeeker.Next; } } //Next epoch ++AttemptEpoch; _currMaxNoise = newMaxNoise; InfoMessage = $"maxNoise={_currMaxNoise.ToString(CultureInfo.InvariantCulture)}"; //Adjusted predictors Matrix predictors = PreparePredictors(_currMaxNoise); //Decomposition QRD decomposition = null; bool useableQRD = true; try { //Try to create QRD. Any exception signals numerical unstability decomposition = new QRD(predictors); } catch { //Creation of QRD object throws exception. QRD object is not ready for use. useableQRD = false; if (AttemptEpoch == 1) { //No previous successful epoch so stop training throw; } } if (useableQRD) { //QRD is ready for use (low probability of numerical unstability) //New weights double[] newWeights = new double[_net.NumOfWeights]; //Regression for each output neuron for (int outputIdx = 0; outputIdx < _net.NumOfOutputValues; outputIdx++) { //Regression Matrix solution = decomposition.Solve(_outputSingleColMatrixCollection[outputIdx]); //Store weights //Input weights for (int i = 0; i < solution.NumOfRows - 1; i++) { newWeights[outputIdx * _net.NumOfInputValues + i] = solution.Data[i][0]; } //Bias weight newWeights[_net.NumOfOutputValues * _net.NumOfInputValues + outputIdx] = solution.Data[solution.NumOfRows - 1][0]; } //Set new weights and compute error _net.SetWeights(newWeights); MSE = _net.ComputeBatchErrorStat(_inputVectorCollection, _outputVectorCollection).MeanSquare; } _maxNoiseSeeker.ProcessError(MSE); return(true); }
//Methods public void Run() { //Linear algebra test double[] flatData = { 0.2, 5, 17.3, 1.01, 54, 7, 2.2, 5.5, 12.13, 11.57, 5.71, -85, -70.1, 15, -18.3, 0.3, 42, -6.25, 0.042, 1, 7.75, -81.01, -21.29, 5.44, 0.1, 4, -4.3, 18.01, 7.12, -3.14, -80.1, 24.4, 4.3, 12.03, 2.789, -13 }; Matrix testM = new Matrix(6, 6, flatData); /* * //Inversion test * Matrix resultM = new Matrix(testM); * resultM.SingleThreadInverse(); */ /* * //Transpose test * Matrix resultM = testM.Transpose(); */ /* * //Multiply test * Matrix resultM = Matrix.Multiply(testM, testM); * for (int i = 0; i < resultM.NumOfRows; i++) * { * Console.WriteLine($"{resultM.Data[i][0]}; {resultM.Data[i][1]}; {resultM.Data[i][2]}; {resultM.Data[i][3]}; {resultM.Data[i][4]}; {resultM.Data[i][5]}"); * } */ ; int numOfweights = 4; int xIdx, dIdx = 0; double[][] data = new double[5][]; data[dIdx] = new double[numOfweights]; xIdx = -1; data[dIdx][++xIdx] = 1; //Bias data[dIdx][++xIdx] = 2; data[dIdx][++xIdx] = 1; data[dIdx][++xIdx] = 3; ++dIdx; data[dIdx] = new double[numOfweights]; xIdx = -1; data[dIdx][++xIdx] = 1; //Bias data[dIdx][++xIdx] = 1; data[dIdx][++xIdx] = 3; data[dIdx][++xIdx] = -3; ++dIdx; data[dIdx] = new double[numOfweights]; xIdx = -1; data[dIdx][++xIdx] = 1; //Bias data[dIdx][++xIdx] = -2; data[dIdx][++xIdx] = 4; data[dIdx][++xIdx] = 4; ++dIdx; data[dIdx] = new double[numOfweights]; xIdx = -1; data[dIdx][++xIdx] = 1; //Bias data[dIdx][++xIdx] = -5; data[dIdx][++xIdx] = 7; data[dIdx][++xIdx] = 6; ++dIdx; data[dIdx] = new double[numOfweights]; xIdx = -1; data[dIdx][++xIdx] = 1; //Bias data[dIdx][++xIdx] = 1; data[dIdx][++xIdx] = 12; data[dIdx][++xIdx] = 5; Matrix M = new Matrix(data, true); Vector desired = new Vector(5); dIdx = -1; desired.Data[++dIdx] = 8; desired.Data[++dIdx] = 13; desired.Data[++dIdx] = 5; desired.Data[++dIdx] = 7; desired.Data[++dIdx] = 10; Vector weights = Matrix.RidgeRegression(M, desired, 0); //Display results for (int i = 0; i < data.Length; i++) { double result = 0; for (int j = 0; j < weights.Length; j++) { result += data[i][j] * weights[j]; } Console.WriteLine($"Computed {result}, Desired {desired.Data[i]}"); } for (int i = 0; i < weights.Length; i++) { Console.WriteLine($"Weight[{i}] = {weights[i]}"); } Console.WriteLine(); //QRD QRD decomposition = new QRD(M); Matrix B = new Matrix(desired.Length, 1, desired.Data); Matrix W = decomposition.Solve(B); //Display results for (int i = 0; i < data.Length; i++) { double result = 0; for (int j = 0; j < W.Data.Length; j++) { result += data[i][j] * W.Data[j][0]; } Console.WriteLine($"Computed {result}, Desired {desired.Data[i]}"); } for (int i = 0; i < W.Data.Length; i++) { Console.WriteLine($"Weight[{i}] = {W.Data[i][0]}"); } ; //TimeSeriesGenerator.SaveTimeSeriesToCsvFile("MackeyGlass_big.csv", "Value", TimeSeriesGenerator.GenMackeyGlassTimeSeries(16000), CultureInfo.InvariantCulture); MackeyGlassGeneratorSettings modSettings = new MackeyGlassGeneratorSettings(18, 0.1, 0.2); IGenerator generator = new MackeyGlassGenerator(modSettings); int steps = 100; for (int i = 0; i < steps; i++) { Console.WriteLine(generator.Next()); } Console.ReadLine(); generator.Reset(); for (int i = 0; i < steps; i++) { Console.WriteLine(generator.Next()); } Console.ReadLine(); ///* SimpleIFSettings settings = new SimpleIFSettings(1, new RandomValueSettings(15, 15), new RandomValueSettings(0.05, 0.05), new RandomValueSettings(5, 5), new RandomValueSettings(20, 20), 0 ); IActivationFunction af = ActivationFactory.Create(settings, new Random(0)); //*/ TestActivation(af, 800, 0.15, 10, 600); return; }