public UnifiedCameraNeuralNetwork(int inputLayerSize, int hiddenLayerSize, int outputLayerSize, float learningRate, bool isStochastic = true, float momentum = 0) { // Network functional parameters _nonLinearityFunc = sigmoid; _nonLinearityFuncDeriv = sigmoidDerivative; _errorFunction = meanSquareError; _errorFunctionDerv = meanSquareErrorDerivative; // Network initialization this.LearningRate = learningRate; _momentum = momentum; _inputLayerSize = inputLayerSize; _hiddenLayerSize = hiddenLayerSize; _outputLayerSize = outputLayerSize; _input = new Matrix(inputLayerSize + 1, 1); // Vector (+ 1 for bias) _expected = new Matrix(outputLayerSize, 1); // Vector // Add 1 for bias _hiddenLayerWeights = new Matrix(_hiddenLayerSize, _inputLayerSize + 1); _outputLayerWeights = new Matrix(_outputLayerSize, _hiddenLayerSize + 1); _prevHiddenLayerWeightsDelta = new Matrix(_hiddenLayerSize, _inputLayerSize + 1); _prevOutputLayerWeightsDelta = new Matrix(_outputLayerSize, _hiddenLayerSize + 1); _isStochastic = isStochastic; _hiddenWeightUpdates = new Matrix(_hiddenLayerSize, _inputLayerSize + 1); _outputWeightUpdates = new Matrix(_outputLayerSize, _hiddenLayerSize + 1); _weightUpdatesCount = 0; _random = new Random(RANDOM_SEED); randomizeWeights(); }
public static void runMatrixTest() { Matrix mat1 = new Matrix(3, 3); mat1[0, 0] = 1; mat1[0, 1] = 0; mat1[0, 2] = 0; mat1[1, 0] = 0; mat1[1, 1] = 1; mat1[1, 2] = 0; mat1[2, 0] = 0; mat1[2, 1] = 0; mat1[2, 2] = 1; Matrix mat2 = new Matrix(3, 3); mat2[0, 0] = 1; mat2[0, 1] = 0; mat2[0, 2] = 0; mat2[1, 0] = 0; mat2[1, 1] = 1; mat2[1, 2] = 0; mat2[2, 0] = 0; mat2[2, 1] = 0; mat2[2, 2] = 1; if (!mat1.Equals(mat2)) { Console.WriteLine("Equal test #1 failed"); return; } mat2[0, 0] = 0; if (mat1.Equals(mat2)) { Console.WriteLine("Equal test #2 failed"); return; } if (!mat1.Equals(Matrix.identity(3, 3)) || mat2.Equals(Matrix.identity(3, 3))) { Console.WriteLine("Equal test #3 failed"); return; } float[] vals = new float[9]; for (int i = 0; i < 9; i++) { vals[i] = i; } Matrix matVals = new Matrix(vals); Matrix matClone = new Matrix(matVals); if (!matVals.Equals(matClone)) { Console.WriteLine("Copy constructor failed"); return; } Matrix mul = new Matrix(1, 9); mul = mul.transpose(); mul.init(vals); if (!mul.Equals(matClone)) { Console.WriteLine("Init failed"); return; } matVals = new Matrix(3, 3); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { matVals[i, j] = i * 3 + j; } } matClone = new Matrix(matVals); mul = matVals.mul(mat1); if (!mul.Equals(matClone)) { Console.WriteLine("Mul with identity failed"); return; } mul = matVals.mul(matClone); if (mul.Equals(matClone)) { Console.WriteLine("Mul with non-identity failed"); return; } // Debug window zone Matrix add = matClone + matVals; Matrix sub = matClone - matVals; Matrix transpose = matClone.transpose(); Matrix mulScalar = matClone * 0.5f; Matrix dot = matClone.dot(matVals); Matrix.MatrixPerElementOperation testfunc1 = testMatrixPerElementOperation; Matrix.MatrixPerElementProduct testfunc2 = testMatrixProduct; Matrix invoke1 = matClone.invoke(testfunc1); Matrix invoke2 = matClone.invoke(testfunc2, matVals); Console.WriteLine("Test ended"); }