public static void DActivIdentity(Vector x, Vector v, ref Vector derivatives) { if (v == null || derivatives == null || v.Dimension < x.Dimension || derivatives.Dimension < x.Dimension) throw new Exception("Neural.MLPerceptron2.ALayer.DActivIdentity()"); derivatives.FillWith(1); }
public void Build(int in_dim, int[] layer_size, CellType[] types) { #region validating arguments if (in_dim < 0 || layer_size.Length < 1 || layer_size.Length != types.Length) { throw new Exception(this.GetType().ToString() + ".Build: wrong arguments"); } if (layer_size.Any(t => t < 1)) { throw new Exception(this.GetType().ToString() + ".Build: nonpositive layer size"); } #endregion #region initializing structures InputAverage = new Vector(in_dim); InputAverage.FillWith(0); InputInvStddev = new Vector(in_dim); InputInvStddev.FillWith(1); Input = new Vector(in_dim); Layer = new ALayer[layer_size.Length]; int layer_input_dim = in_dim; for (int l = 0; l < Layer.Length; l++) { Layer[l] = new ALayer(); Layer[l].Build(layer_input_dim, types[l], layer_size[l]); layer_input_dim = Layer[l].Output.Dimension; } int out_dim = layer_size[layer_size.Length - 1]; dL_dOutput = new Vector(out_dim); #endregion AfterConstruction(); }
// product of vector and matrix public static Vector operator *(Vector v, Matrix m) { int rows = m.Height; int cols = m.Width; if (v.Dimension != rows) throw new Exception("Computing.Vector.operator*(Vector,Matrix)"); var ret = new Vector(cols); ret.FillWith(0); for (int j = 0; j < cols; j++) for (int i = 0; i < rows; i++) ret[j] += m[i, j] * v[i]; return (ret); }
public static Vector operator *(Matrix m, Vector v) { int ret_dim=m.Height; int v_dim=v.Dimension; if (v_dim!=m.Width) throw (new System.Exception("Multiplication of matrix and vector impossible due to dimensions")); Vector ret = new Vector(ret_dim); ret.FillWith(0); for (int i=0; i<ret_dim; i++) for (int j=0; j<v_dim; j++) ret[i] += m[i, j] * v[j]; return (ret); }
public static void Inverse(Matrix A, ref Matrix invA) { int i,j,dim = A.Height; if (dim<1 || dim!=A.Width) throw new Exception("Dimensions do not fit"); Matrix.AssureDimensions(ref invA, dim,dim); Vector b = new Vector(dim), x = null; b.FillWith(0); for (j=0; j<dim; j++) { b[j] = 1; LinearEquationGaussElimination(A, ref x, b); for (i=0; i<dim; i++) invA[i,j] = x[i]; b[j] = 0; } }