setValue() public method

public setValue ( int index, double value ) : void
index int
value double
return void
        public void testFeedForwardAndBAckLoopWorksWithMomentum()
        {
            // example 11.14 of Neural Network Design by Hagan, Demuth and Beale
            Matrix hiddenLayerWeightMatrix = new Matrix(2, 1);
            hiddenLayerWeightMatrix.set(0, 0, -0.27);
            hiddenLayerWeightMatrix.set(1, 0, -0.41);

            Vector hiddenLayerBiasVector = new Vector(2);
            hiddenLayerBiasVector.setValue(0, -0.48);
            hiddenLayerBiasVector.setValue(1, -0.13);

            Vector input = new Vector(1);
            input.setValue(0, 1);

            Matrix outputLayerWeightMatrix = new Matrix(1, 2);
            outputLayerWeightMatrix.set(0, 0, 0.09);
            outputLayerWeightMatrix.set(0, 1, -0.17);

            Vector outputLayerBiasVector = new Vector(1);
            outputLayerBiasVector.setValue(0, 0.48);

            Vector error = new Vector(1);
            error.setValue(0, 1.261);

            double learningRate = 0.1;
            double momentumFactor = 0.5;
            FeedForwardNeuralNetwork ffnn = new FeedForwardNeuralNetwork(
                    hiddenLayerWeightMatrix, hiddenLayerBiasVector,
                    outputLayerWeightMatrix, outputLayerBiasVector);

            ffnn.setTrainingScheme(new BackPropLearning(learningRate,
                    momentumFactor));
            ffnn.processInput(input);
            ffnn.processError(error);

            Matrix finalHiddenLayerWeights = ffnn.getHiddenLayerWeights();
            Assert.AreEqual(-0.2675, finalHiddenLayerWeights.get(0, 0), 0.001);
            Assert.AreEqual(-0.4149, finalHiddenLayerWeights.get(1, 0), 0.001);

            Vector hiddenLayerBias = ffnn.getHiddenLayerBias();
            Assert.AreEqual(-0.4775, hiddenLayerBias.getValue(0), 0.001);
            Assert.AreEqual(-0.1349, hiddenLayerBias.getValue(1), 0.001);

            Matrix finalOutputLayerWeights = ffnn.getOutputLayerWeights();
            Assert.AreEqual(0.1304, finalOutputLayerWeights.get(0, 0), 0.001);
            Assert.AreEqual(-0.1235, finalOutputLayerWeights.get(0, 1), 0.001);

            Vector outputLayerBias = ffnn.getOutputLayerBias();
            Assert.AreEqual(0.6061, outputLayerBias.getValue(0), 0.001);
        }
Example #2
0
 public Vector plus(Vector v)
 {
     Vector result = new Vector(size());
     for (int i = 0; i < size(); i++)
     {
         result.setValue(i, getValue(i) + v.getValue(i));
     }
     return result;
 }
Example #3
0
 public Vector copyVector()
 {
     Vector result = new Vector(getRowDimension());
     for (int i = 0; i < getRowDimension(); i++)
     {
         result.setValue(i, getValue(i));
     }
     return result;
 }
Example #4
0
        public Vector plus(Vector v)
        {
            Vector result = new Vector(size());

            for (int i = 0; i < size(); i++)
            {
                result.setValue(i, getValue(i) + v.getValue(i));
            }
            return(result);
        }
Example #5
0
        public Vector copyVector()
        {
            Vector result = new Vector(getRowDimension());

            for (int i = 0; i < getRowDimension(); i++)
            {
                result.setValue(i, getValue(i));
            }
            return(result);
        }
Example #6
0
        public void testFeedForward()
        {
            // example 11.14 of Neural Network Design by Hagan, Demuth and Beale
            // lots of tedious tests necessary to ensure nn is fundamentally correct
            Matrix weightMatrix1 = new Matrix(2, 1);
            weightMatrix1.set(0, 0, -0.27);
            weightMatrix1.set(1, 0, -0.41);

            Vector biasVector1 = new Vector(2);
            biasVector1.setValue(0, -0.48);
            biasVector1.setValue(1, -0.13);

            Layer layer1 = new Layer(weightMatrix1, biasVector1,
                    new LogSigActivationFunction());

            Vector inputVector1 = new Vector(1);
            inputVector1.setValue(0, 1);

            Vector expected = new Vector(2);
            expected.setValue(0, 0.321);
            expected.setValue(1, 0.368);

            Vector result1 = layer1.feedForward(inputVector1);
            Assert.AreEqual(expected.getValue(0), result1.getValue(0), 0.001);
            Assert.AreEqual(expected.getValue(1), result1.getValue(1), 0.001);

            Matrix weightMatrix2 = new Matrix(1, 2);
            weightMatrix2.set(0, 0, 0.09);
            weightMatrix2.set(0, 1, -0.17);

            Vector biasVector2 = new Vector(1);
            biasVector2.setValue(0, 0.48);

            Layer layer2 = new Layer(weightMatrix2, biasVector2,
                    new PureLinearActivationFunction());
            Vector inputVector2 = layer1.getLastActivationValues();
            Vector result2 = layer2.feedForward(inputVector2);
            Assert.AreEqual(0.446, result2.getValue(0), 0.001);
        }
Example #7
0
        public void testSensitivityMatrixCalculationFromErrorVector()
        {
            Matrix weightMatrix1 = new Matrix(2, 1);
            weightMatrix1.set(0, 0, -0.27);
            weightMatrix1.set(1, 0, -0.41);

            Vector biasVector1 = new Vector(2);
            biasVector1.setValue(0, -0.48);
            biasVector1.setValue(1, -0.13);

            Layer layer1 = new Layer(weightMatrix1, biasVector1,
                    new LogSigActivationFunction());

            Vector inputVector1 = new Vector(1);
            inputVector1.setValue(0, 1);

            layer1.feedForward(inputVector1);

            Matrix weightMatrix2 = new Matrix(1, 2);
            weightMatrix2.set(0, 0, 0.09);
            weightMatrix2.set(0, 1, -0.17);

            Vector biasVector2 = new Vector(1);
            biasVector2.setValue(0, 0.48);

            Layer layer2 = new Layer(weightMatrix2, biasVector2,
                    new PureLinearActivationFunction());
            Vector inputVector2 = layer1.getLastActivationValues();
            layer2.feedForward(inputVector2);

            Vector errorVector = new Vector(1);
            errorVector.setValue(0, 1.261);
            LayerSensitivity layer2Sensitivity = new LayerSensitivity(layer2);
            layer2Sensitivity.sensitivityMatrixFromErrorMatrix(errorVector);

            Matrix sensitivityMatrix = layer2Sensitivity.getSensitivityMatrix();
            Assert.AreEqual(-2.522, sensitivityMatrix.get(0, 0), 0.0001);
        }
Example #8
0
	public Vector feedForward(Vector inputVector) {
		lastInput = inputVector;
		Matrix inducedField = weightMatrix.times(inputVector).plus(biasVector);

		Vector inducedFieldVector = new Vector(numberOfNeurons());
		for (int i = 0; i < numberOfNeurons(); i++) {
			inducedFieldVector.setValue(i, inducedField.get(i, 0));
		}

		lastInducedField = inducedFieldVector.copyVector();
		Vector resultVector = new Vector(numberOfNeurons());
		for (int i = 0; i < numberOfNeurons(); i++) {
			resultVector.setValue(i, activationFunction
					.activation(inducedFieldVector.getValue(i)));
		}
		// set the result as the last activation value
		lastActivationValues = resultVector.copyVector();
		return resultVector;
	}
Example #9
0
        public void testWeightsAndBiasesUpdatedCorrectly()
        {
            Matrix weightMatrix1 = new Matrix(2, 1);
            weightMatrix1.set(0, 0, -0.27);
            weightMatrix1.set(1, 0, -0.41);

            Vector biasVector1 = new Vector(2);
            biasVector1.setValue(0, -0.48);
            biasVector1.setValue(1, -0.13);

            Layer layer1 = new Layer(weightMatrix1, biasVector1,
                    new LogSigActivationFunction());
            LayerSensitivity layer1Sensitivity = new LayerSensitivity(layer1);

            Vector inputVector1 = new Vector(1);
            inputVector1.setValue(0, 1);

            layer1.feedForward(inputVector1);

            Matrix weightMatrix2 = new Matrix(1, 2);
            weightMatrix2.set(0, 0, 0.09);
            weightMatrix2.set(0, 1, -0.17);

            Vector biasVector2 = new Vector(1);
            biasVector2.setValue(0, 0.48);

            Layer layer2 = new Layer(weightMatrix2, biasVector2,
                    new PureLinearActivationFunction());
            Vector inputVector2 = layer1.getLastActivationValues();
            layer2.feedForward(inputVector2);

            Vector errorVector = new Vector(1);
            errorVector.setValue(0, 1.261);
            LayerSensitivity layer2Sensitivity = new LayerSensitivity(layer2);
            layer2Sensitivity.sensitivityMatrixFromErrorMatrix(errorVector);

            layer1Sensitivity
                    .sensitivityMatrixFromSucceedingLayer(layer2Sensitivity);

            BackPropLearning.calculateWeightUpdates(layer2Sensitivity, layer1
                    .getLastActivationValues(), 0.1);

            BackPropLearning.calculateBiasUpdates(layer2Sensitivity, 0.1);

            BackPropLearning.calculateWeightUpdates(layer1Sensitivity,
                    inputVector1, 0.1);

            BackPropLearning.calculateBiasUpdates(layer1Sensitivity, 0.1);

            layer2.updateWeights();
            Matrix newWeightMatrix2 = layer2.getWeightMatrix();
            Assert.AreEqual(0.171, newWeightMatrix2.get(0, 0), 0.001);
            Assert.AreEqual(-0.0772, newWeightMatrix2.get(0, 1), 0.001);

            layer2.updateBiases();
            Vector newBiasVector2 = layer2.getBiasVector();
            Assert.AreEqual(0.7322, newBiasVector2.getValue(0), 0.00001);

            layer1.updateWeights();
            Matrix newWeightMatrix1 = layer1.getWeightMatrix();

            Assert.AreEqual(-0.265, newWeightMatrix1.get(0, 0), 0.001);
            Assert.AreEqual(-0.419, newWeightMatrix1.get(1, 0), 0.001);

            layer1.updateBiases();
            Vector newBiasVector1 = layer1.getBiasVector();

            Assert.AreEqual(-0.475, newBiasVector1.getValue(0), 0.001);
            Assert.AreEqual(-0.139, newBiasVector1.getValue(1), 0.001);
        }
Example #10
0
        public void testBiasUpdateMatrixesFormedCorrectly()
        {
            Matrix weightMatrix1 = new Matrix(2, 1);
            weightMatrix1.set(0, 0, -0.27);
            weightMatrix1.set(1, 0, -0.41);

            Vector biasVector1 = new Vector(2);
            biasVector1.setValue(0, -0.48);
            biasVector1.setValue(1, -0.13);

            Layer layer1 = new Layer(weightMatrix1, biasVector1,
                    new LogSigActivationFunction());
            LayerSensitivity layer1Sensitivity = new LayerSensitivity(layer1);

            Vector inputVector1 = new Vector(1);
            inputVector1.setValue(0, 1);

            layer1.feedForward(inputVector1);

            Matrix weightMatrix2 = new Matrix(1, 2);
            weightMatrix2.set(0, 0, 0.09);
            weightMatrix2.set(0, 1, -0.17);

            Vector biasVector2 = new Vector(1);
            biasVector2.setValue(0, 0.48);

            Layer layer2 = new Layer(weightMatrix2, biasVector2,
                    new PureLinearActivationFunction());
            LayerSensitivity layer2Sensitivity = new LayerSensitivity(layer2);
            Vector inputVector2 = layer1.getLastActivationValues();
            layer2.feedForward(inputVector2);

            Vector errorVector = new Vector(1);
            errorVector.setValue(0, 1.261);
            layer2Sensitivity.sensitivityMatrixFromErrorMatrix(errorVector);

            layer1Sensitivity
                    .sensitivityMatrixFromSucceedingLayer(layer2Sensitivity);

            Vector biasUpdateVector2 = BackPropLearning.calculateBiasUpdates(
                    layer2Sensitivity, 0.1);
            Assert.AreEqual(0.2522, biasUpdateVector2.getValue(0), 0.001);

            Vector lastBiasUpdateVector2 = layer2.getLastBiasUpdateVector();
            Assert.AreEqual(0.2522, lastBiasUpdateVector2.getValue(0), 0.001);

            Vector penultimateBiasUpdateVector2 = layer2
                    .getPenultimateBiasUpdateVector();
            Assert.AreEqual(0.0, penultimateBiasUpdateVector2.getValue(0),
                    0.001);

            Vector biasUpdateVector1 = BackPropLearning.calculateBiasUpdates(
                    layer1Sensitivity, 0.1);
            Assert.AreEqual(0.00495, biasUpdateVector1.getValue(0), 0.001);
            Assert.AreEqual(-0.00997, biasUpdateVector1.getValue(1), 0.001);

            Vector lastBiasUpdateVector1 = layer1.getLastBiasUpdateVector();

            Assert.AreEqual(0.00495, lastBiasUpdateVector1.getValue(0), 0.001);
            Assert.AreEqual(-0.00997, lastBiasUpdateVector1.getValue(1), 0.001);

            Vector penultimateBiasUpdateVector1 = layer1
                    .getPenultimateBiasUpdateVector();
            Assert.AreEqual(0.0, penultimateBiasUpdateVector1.getValue(0),
                    0.001);
            Assert.AreEqual(0.0, penultimateBiasUpdateVector1.getValue(1),
                    0.001);
        }
Example #11
0
	private static void initializeVector(Vector aVector, double lowerLimit,
			double upperLimit) {
		for (int i = 0; i < aVector.size(); i++) {

			double random = Util.generateRandomDoubleBetween(lowerLimit,
					upperLimit);
			aVector.setValue(i, random);
		}
	}
Example #12
0
	public void updateBiases() {
		Matrix biasMatrix = biasVector.plusEquals(lastBiasUpdateVector);
		Vector result = new Vector(biasMatrix.getRowDimension());
		for (int i = 0; i < biasMatrix.getRowDimension(); i++) {
			result.setValue(i, biasMatrix.get(i, 0));
		}
		biasVector = result;
	}
Example #13
0
        public Vector calculateBiasUpdates(LayerSensitivity layerSensitivity,
                double alpha, double momentum)
        {
            Layer layer = layerSensitivity.getLayer();
            Matrix biasUpdateMatrixWithoutMomentum = layerSensitivity
                    .getSensitivityMatrix().times(alpha).times(-1.0);

            Matrix biasUpdateMatrixWithMomentum = layer.getLastBiasUpdateVector()
                    .times(momentum).plus(
                            biasUpdateMatrixWithoutMomentum.times(1.0 - momentum));
            Vector result = new Vector(biasUpdateMatrixWithMomentum
                    .getRowDimension());
            for (int i = 0; i < biasUpdateMatrixWithMomentum.getRowDimension(); i++)
            {
                result.setValue(i, biasUpdateMatrixWithMomentum.get(i, 0));
            }
            layer.acceptNewBiasUpdate(result.copyVector());
            return result;
        }
Example #14
0
        public static Vector calculateBiasUpdates(
                LayerSensitivity layerSensitivity, double alpha)
        {
            Layer layer = layerSensitivity.getLayer();
            Matrix biasUpdateMatrix = layerSensitivity.getSensitivityMatrix()
                    .times(alpha).times(-1.0);

            Vector result = new Vector(biasUpdateMatrix.getRowDimension());
            for (int i = 0; i < biasUpdateMatrix.getRowDimension(); i++)
            {
                result.setValue(i, biasUpdateMatrix.get(i, 0));
            }
            layer.acceptNewBiasUpdate(result.copyVector());
            return result;
        }