public void TransposeMatrix() { MatrixMB mat = new MatrixMB(3, 3); mat[0, 0] = 1; mat[0, 1] = 2; mat[0, 2] = 3; mat[1, 0] = 4; mat[1, 1] = 5; mat[1, 2] = 6; mat[2, 0] = 7; mat[2, 1] = 8; mat[2, 2] = 9; var t = mat.Transposed; Assert.AreEqual(1, t[0, 0]); Assert.AreEqual(2, t[1, 0]); Assert.AreEqual(3, t[2, 0]); Assert.AreEqual(4, t[0, 1]); Assert.AreEqual(5, t[1, 1]); Assert.AreEqual(6, t[2, 1]); Assert.AreEqual(7, t[0, 2]); Assert.AreEqual(8, t[1, 2]); Assert.AreEqual(9, t[2, 2]); }
public void CalculatingLowerUpperMatrices() { MatrixMB mat = new MatrixMB(2, 2); mat.Data[0][0] = 4; mat.Data[0][1] = 7; mat.Data[1][0] = 2; mat.Data[1][1] = 6; mat.MakeLU(); Assert.AreEqual(4, mat.Data[0][0]); Assert.AreEqual(7, mat.Data[0][1]); Assert.AreEqual(2, mat.Data[1][0]); Assert.AreEqual(6, mat.Data[1][1]); var L = mat.L; Assert.AreEqual(1, L.Data[0][0]); Assert.AreEqual(0, L.Data[0][1]); Assert.AreEqual(0.5, L.Data[1][0]); Assert.AreEqual(1, L.Data[1][1]); var U = mat.U; Assert.AreEqual(4, U.Data[0][0]); Assert.AreEqual(7, U.Data[0][1]); Assert.AreEqual(0, U.Data[1][0]); Assert.AreEqual(2.5, U.Data[1][1]); }
public void MatrixDeterminant() { MatrixMB mat = new MatrixMB(3, 3); mat.Data[0][0] = 1; mat.Data[0][1] = 9; mat.Data[0][2] = 1; mat.Data[1][0] = 9; mat.Data[1][1] = 1; mat.Data[1][2] = 9; mat.Data[2][0] = 1; mat.Data[2][1] = 9; mat.Data[2][2] = 1; double det = mat.Determinant; Assert.AreEqual(det, 0); Assert.AreEqual(1, mat.Data[0][0]); Assert.AreEqual(9, mat.Data[0][1]); Assert.AreEqual(1, mat.Data[0][2]); Assert.AreEqual(9, mat.Data[1][0]); Assert.AreEqual(1, mat.Data[1][1]); Assert.AreEqual(9, mat.Data[1][2]); Assert.AreEqual(1, mat.Data[2][0]); Assert.AreEqual(9, mat.Data[2][1]); Assert.AreEqual(1, mat.Data[2][2]); }
public void MultiplyTwoMatrices() { MatrixMB mat1 = new MatrixMB(2, 3); mat1.Data[0][0] = 1; mat1.Data[0][1] = 2; mat1.Data[0][2] = 3; mat1.Data[1][0] = 4; mat1.Data[1][1] = 5; mat1.Data[1][2] = 6; MatrixMB mat2 = new MatrixMB(3, 2); mat2.Data[0][0] = 7; mat2.Data[0][1] = 8; mat2.Data[1][0] = 9; mat2.Data[1][1] = 10; mat2.Data[2][0] = 11; mat2.Data[2][1] = 12; var mat = mat1 * mat2; Assert.AreEqual(58, mat[0, 0]); Assert.AreEqual(64, mat[0, 1]); Assert.AreEqual(139, mat[1, 0]); Assert.AreEqual(154, mat[1, 1]); }
public void FillMatrix() { MatrixMB mat = new MatrixMB(3, 3); mat.Data[0][0] = 1; mat.Data[0][1] = 2; mat.Data[0][2] = 3; mat.Data[1][0] = 4; mat.Data[1][1] = 5; mat.Data[1][2] = 6; mat.Data[2][0] = 7; mat.Data[2][1] = 8; mat.Data[2][2] = 9; Assert.AreEqual(1, mat[0, 0]); Assert.AreEqual(2, mat[0, 1]); Assert.AreEqual(3, mat[0, 2]); Assert.AreEqual(4, mat[1, 0]); Assert.AreEqual(5, mat[1, 1]); Assert.AreEqual(6, mat[1, 2]); Assert.AreEqual(7, mat[2, 0]); Assert.AreEqual(8, mat[2, 1]); Assert.AreEqual(9, mat[2, 2]); }
public void Creation() { MatrixMB mat = new MatrixMB(5, 5); mat.FillWithNumber(1); Assert.AreEqual(25, mat.SumAllValues()); }
public void SumTwoMatrices() { MatrixMB mat1 = new MatrixMB(5, 5); MatrixMB mat2 = new MatrixMB(5, 5); mat1.FillWithNumber(1); mat2.FillWithNumber(2); var mat = mat1 + mat2; Assert.AreEqual(75, mat.SumAllValues()); }
public void SubstractTwoMatrices() { MatrixMB mat1 = new MatrixMB(5, 5); MatrixMB mat2 = new MatrixMB(5, 5); mat1.FillWithNumber(4); mat2.FillWithNumber(2); var mat = mat1 - mat2; Assert.AreEqual(50, mat.SumAllValues()); }
public void Test__WeightCorrectionCalculation() { //this is just check of calculation: ((hessian+mu*I)\gradient)' if is correct /* * >> %((hessian+mu*I)\gradient)' * >> hessian = [1 2; 3 4] * hessian = * * 1 2 * 3 4 * >> mu = 2 * mu = * 2 * >> I = eye(2) * I = * 1 0 * 0 1 * >> gradient=[1;2] * gradient = * 1 * 2 * >> res=((hessian+mu*I)\gradient)' * res = * 0.1667 0.2500 */ var hessian = new MatrixMB(2, 2); //no matter if its hessian result or not hessian[0, 0] = 1; hessian[0, 1] = 2; hessian[1, 0] = 3; hessian[1, 1] = 4; double mu = 2; var I = MatrixMB.Eye(2); var gradient = new MatrixMB(2, 1); gradient[0, 0] = 1; gradient[1, 0] = 2; // var diff = ((hessian.HessianMat + (I * setting.MU)).Inverted * hessian.GradientMat).Transposed; var add = hessian + I * mu; var inv = add.Inverted; var div = inv * gradient; var res = div.Transposed; Assert.AreEqual(1, res.Rows); Assert.AreEqual(2, res.Cols); Assert.AreEqual(0.1667, Math.Round(res[0, 0], 4)); Assert.AreEqual(0.25, Math.Round(res[0, 1], 4)); }
/// <summary> /// Normalize input data to the values in common range /// </summary> public void Normalize() { /* Ti = Ti/max(max(abs(Ti))); */ MatrixMB mb = new MatrixMB(Rows, Cols, Data); mb.Operation(MatrixAction.Abs, 0); double max = mb.MaxValue; this.Operation(MatrixAction.Divide, max); //stare //this.Operation(MatrixAction.Abs, 0); //double max = this.MaxValue; //this.Operation(MatrixAction.Divide, max); }
/// <summary> /// Compute psudo hessian matrix and its gradient /// </summary> /// <param name="info">NetworkInfo - information about neural network</param> /// <param name="inp">Input - input data patterns used for learn</param> /// <param name="dout">Output - output data patterns used for learn</param> /// <param name="topo">Topography - neural network topography</param> /// <param name="ww">Weights - weights</param> /// <param name="act">Activation - activation function selection</param> /// <param name="gain">Gain - neuron gain</param> /// <param name="iw">Index - topography indexes</param> public void Compute(ref NetworkInfo info, ref Input inp, ref Output dout, ref Topography topo, Weights ww, ref Activation act, ref Gain gain, ref Index iw) { GradientMat = MatrixMB.Zeros(info.nw, 1); HessianMat = MatrixMB.Zeros(info.nw, info.nw); np = info.np; //number of patterns ni = info.ni; //number of inputs no = info.no; //number of outputs nw = info.nw; //number of weights nn = info.nn; //number of neurons nio = nn + ni - no; zeros = ni.Zeros(); delo = MatrixMB.Zeros(1, nio + 1); J = MatrixMB.Zeros(1, nw); for (p = 0; p < np; p++) { node.Clear(); node.AddRange(inp.Data[p]); CalculateFunctionValuesAndDerivates(ref ww, ref iw, ref topo, ref act, ref gain); for (k = 0; k < no; k++) { o = nio + k; error = dout.Data[p][k] - node[o]; J.ClearWithZeros(); s = iw.Pos(o - ni); J.Data[0][s] = -derivates[o]; delo.ClearWithZeros(); CalculateJacobian(ref ww, ref iw, ref topo); CalculateForHiddenLayer(ref iw, ref topo, ref ww); if (dout[p, 0] > 0.5) { J = J * ratio; } var JT = J.Transposed; GradientMat = GradientMat + JT * error; HessianMat = HessianMat + JT * J; } } }
/// <summary> /// Normalize input data to the values in common range /// </summary> public void Normalize() { /* * Ti = Ti/max(max(abs(Ti))); */ MatrixMB mb = new MatrixMB(Rows, Cols, Data); mb.Operation(MatrixAction.Abs, 0); double max = mb.MaxValue; this.Operation(MatrixAction.Divide, max); //stare //this.Operation(MatrixAction.Abs, 0); //double max = this.MaxValue; //this.Operation(MatrixAction.Divide, max); }
public void DivideMatrixByMatrix() { MatrixMB mat = new MatrixMB(2, 2); mat.Data[0][0] = 4; mat.Data[0][1] = 7; mat.Data[1][0] = 2; mat.Data[1][1] = 6; MatrixMB b = new MatrixMB(2, 1); b[0, 0] = 1; b[1, 0] = 2; var divided = mat.Inverted * b; Assert.AreEqual(-0.8, Math.Round(divided[0, 0], 4)); Assert.AreEqual(0.6, Math.Round(divided[1, 0], 4)); }
private void OpenDataFile() { try { blockInterface(); inputDataFilename = Common.Dialog.Open(LearnByError.Internazional.Resource.Inst.Get("r165"), LearnByError.Internazional.Resource.Inst.Get("r166"), "dat"); if (inputDataFilename == "") { return; } inputData = MatrixMB.Load(inputDataFilename); info = LearnByError.Internazional.Resource.Inst.Get("r164") + inputDataFilename; } catch (Exception ex) { ex.ToLog(); info = LearnByError.Internazional.Resource.Inst.Get("r163"); } finally { unblockInterface(); } }
public void Weigths___Correction___Test() { var gradient = new MatrixMB(7, 1); //gradient = [-0.839948683228052; 2.319597374905329; 2.319597374905329; -2.000000000000000; 5.523188311911530; 5.523188311911531; 6.889667569278484]; gradient[0, 0] = -0.839948683228052; gradient[1, 0] = 2.319597374905329; gradient[2, 0] = 2.319597374905329; gradient[3, 0] = -2.000000000000000; gradient[4, 0] = 5.523188311911530; gradient[5, 0] = 5.523188311911531; gradient[6, 0] = 6.889667569278484; /* * hessian = [ * 1.058270685684809 -0.705513790456539 -0.705513790456539 2.519846049684157 -1.679897366456105 -1.679897366456105 -0.639700008449225; * -0.705513790456539 1.058270685684809 0.352756895228269 -1.679897366456105 2.519846049684157 0.839948683228052 1.279400016898449; * -0.705513790456539 0.352756895228269 1.058270685684809 -1.679897366456105 0.839948683228052 2.519846049684157 1.279400016898449; * 2.519846049684157 -1.679897366456105 -1.679897366456105 6.000000000000000 -4.000000000000000 -4.000000000000000 -1.523188311911530; * -1.679897366456105 2.519846049684157 0.839948683228052 -4.000000000000000 6.000000000000000 2.000000000000000 3.046376623823059; * -1.679897366456105 0.839948683228052 2.519846049684157 -4.000000000000000 2.000000000000000 6.000000000000000 3.046376623823059; * -0.639700008449225 1.279400016898449 1.279400016898449 -1.523188311911530 3.046376623823059 3.046376623823059 3.480153950315843 * ]; */ MatrixMB hessian = new MatrixMB(7, 7); //col 1 hessian[0, 0] = 1.058270685684809; hessian[1, 0] = -0.705513790456539; hessian[2, 0] = -0.705513790456539; hessian[3, 0] = 2.519846049684157; hessian[4, 0] = -1.679897366456105; hessian[5, 0] = -1.679897366456105; hessian[6, 0] = -0.639700008449225; //col 2 hessian[0, 1] = -0.705513790456539; hessian[1, 1] = 1.058270685684809; hessian[2, 1] = 0.352756895228269; hessian[3, 1] = -1.679897366456105; hessian[4, 1] = 2.519846049684157; hessian[5, 1] = 0.839948683228052; hessian[6, 1] = 1.279400016898449; //col 3 hessian[0, 2] = -0.705513790456539; hessian[1, 2] = 0.352756895228269; hessian[2, 2] = 1.058270685684809; hessian[3, 2] = -1.679897366456105; hessian[4, 2] = 0.839948683228052; hessian[5, 2] = 2.519846049684157; hessian[6, 2] = 1.279400016898449; //col 4 hessian[0, 3] = 2.519846049684157; hessian[1, 3] = -1.679897366456105; hessian[2, 3] = -1.679897366456105; hessian[3, 3] = 6.000000000000000; hessian[4, 3] = -4.000000000000000; hessian[5, 3] = -4.000000000000000; hessian[6, 3] = -1.523188311911530; //col 5 hessian[0, 4] = -1.679897366456105; hessian[1, 4] = 2.519846049684157; hessian[2, 4] = 0.839948683228052; hessian[3, 4] = -4.000000000000000; hessian[4, 4] = 6.000000000000000; hessian[5, 4] = 2.000000000000000; hessian[6, 4] = 3.046376623823059; //col 6 hessian[0, 5] = -1.679897366456105; hessian[1, 5] = 0.839948683228052; hessian[2, 5] = 2.519846049684157; hessian[3, 5] = -4.000000000000000; hessian[4, 5] = 2.000000000000000; hessian[5, 5] = 6.000000000000000; hessian[6, 5] = 3.046376623823059; //col 7 hessian[0, 6] = -0.639700008449225; hessian[1, 6] = 1.279400016898449; hessian[2, 6] = 1.279400016898449; hessian[3, 6] = -1.523188311911530; hessian[4, 6] = 3.046376623823059; hessian[5, 6] = 3.046376623823059; hessian[6, 6] = 3.480153950315843; Weights weights = new Weights(7); weights.FillWithNumber(1); double mu = 0.01; var I = MatrixMB.Eye(weights.Length); Console.WriteLine("Testowanie obliczania korekty wag"); Console.WriteLine("Hesjan:"); Console.WriteLine(hessian.MatrixToString()); Console.WriteLine("Gradient:"); Console.WriteLine(gradient.MatrixToString()); Console.WriteLine("Wagi początkowe:"); Console.WriteLine(weights.MatrixToString()); Console.WriteLine("MU = " + mu.ToString()); Console.WriteLine("Macierz I"); Console.WriteLine(I.MatrixToString()); //korekta_wag = ((hessian+mu*I)\gradient)'; /* * Lewa strona * Columns 1 through 6 * * 1.068270685684809 -0.705513790456539 -0.705513790456539 2.519846049684157 -1.679897366456105 -1.679897366456105 * -0.705513790456539 1.068270685684809 0.352756895228269 -1.679897366456105 2.519846049684157 0.839948683228052 * -0.705513790456539 0.352756895228269 1.068270685684809 -1.679897366456105 0.839948683228052 2.519846049684157 * 2.519846049684157 -1.679897366456105 -1.679897366456105 6.010000000000000 -4.000000000000000 -4.000000000000000 * -1.679897366456105 2.519846049684157 0.839948683228052 -4.000000000000000 6.010000000000000 2.000000000000000 * -1.679897366456105 0.839948683228052 2.519846049684157 -4.000000000000000 2.000000000000000 6.010000000000000 * -0.639700008449225 1.279400016898449 1.279400016898449 -1.523188311911530 3.046376623823059 3.046376623823059 * * Column 7 * * -0.639700008449225 * 1.279400016898449 * 1.279400016898449 * -1.523188311911530 * 3.046376623823059 * 3.046376623823059 * 3.490153950315843 */ var lewa_strona_matlab = new MatrixMB(7, 7); lewa_strona_matlab[0, 0] = 1.06827068568480900000; lewa_strona_matlab[0, 1] = -0.70551379045653895000; lewa_strona_matlab[0, 2] = -0.70551379045653895000; lewa_strona_matlab[0, 3] = 2.51984604968415700000; lewa_strona_matlab[0, 4] = -1.67989736645610500000; lewa_strona_matlab[0, 5] = -1.67989736645610500000; lewa_strona_matlab[0, 6] = -0.63970000844922503000; lewa_strona_matlab[1, 0] = -0.70551379045653895000; lewa_strona_matlab[1, 1] = 1.06827068568480900000; lewa_strona_matlab[1, 2] = 0.35275689522826897000; lewa_strona_matlab[1, 3] = -1.67989736645610500000; lewa_strona_matlab[1, 4] = 2.51984604968415700000; lewa_strona_matlab[1, 5] = 0.83994868322805205000; lewa_strona_matlab[1, 6] = 1.27940001689844900000; lewa_strona_matlab[2, 0] = -0.70551379045653895000; lewa_strona_matlab[2, 1] = 0.35275689522826897000; lewa_strona_matlab[2, 2] = 1.06827068568480900000; lewa_strona_matlab[2, 3] = -1.67989736645610500000; lewa_strona_matlab[2, 4] = 0.83994868322805205000; lewa_strona_matlab[2, 5] = 2.51984604968415700000; lewa_strona_matlab[2, 6] = 1.27940001689844900000; lewa_strona_matlab[3, 0] = 2.51984604968415700000; lewa_strona_matlab[3, 1] = -1.67989736645610500000; lewa_strona_matlab[3, 2] = -1.67989736645610500000; lewa_strona_matlab[3, 3] = 6.00999999999999980000; lewa_strona_matlab[3, 4] = -4.00000000000000000000; lewa_strona_matlab[3, 5] = -4.00000000000000000000; lewa_strona_matlab[3, 6] = -1.52318831191152990000; lewa_strona_matlab[4, 0] = -1.67989736645610500000; lewa_strona_matlab[4, 1] = 2.51984604968415700000; lewa_strona_matlab[4, 2] = 0.83994868322805205000; lewa_strona_matlab[4, 3] = -4.00000000000000000000; lewa_strona_matlab[4, 4] = 6.00999999999999980000; lewa_strona_matlab[4, 5] = 2.00000000000000000000; lewa_strona_matlab[4, 6] = 3.04637662382305900000; lewa_strona_matlab[5, 0] = -1.67989736645610500000; lewa_strona_matlab[5, 1] = 0.83994868322805205000; lewa_strona_matlab[5, 2] = 2.51984604968415700000; lewa_strona_matlab[5, 3] = -4.00000000000000000000; lewa_strona_matlab[5, 4] = 2.00000000000000000000; lewa_strona_matlab[5, 5] = 6.00999999999999980000; lewa_strona_matlab[5, 6] = 3.04637662382305900000; lewa_strona_matlab[6, 0] = -0.63970000844922503000; lewa_strona_matlab[6, 1] = 1.27940001689844900000; lewa_strona_matlab[6, 2] = 1.27940001689844900000; lewa_strona_matlab[6, 3] = -1.52318831191152990000; lewa_strona_matlab[6, 4] = 3.04637662382305900000; lewa_strona_matlab[6, 5] = 3.04637662382305900000; lewa_strona_matlab[6, 6] = 3.49015395031584270000; var lewa_strona_csharp = hessian + (I * mu); Console.WriteLine("Lewa strona dzielenia - obliczanie korekty wag => (hessian+mu*I)"); for (int i = 0; i < lewa_strona_csharp.Rows; i++) { for (int j = 0; j < lewa_strona_csharp.Cols; j++) { var result = Math.Round(((decimal)lewa_strona_csharp[i, j]), 15); var expected = Math.Round(((decimal)lewa_strona_matlab[i, j]), 15); Console.WriteLine(string.Format("Poz[{0},{1}] => NBN C#: {2}\tMatLab NBN: {3}\t{4}", i, j, result, expected, result == expected ? "OK" : "źle")); } } for (int i = 0; i < lewa_strona_csharp.Rows; i++) { for (int j = 0; j < lewa_strona_csharp.Cols; j++) { var result = Math.Round(((decimal)lewa_strona_csharp[i, j]), 15); var expected = Math.Round(((decimal)lewa_strona_matlab[i, j]), 15); Assert.AreEqual(expected, result); } } var diff = ((hessian + (I * mu)).Inverted * gradient).Transposed; /* * Expected weights diff * % Otrzymana korekta wag: * % * % 0.27954017281149085000 * % 0.21238647096009383000 * % 0.21238647096010940000 * % 0.66561250322368737000 * % 0.50571296842532187000 * % 0.50571296842531543000 * % 1.27722267527372440000 * % Koniec * */ var diffMatLab = new double[] { 0.27954017281149085000, 0.21238647096009383000, 0.21238647096010940000, 0.66561250322368737000, 0.50571296842532187000, 0.50571296842531543000, 1.27722267527372440000 }; /* * Test matlabowy * function obliczanie_korekty_wag() * %ww = ww_backup - ((hessian+mu*I)\gradient)'; * clear(); * ww = [1 1 1 1 1 1 1]; * mu = 0.01; * I = eye(size(ww,2));%tyle co wag * format long; * gradient = [-0.839948683228052; 2.319597374905329; 2.319597374905329; -2.000000000000000; 5.523188311911530; 5.523188311911531; 6.889667569278484]; * hessian = [ * 1.058270685684809 -0.705513790456539 -0.705513790456539 2.519846049684157 -1.679897366456105 -1.679897366456105 -0.639700008449225; * -0.705513790456539 1.058270685684809 0.352756895228269 -1.679897366456105 2.519846049684157 0.839948683228052 1.279400016898449; * -0.705513790456539 0.352756895228269 1.058270685684809 -1.679897366456105 0.839948683228052 2.519846049684157 1.279400016898449; * 2.519846049684157 -1.679897366456105 -1.679897366456105 6.000000000000000 -4.000000000000000 -4.000000000000000 -1.523188311911530; * -1.679897366456105 2.519846049684157 0.839948683228052 -4.000000000000000 6.000000000000000 2.000000000000000 3.046376623823059; * -1.679897366456105 0.839948683228052 2.519846049684157 -4.000000000000000 2.000000000000000 6.000000000000000 3.046376623823059; * -0.639700008449225 1.279400016898449 1.279400016898449 -1.523188311911530 3.046376623823059 3.046376623823059 3.480153950315843 * ]; * * korekta_wag = ((hessian+mu*I)\gradient)'; * fprintf('\nOtrzymana korekta wag:\n'); * for i=1:size(ww,2) * fprintf('\n%.20f',korekta_wag(i)); * end * fprintf('\nKoniec\n'); * end * * % Otrzymana korekta wag: * % * % 0.27954017281149085000 * % 0.21238647096009383000 * % 0.21238647096010940000 * % 0.66561250322368737000 * % 0.50571296842532187000 * % 0.50571296842531543000 * % 1.27722267527372440000 * % Koniec * */ Console.WriteLine("Korekta wag:"); int accuracy = 15; for (int i = 0; i < diffMatLab.Length; i++) { decimal result = (decimal)diff[0, i]; decimal expected = (decimal)diffMatLab[i]; result = Math.Round(result, accuracy); expected = Math.Round(expected, accuracy); Console.WriteLine(string.Format("NBN C#: {0}\tMatLab NBN: {1}\t {2}", result, expected, result == expected ? "OK" : "źle")); } for (int i = 0; i < diffMatLab.Length; i++) { decimal result = (decimal)diff[0, i]; decimal expected = (decimal)diffMatLab[i]; result = Math.Round(result, accuracy); expected = Math.Round(expected, accuracy); Assert.AreEqual(expected, result); } }
/// <summary> /// Load netowrk data for specified position /// </summary> /// <param name="url">String - web address of data file</param> private void loadSample(String url) { try { String filename = Common.Folder.Samples + "\\" + System.IO.Path.GetFileName(url); if (!System.IO.File.Exists(filename)) { status.Text = Form1_Res.Pobieranie_pliku_z_sieci; progress.Style = ProgressBarStyle.Marquee; progress.Visible = true; var bw = new BackgroundWorker(); bw.DoWork += (a, b) => { try { String address = (String)b.Argument; var request = WebRequest.Create(url); request.Timeout = 60000; using (var response = request.GetResponse()) { using (var file = File.Create(filename)) { response.GetResponseStream().CopyTo(file); } } b.Result = filename; } catch { b.Result = ""; } }; bw.RunWorkerCompleted += (a, b) => { try { inputDataFilename = (String)b.Result; status.Text = Form1_Res.Plik_został_pobrany; try { inputData = MatrixMB.Load(inputDataFilename); var tmp = String.Format(Form1_Res.Dane_z_pliku_0_zostały_wczytane, inputDataFilename); info = tmp; Common.Log.Write(tmp); } catch { var tmp = String.Format(Form1_Res.Dane_z_pliku_0_nie_zostały_wczytane, inputDataFilename); info = tmp; Common.Log.Write(tmp); } } catch { status.Text = Form1_Res.Plik_nie_został_pobrany; } finally { progress.Visible = false; unblockInterface(); } }; blockInterface(); progress.Style = ProgressBarStyle.Marquee; progress.Visible = true; progress.Enabled = true; bw.RunWorkerAsync(url); } else { try { inputDataFilename = filename; inputData = MatrixMB.Load(inputDataFilename); var tmp = String.Format(Form1_Res.Dane_z_pliku_0_zostały_wczytane, inputDataFilename); info = tmp; Common.Log.Write(tmp); } catch (Exception ex) { var tmp = String.Format(Form1_Res.Dane_z_pliku_0_nie_zostały_wczytane, inputDataFilename); info = tmp; Common.Log.Write(tmp, ex); } } } catch (Exception ex) { info = ex.ToLog().GetError(); } }
public void InvertMatrix() { Console.WriteLine("Testowanie odwracania macierzy"); //mat.Inverted * mat = mat.Identity //Jeśli A ma odwrotną A to odwrotna A jest równa A MatrixMB mat = new MatrixMB(2, 2); mat.Data[0][0] = 4; mat.Data[0][1] = 7; mat.Data[1][0] = 2; mat.Data[1][1] = 6; Console.WriteLine("Macierz odwracana"); Console.WriteLine(mat.MatrixToString()); double det = mat.Determinant; Assert.AreNotEqual(0, det); Assert.AreEqual(10, det); var inv = mat.Inverted; Console.WriteLine("Macierz odwrócona"); Console.WriteLine(inv.MatrixToString()); Assert.AreEqual(0.6000, Math.Round(inv.Data[0][0], 4)); Assert.AreEqual(-0.7000, Math.Round(inv.Data[0][1], 4)); Assert.AreEqual(-0.2000, Math.Round(inv.Data[1][0], 4)); Assert.AreEqual(0.4000, Math.Round(inv.Data[1][1], 4)); var A = inv.Inverted; Console.WriteLine("Odwrócenie macierzy odwróconej"); Console.WriteLine(inv.MatrixToString()); int accuracy = 15; for (int i = 0; i < A.Rows; i++) { for (int j = 0; j < A.Cols; j++) { decimal o = Math.Round((decimal)mat[i, j], accuracy); decimal a = Math.Round((decimal)A[i, j], accuracy); Console.WriteLine(string.Format("Orginał: {0}\tPorównywana: {1}", o, a)); } } for (int i = 0; i < A.Rows; i++) { for (int j = 0; j < A.Cols; j++) { decimal o = Math.Round((decimal)mat[i, j], accuracy); decimal a = Math.Round((decimal)A[i, j], accuracy); Assert.AreEqual(o, a); } } var identity = inv * mat; Assert.AreEqual(1, Math.Round(identity.Data[0][0], accuracy)); Assert.AreEqual(0, Math.Round(identity.Data[0][1], accuracy)); Assert.AreEqual(0, Math.Round(identity.Data[1][0], accuracy)); Assert.AreEqual(1, Math.Round(identity.Data[1][1], accuracy)); }
public void Hessian___Gradient___Calculation___Test() { int accuracy = 15; /* * function obliczanie_hesjan_gradient() * clear(); * inp = [-1 -1;-1 1; 1 -1]; * dout = [1;0;0]; * topo = [3 1 2 4 1 2 3]; * ww = [1 1 1 1 1 1 1]; * act = [2 0]; * gain = [1 1]; * param = [3 2 1 7 2]; * iw = [1 4 8]; * format long; * * [gradient,hessian] = Hessian(inp,dout,topo,ww,act,gain,param,iw); * fprintf('Otrzymany gradient:\n'); * disp(gradient); * fprintf('\nOtrzymany hesjan:\n'); * disp(hessian); * * % To jest otrzymywane * % Otrzymany gradient: * % -0.839948683228052 * % 2.319597374905329 * % 2.319597374905329 * % -2.000000000000000 * % 5.523188311911530 * % 5.523188311911531 * % 6.889667569278484 * % * % * % Otrzymany hesjan: * % Columns 1 through 6 * % * % 1.058270685684809 -0.705513790456539 -0.705513790456539 2.519846049684157 -1.679897366456105 -1.679897366456105 * % -0.705513790456539 1.058270685684809 0.352756895228269 -1.679897366456105 2.519846049684157 0.839948683228052 * % -0.705513790456539 0.352756895228269 1.058270685684809 -1.679897366456105 0.839948683228052 2.519846049684157 * % 2.519846049684157 -1.679897366456105 -1.679897366456105 6.000000000000000 -4.000000000000000 -4.000000000000000 * % -1.679897366456105 2.519846049684157 0.839948683228052 -4.000000000000000 6.000000000000000 2.000000000000000 * % -1.679897366456105 0.839948683228052 2.519846049684157 -4.000000000000000 2.000000000000000 6.000000000000000 * % -0.639700008449225 1.279400016898449 1.279400016898449 -1.523188311911530 3.046376623823059 3.046376623823059 * % * % Column 7 * % * % -0.639700008449225 * % 1.279400016898449 * % 1.279400016898449 * % -1.523188311911530 * % 3.046376623823059 * % 3.046376623823059 * % 3.480153950315843 * end */ Input input = new Input(3, 2);//inp = [-1 -1;-1 1; 1 -1]; input[0, 0] = -1; input[0, 1] = -1; input[1, 0] = -1; input[1, 1] = 1; input[2, 0] = 1; input[2, 1] = -1; Output output = new Output(3, 1);//dout = [1;0;0]; output[0, 0] = 1; output[1, 0] = 0; output[2, 0] = 0; NetworkInfo info = new NetworkInfo();//param = [3 2 1 7 2]; info.ni = 2; info.nn = 2; info.no = 1; info.np = 3; info.nw = 7; VectorHorizontal vh = new VectorHorizontal(3); vh[0, 0] = 2; vh[0, 1] = 1; vh[0, 2] = 1; Topography topo = Topography.Generate(TopographyType.BMLP, vh);//topo = [3 1 2 4 1 2 3]; //w C# indeksy są od zera a nie od 1 więc wszystko o 1 w dół przestawione jest Assert.AreEqual(2, topo[0]); Assert.AreEqual(0, topo[1]); Assert.AreEqual(1, topo[2]); Assert.AreEqual(3, topo[3]); Assert.AreEqual(0, topo[4]); Assert.AreEqual(1, topo[5]); Assert.AreEqual(2, topo[6]); Weights weights = new Weights(info.nw); //w = [1 1 1 1 1 1 1]; weights.FillWithNumber(1); //załatwione Activation act = new Activation(2); //act = [2 0]; act[0] = 2; act[1] = 0; Gain gain = new Gain(2);//gain = [1 1]; gain[0] = 1; gain[1] = 1; Index iw = Index.Find(ref topo);//iw = [1 4 8]; //ta sama sytuacja, indeksy od 0 startują Assert.AreEqual(0, iw[0]); Assert.AreEqual(3, iw[1]); Assert.AreEqual(7, iw[2]); Console.WriteLine("Testowanie obliczania gradientu i macierzy hesjana"); Console.WriteLine("Użyte dane:"); Console.WriteLine("\nDane wejściowe:"); Console.WriteLine(input.MatrixToString()); Console.WriteLine("\nDane wyjściowe:"); Console.WriteLine(output.MatrixToString()); Console.WriteLine("\nWagi;"); Console.WriteLine(weights.MatrixToString()); Console.WriteLine("\nTopologia:"); Console.WriteLine(topo.MatrixToString()); Console.WriteLine("\nIndeksy topologii:"); Console.WriteLine(iw.MatrixToString()); Console.WriteLine("\nFunkcje aktywacji:"); Console.WriteLine(act.MatrixToString()); Console.WriteLine("\nWzmocnienia (gains):"); Console.WriteLine(gain.MatrixToString()); Console.WriteLine("\nParametry (param):"); Console.WriteLine(info.ToString()); Hessian hess = new Hessian(ref info); hess.Compute(ref info, ref input, ref output, ref topo, weights, ref act, ref gain, ref iw); var g = hess.GradientMat; var h = hess.HessianMat; Console.WriteLine("\nSprawdzanie gradientu z dokładnością do 15 miejsc po przecinku"); var matG = new double[] { -0.839948683228052, 2.319597374905329, 2.319597374905329, -2.000000000000000, 5.523188311911530, 5.523188311911531, 6.889667569278484 }; /* * % Otrzymany gradient: * % -0.839948683228052 * % 2.319597374905329 * % 2.319597374905329 * % -2.000000000000000 * % 5.523188311911530 * % 5.523188311911531 * % 6.889667569278484 */ for (int i = 0; i < matG.Length; i++) { Console.WriteLine(string.Format("NBN C#: {0}\tMatLab NBN: {1}\t{2}", Math.Round(g[i, 0], accuracy), matG[i], Math.Round(g[i, 0], accuracy) == matG[i] ? "OK" : "źle")); } Assert.AreEqual(-0.839948683228052, Math.Round(g[0, 0], accuracy)); Assert.AreEqual(2.319597374905329, Math.Round(g[1, 0], accuracy)); Assert.AreEqual(2.319597374905329, Math.Round(g[2, 0], accuracy)); Assert.AreEqual(-2.000000000000000, Math.Round(g[3, 0], accuracy)); Assert.AreEqual(5.523188311911530, Math.Round(g[4, 0], accuracy)); Assert.AreEqual(5.523188311911531, Math.Round(g[5, 0], accuracy)); Assert.AreEqual(6.889667569278484, Math.Round(g[6, 0], accuracy)); Console.WriteLine("\nSprawdzanie macierzy hesjana\nPorównania z dokładnością do 15 miejsc po przecinku"); MatrixMB matH = new MatrixMB(7, 7); //col 1 matH[0, 0] = 1.058270685684809; matH[1, 0] = -0.705513790456539; matH[2, 0] = -0.705513790456539; matH[3, 0] = 2.519846049684157; matH[4, 0] = -1.679897366456105; matH[5, 0] = -1.679897366456105; matH[6, 0] = -0.639700008449225; //col 2 matH[0, 1] = -0.705513790456539; matH[1, 1] = 1.058270685684809; matH[2, 1] = 0.352756895228269; matH[3, 1] = -1.679897366456105; matH[4, 1] = 2.519846049684157; matH[5, 1] = 0.839948683228052; matH[6, 1] = 1.279400016898449; //col 3 matH[0, 2] = -0.705513790456539; matH[1, 2] = 0.352756895228269; matH[2, 2] = 1.058270685684809; matH[3, 2] = -1.679897366456105; matH[4, 2] = 0.839948683228052; matH[5, 2] = 2.519846049684157; matH[6, 2] = 1.279400016898449; //col 4 matH[0, 3] = 2.519846049684157; matH[1, 3] = -1.679897366456105; matH[2, 3] = -1.679897366456105; matH[3, 3] = 6.000000000000000; matH[4, 3] = -4.000000000000000; matH[5, 3] = -4.000000000000000; matH[6, 3] = -1.523188311911530; //col 5 matH[0, 4] = -1.679897366456105; matH[1, 4] = 2.519846049684157; matH[2, 4] = 0.839948683228052; matH[3, 4] = -4.000000000000000; matH[4, 4] = 6.000000000000000; matH[5, 4] = 2.000000000000000; matH[6, 4] = 3.046376623823059; //col 6 matH[0, 5] = -1.679897366456105; matH[1, 5] = 0.839948683228052; matH[2, 5] = 2.519846049684157; matH[3, 5] = -4.000000000000000; matH[4, 5] = 2.000000000000000; matH[5, 5] = 6.000000000000000; matH[6, 5] = 3.046376623823059; //col 7 matH[0, 6] = -0.639700008449225; matH[1, 6] = 1.279400016898449; matH[2, 6] = 1.279400016898449; matH[3, 6] = -1.523188311911530; matH[4, 6] = 3.046376623823059; matH[5, 6] = 3.046376623823059; matH[6, 6] = 3.480153950315843; for (int k = 0; k < h.Cols; k++) { Console.WriteLine(string.Format("Kolumna {0}", k + 1)); for (int w = 0; w < h.Rows; w++) { decimal dh = Math.Round((decimal)h[w, k], accuracy); decimal dmh = Math.Round((decimal)matH[w, k], accuracy); Console.WriteLine(string.Format("NBN C#: {0}\tMatLab NBN: {1}\t{2}", dh, dmh, dh == dmh ? "OK" : "źle")); } Console.WriteLine(""); } for (int k = 0; k < h.Cols; k++) { for (int w = 0; w < h.Rows; w++) { decimal dh = Math.Round((decimal)h[w, k], accuracy); decimal dmh = Math.Round((decimal)matH[w, k], accuracy); Assert.AreEqual(dmh, dh); } } /* * % Otrzymany hesjan: * % Columns 1 through 6 * % * % 1.058270685684809 -0.705513790456539 -0.705513790456539 2.519846049684157 -1.679897366456105 -1.679897366456105 * % -0.705513790456539 1.058270685684809 0.352756895228269 -1.679897366456105 2.519846049684157 0.839948683228052 * % -0.705513790456539 0.352756895228269 1.058270685684809 -1.679897366456105 0.839948683228052 2.519846049684157 * % 2.519846049684157 -1.679897366456105 -1.679897366456105 6.000000000000000 -4.000000000000000 -4.000000000000000 * % -1.679897366456105 2.519846049684157 0.839948683228052 -4.000000000000000 6.000000000000000 2.000000000000000 * % -1.679897366456105 0.839948683228052 2.519846049684157 -4.000000000000000 2.000000000000000 6.000000000000000 * % -0.639700008449225 1.279400016898449 1.279400016898449 -1.523188311911530 3.046376623823059 3.046376623823059 * % * % Column 7 * % * % -0.639700008449225 * % 1.279400016898449 * % 1.279400016898449 * % -1.523188311911530 * % 3.046376623823059 * % 3.046376623823059 * % 3.480153950315843 */ }
/// <summary> /// Compute psudo hessian matrix and its gradient /// </summary> /// <param name="info">NetworkInfo - information about neural network</param> /// <param name="inp">Input - input data patterns used for learn</param> /// <param name="dout">Output - output data patterns used for learn</param> /// <param name="topo">Topography - neural network topography</param> /// <param name="ww">Weights - weights</param> /// <param name="act">Activation - activation function selection</param> /// <param name="gain">Gain - neuron gain</param> /// <param name="iw">Index - topography indexes</param> public void Compute(ref NetworkInfo info, ref Input inp, ref Output dout, ref Topography topo, Weights ww, ref Activation act, ref Gain gain, ref Index iw) { GradientMat = MatrixMB.Zeros(info.nw, 1); HessianMat = MatrixMB.Zeros(info.nw, info.nw); np = info.np;//number of patterns ni = info.ni;//number of inputs no = info.no;//number of outputs nw = info.nw;//number of weights nn = info.nn;//number of neurons nio = nn + ni - no; zeros = ni.Zeros(); delo = MatrixMB.Zeros(1, nio + 1); J = MatrixMB.Zeros(1, nw); for (p = 0; p < np; p++) { node.Clear(); node.AddRange(inp.Data[p]); CalculateFunctionValuesAndDerivates(ref ww, ref iw, ref topo, ref act, ref gain); for (k = 0; k < no; k++) { o = nio + k; error = dout.Data[p][k] - node[o]; J.ClearWithZeros(); s = iw.Pos(o - ni); J.Data[0][s] = -derivates[o]; delo.ClearWithZeros(); CalculateJacobian(ref ww, ref iw, ref topo); CalculateForHiddenLayer(ref iw, ref topo, ref ww); if (dout[p, 0] > 0.5) J = J * ratio; var JT = J.Transposed; GradientMat = GradientMat + JT * error; HessianMat = HessianMat + JT * J; } } }