public void XorTest() { NeuralNetwork network = new NeuralNetwork(2, 1, 1, 1); float[,] xorInput = new float[4, 2] { { 0, 0 }, { 0, 1 }, { 1, 0 }, { 1, 1 } }; float[,] xorOutput = new float[4, 1] { { 1 }, { 0 }, { 0 }, { 1 } }; SparseMatrix X = new SparseMatrix(SparseCompressedRowMatrixStorage <float> .OfArray(xorInput)); SparseMatrix Y = new SparseMatrix(SparseCompressedRowMatrixStorage <float> .OfArray(xorOutput)); network.LearnNetwork(X, Y, 5000, 0.001f, 0); network.Inputs = DenseVector.OfArray(new float[2] { 0, 0 }); network.ForwardPropagation(); var test = network.GetAswer(); Assert.IsNotNull(test); Assert.AreEqual(Math.Round(test[0]), 1); }
public static MatrixStorage <T> MatrixStorage <T>(TestMatrixStorage type, T[,] data) where T : struct, IEquatable <T>, IFormattable { switch (type) { case TestMatrixStorage.DenseMatrix: return(DenseColumnMajorMatrixStorage <T> .OfArray(data)); case TestMatrixStorage.SparseMatrix: return(SparseCompressedRowMatrixStorage <T> .OfArray(data)); case TestMatrixStorage.DiagonalMatrix: return(DiagonalMatrixStorage <T> .OfArray(data)); default: throw new NotSupportedException(); } }
// Apenas um teste, não é pra ser executado pela aplicação /* * public void GeraImagem() * { * String im = ".\\SaidaProcessadaVetor.txt"; * StreamReader sr = new StreamReader(im); * * double[] temp = new double[3600]; * using (sr) * { * string line; * int i = 0; * while ((line = sr.ReadLine()) != null) * { * temp[i] = Double.Parse(line); * i++; * } * } * * new GeraBitmap().ToBitmap(temp, im.Replace(".\\", "")); * } */ /// <summary> /// Separar essa função em 3: lê vetor, lê matriz e CGNE /// </summary> public void CGNE() { try { int rows = 50816; int columns = 3600; //var path = HttpContext.Current.Server.MapPath("~/Content/Signals/"); string hFile = path + "H-1.txt"; string gFile = path + @"Sent\" + this.filaDeProcessos.First(); double intensidade = Double.Parse(gFile.Split('#').GetValue(1).ToString()); var M = Matrix <double> .Build; var V = Vector <double> .Build; StreamReader sr = new StreamReader(gFile); var g = V.Dense(rows); /* * Separar para função de vetor * */ try { System.Diagnostics.Debug.WriteLine(DateTime.Now); double[] temp = new double[rows]; using (sr) { string line; int i = 0; while ((line = sr.ReadLine()) != null) { if (intensidade != 1) { temp[i] = Double.Parse(line) * intensidade; } else { temp[i] = Double.Parse(line); } i++; } } g = V.Dense(temp); System.Diagnostics.Debug.WriteLine(DateTime.Now + " -> Arquivo g lido"); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); System.Diagnostics.Debug.WriteLine(e.StackTrace); } sr.Dispose(); sr = new StreamReader(hFile); /* * Separar para função de matriz * */ try { //var f = CGNECall(hFile, rows, columns, g); System.Diagnostics.Debug.WriteLine(DateTime.Now + " -> começou o CGNE"); Matrix <double> h; using (StreamReader reader = new StreamReader(File.OpenRead(hFile))) { string line; int i = 0; double[,] matrixTemp = new double[rows, columns]; while ((line = sr.ReadLine()) != null) { string[] tokens = line.Split(','); int j = 0; foreach (var token in tokens) { matrixTemp[i, j] = Double.Parse(token); j++; } i++; } /* * var blasMatrix = new BlasMatrix(reader, columns); * int i = 0; * double[,] matrixTemp = new double[rows, columns]; * foreach (var element in blasMatrix.Records) * { * for (int j = 0; j < columns; j++) * { * matrixTemp[i, j] = element.getRow(0, j); * } * i++; * } */ System.Diagnostics.Debug.WriteLine(DateTime.Now + "Arquivo H lido"); sr.DiscardBufferedData(); reader.DiscardBufferedData(); reader.Dispose(); sr.Dispose(); h = M.Sparse(SparseCompressedRowMatrixStorage <double> .OfArray(matrixTemp)); //h = M.DenseOfArray(matrixTemp); /* * Usando Dense dobra o uso de memória (é feita uma cópia de matrixTemp) * * h = M.Dense(DenseColumnMajorMatrixStorage<double>.OfArray(matrixTemp)) * */ } sr.Dispose(); /* * Aqui são as operações de inicialização */ Vector <double> f = V.Dense(columns); Vector <double> f_aux = V.Dense(columns); //var r = g - h.Multiply(f); var r = g; Vector <double> r_aux; var hT = h.Transpose(); var p = hT.Multiply(r); Vector <double> p_aux; for (int i = 0; i < 15; i++) { var r_T = r.ToRowMatrix(); var alfa_upper = r_T.Multiply(r); var p_T = p.ToRowMatrix(); var alfa_down = p_T.Multiply(p); var alfa = alfa_upper.PointwiseDivide(alfa_down); double alfa_scalar = alfa.Single(); f_aux += f.Add(p.Multiply(alfa_scalar)); f = f_aux; r_aux = r.Subtract(h.Multiply(alfa_scalar).Multiply(p)); var r_auxT = r_aux.ToRowMatrix(); var beta_upper = r_auxT.Multiply(r_aux); var beta = beta_upper.PointwiseDivide(alfa_upper); System.Diagnostics.Debug.WriteLine(DateTime.Now + " -> before matrix 2"); double beta_scalar = beta.Single(); p_aux = hT.Multiply(r_aux); var pplus = p_aux.Add(p.Multiply(beta_scalar)); p = pplus; r = r_aux; System.Diagnostics.Debug.WriteLine(DateTime.Now + $" -> iteração {i}"); } string sinal = this.filaDeProcessos.First(); //COMENTAR ESSA LINHA PARA OTIMIZAR salvaVetor(sinal, f_aux.ToArray(), @"Processed\"); // Remove da fila this.filaDeProcessos.Remove(sinal); new GeraBitmap().ToBitmap(f_aux.ToArray(), sinal, pathImagens); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); System.Diagnostics.Debug.WriteLine(e.StackTrace); } sr.Dispose(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("The file could not be read:"); System.Diagnostics.Debug.WriteLine(e.Message); } }