Java Code Examples for org.neuroph.nnet.MultiLayerPerceptron#randomizeWeights()

The following examples show how to use org.neuroph.nnet.MultiLayerPerceptron#randomizeWeights() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MomentumBackPropagationTest.java    From NeurophFramework with Apache License 2.0 7 votes vote down vote up
@Test
public void testXorMSE() {
    MultiLayerPerceptron myMlPerceptron = new MultiLayerPerceptron(TransferFunctionType.SIGMOID, 2, 3, 1);
    myMlPerceptron.randomizeWeights(new WeightsRandomizer(new Random(123)));

    myMlPerceptron.setLearningRule(instance);
    myMlPerceptron.learn(xorDataSet);

    MeanSquaredError mse = new MeanSquaredError();
    for (DataSetRow testSetRow : xorDataSet.getRows()) {
        myMlPerceptron.setInput(testSetRow.getInput());
        myMlPerceptron.calculate();
        double[] networkOutput = myMlPerceptron.getOutput();
        mse.addPatternError(networkOutput, testSetRow.getDesiredOutput());
    }
    assertTrue(mse.getTotalError() < maxError);
}
 
Example 2
Source File: BackPropagationTest.java    From NeurophFramework with Apache License 2.0 7 votes vote down vote up
@Test
public void testXorMSE() {
    MultiLayerPerceptron myMlPerceptron = new MultiLayerPerceptron(TransferFunctionType.SIGMOID, 2, 3, 1);
    myMlPerceptron.randomizeWeights(new WeightsRandomizer(new Random(123)));

    myMlPerceptron.setLearningRule(instance);
    myMlPerceptron.learn(xorDataSet);

    MeanSquaredError mse = new MeanSquaredError();
    for (DataSetRow testSetRow : xorDataSet.getRows()) {
        myMlPerceptron.setInput(testSetRow.getInput());
        myMlPerceptron.calculate();
        double[] networkOutput = myMlPerceptron.getOutput();
        mse.addPatternError(networkOutput, testSetRow.getDesiredOutput());
    }
    assertTrue(mse.getTotalError() < maxError);
}
 
Example 3
Source File: MomentumBackPropagationTest.java    From NeurophFramework with Apache License 2.0 5 votes vote down vote up
@Test
public void testXorMaxError() {
    MultiLayerPerceptron myMlPerceptron = new MultiLayerPerceptron(TransferFunctionType.SIGMOID, 2, 3, 1);
    myMlPerceptron.randomizeWeights(new WeightsRandomizer(new Random(123)));

    myMlPerceptron.setLearningRule(instance);
    myMlPerceptron.learn(xorDataSet);

    assertTrue(instance.getTotalNetworkError() < maxError);
}
 
Example 4
Source File: MomentumBackPropagationTest.java    From NeurophFramework with Apache License 2.0 5 votes vote down vote up
@Test
public void testIrisMaxError() {
    MultiLayerPerceptron myMlPerceptron = new MultiLayerPerceptron(4, 16, 3);
    myMlPerceptron.randomizeWeights(new WeightsRandomizer(new Random(123)));
    myMlPerceptron.setLearningRule(instance);

    myMlPerceptron.learn(irisDataSet);

    assertTrue(instance.getTotalNetworkError() < maxError);
}
 
Example 5
Source File: BackPropagationTest.java    From NeurophFramework with Apache License 2.0 5 votes vote down vote up
@Test
public void testXorMaxError() {
    MultiLayerPerceptron myMlPerceptron = new MultiLayerPerceptron(TransferFunctionType.SIGMOID, 2, 3, 1);
    myMlPerceptron.randomizeWeights(new WeightsRandomizer(new Random(123)));

    myMlPerceptron.setLearningRule(instance);
    myMlPerceptron.learn(xorDataSet);

    assertTrue(instance.getTotalNetworkError() < maxError);
}
 
Example 6
Source File: BackPropagationTest.java    From NeurophFramework with Apache License 2.0 5 votes vote down vote up
@Test
public void testIrisMaxError() {
    MultiLayerPerceptron myMlPerceptron = new MultiLayerPerceptron(4, 16, 3);
    myMlPerceptron.randomizeWeights(new WeightsRandomizer(new Random(123)));
    myMlPerceptron.setLearningRule(instance);

    myMlPerceptron.learn(irisDataSet);

    assertTrue(instance.getTotalNetworkError() < maxError);
}
 
Example 7
Source File: RandomizationSample.java    From NeurophFramework with Apache License 2.0 5 votes vote down vote up
/**
 * Runs this sample
 */
public static void main(String[] args) {

    MultiLayerPerceptron neuralNet = new MultiLayerPerceptron(2, 3, 1);
    // neuralNet.randomizeWeights(new WeightsRandomizer());
    // neuralNet.randomizeWeights(new RangeRandomizer(0.1, 0.9));
    // neuralNet.randomizeWeights(new GaussianRandomizer(0.4, 0.3));
    neuralNet.randomizeWeights(new NguyenWidrowRandomizer(0.3, 0.7));
    printWeights(neuralNet);

    neuralNet.randomizeWeights(new DistortRandomizer(0.5));
    printWeights(neuralNet);
}
 
Example 8
Source File: XorMultiLayerPerceptronSample.java    From NeurophFramework with Apache License 2.0 4 votes vote down vote up
/**
     * Runs this sample
     */
    public void run() {

        // create training set (logical XOR function)
        DataSet trainingSet = new DataSet(2, 1);
        trainingSet.add(new DataSetRow(new double[]{0, 0}, new double[]{0}));
        trainingSet.add(new DataSetRow(new double[]{0, 1}, new double[]{1}));
        trainingSet.add(new DataSetRow(new double[]{1, 0}, new double[]{1}));
        trainingSet.add(new DataSetRow(new double[]{1, 1}, new double[]{0}));

        // create multi layer perceptron
        MultiLayerPerceptron myMlPerceptron = new MultiLayerPerceptron(TransferFunctionType.SIGMOID, 2, 3, 1);
        myMlPerceptron.randomizeWeights(new WeightsRandomizer(new Random(123)));

        System.out.println(Arrays.toString(myMlPerceptron.getWeights()));

        myMlPerceptron.setLearningRule(new BackPropagation());

        myMlPerceptron.getLearningRule().setLearningRate(0.5);
        // enable batch if using MomentumBackpropagation
//        if( myMlPerceptron.getLearningRule() instanceof MomentumBackpropagation )
//        	((MomentumBackpropagation)myMlPerceptron.getLearningRule()).setBatchMode(false);

        LearningRule learningRule = myMlPerceptron.getLearningRule();
        learningRule.addListener(this);

        // learn the training set
        System.out.println("Training neural network...");
        myMlPerceptron.learn(trainingSet);

        // test perceptron
        System.out.println("Testing trained neural network");
        testNeuralNetwork(myMlPerceptron, trainingSet);

        // save trained neural network
        myMlPerceptron.save("myMlPerceptron.nnet");

        // load saved neural network
        NeuralNetwork loadedMlPerceptron = NeuralNetwork.createFromFile("myMlPerceptron.nnet");

        // test loaded neural network
        System.out.println("Testing loaded neural network");
        testNeuralNetwork(loadedMlPerceptron, trainingSet);
    }