//------------- constructors -----------// public PerceptronNew( IAlgebraLinear <MatrixType> algebra, int input_dimensions, int output_dimensions, IFunction <double, double> output_function, IFunction <double, double> output_function_derivative) { d_random = new RNGCryptoServiceProvider(); d_algebra = algebra; d_output_function = output_function; d_output_function_derivative = output_function_derivative; layerNumber = nLayers++; //useful for debugging purposes d_input_size = input_dimensions + 1; //input + bias d_output_size = output_dimensions; d_weights = d_algebra.CreateZeros(d_input_size, d_output_size); d_batch_weights = d_algebra.CreateZeros(d_input_size, d_output_size); d_eligibility_traces = d_algebra.CreateZeros(d_input_size, d_output_size); d_input = d_algebra.CreateZeros(1, d_input_size); input_error = d_algebra.CreateZeros(1, d_input_size); d_activation = d_algebra.CreateZeros(1, d_output_size); activation_error = d_algebra.CreateZeros(1, d_output_size); d_output = d_algebra.CreateZeros(1, d_output_size); d_output_error = d_algebra.CreateZeros(1, d_output_size); ClearInputMatrix(); }
public AMatrix <MatrixDataType> Solve(AMatrix <MatrixDataType> A, AMatrix <MatrixDataType> b, AMatrix <MatrixDataType> x0, int max_iter) { // [solution, solutions, errors, norms] = gmres_simple(A, b, x0, kmax, chosen_solution) AMatrix <MatrixDataType> x = x0; AMatrix <MatrixDataType> r = b - (A * x); double rho0 = r.L2Norm(); if (rho0 == 0) { return(x0); } List <int> REPEATED = new List <int>(); List <AMatrix <MatrixDataType> > solutions = new List <AMatrix <MatrixDataType> >(); solutions.Add(x.Transpose()); List <double> errors = new List <double>(); errors.Add(rho0); List <double> norms = new List <double>(); norms.Add(x.L2Norm()); // scale tol for relative residual reduction AMatrix <MatrixDataType> H = algebra.CreateZeros(1, 0); AMatrix <MatrixDataType> V = r / rho0; AMatrix <MatrixDataType> gamma = this.algebra.CreateZeros(1, 1); gamma.SetElement(0, 0, 1); double nu = 1; for (int iteration = 0; iteration < max_iter; iteration++) { Tuple <AMatrix <MatrixDataType>, AMatrix <MatrixDataType> > tuple = ArnoldiStep(A, V, H, REPEATED, -1); V = tuple.Item1; H = tuple.Item2; // compute the nul vector of H' AMatrix <MatrixDataType> gk = ((gamma * H.GetColumnSection(0, iteration + 1, iteration)) / H.GetElement(iteration + 1, iteration)); gamma = gamma.AppendColumns(gk * -1.0); // Compute the residual norm nu = nu + (gk.Transpose() * gk).GetElement(); errors.Add(rho0 / Math.Sqrt(nu)); // compute explicit residual every step int k1 = H.ColumnCount; AMatrix <MatrixDataType> e = this.algebra.CreateZeros(iteration + 2, 1); e.SetElement(0, 0, 1); AMatrix <MatrixDataType> y = simple_solver.Solve(H, e); AMatrix <MatrixDataType> x1 = V.GetColumns(0, iteration + 1) * (y * rho0); solutions.Add(x1); norms.Add(x1.L2Norm()); } return(solutions[solutions.Count - 1]); // compute the approximate solution //solution = solutions(chosen_solution,:); }