0

I know how to code using java but I'm new on Neuroph I got an VectorSizeMismatchException with these codes.

Main: https://pastebin.com/dntWRMZN

public static void main(String[] args) {
    AiManager.trainNeuralNetwork(AiManager.initilizeNetwork());
}

Manager: https://pastebin.com/csWsiVvt

import org.neuroph.core.Layer;
import org.neuroph.core.NeuralNetwork;
import org.neuroph.core.Neuron;
import org.neuroph.core.data.DataSet;
import org.neuroph.core.data.DataSetRow;
import org.neuroph.nnet.Perceptron;
import org.neuroph.util.ConnectionFactory;
import org.neuroph.util.NeuralNetworkType;

public class AiManager {

  public static NeuralNetwork<?> initilizeNetwork() {
    Layer inputLayer = new Layer();
    inputLayer.addNeuron(new Neuron());
    inputLayer.addNeuron(new Neuron());

    Layer hiddenLayerOne = new Layer();
    hiddenLayerOne.addNeuron(new Neuron());
    hiddenLayerOne.addNeuron(new Neuron());
    hiddenLayerOne.addNeuron(new Neuron());
    hiddenLayerOne.addNeuron(new Neuron());

    Layer hiddenLayerTwo = new Layer();
    hiddenLayerTwo.addNeuron(new Neuron());
    hiddenLayerTwo.addNeuron(new Neuron());
    hiddenLayerTwo.addNeuron(new Neuron());
    hiddenLayerTwo.addNeuron(new Neuron());

    Layer outputLayer = new Layer();
    outputLayer.addNeuron(new Neuron());

    NeuralNetwork<?> ann = new Perceptron(2, 1);

    ann.setInputNeurons(inputLayer.getNeurons());
    ann.setOutputNeurons(outputLayer.getNeurons());

    ann.addLayer(0, inputLayer);
    ann.addLayer(1, hiddenLayerOne);

    ConnectionFactory.fullConnect(ann.getLayerAt(0), ann.getLayerAt(1));

    ann.addLayer(2, hiddenLayerTwo);

    ConnectionFactory.fullConnect(ann.getLayerAt(1), ann.getLayerAt(2));

    ann.addLayer(3, outputLayer);

    ConnectionFactory.fullConnect(ann.getLayerAt(2), ann.getLayerAt(3));

    ConnectionFactory.fullConnect(ann.getLayerAt(0), ann.getLayerAt(ann.getLayersCount()-1), false);

    ann.setNetworkType(NeuralNetworkType.MULTI_LAYER_PERCEPTRON);

    ann.setInputNeurons(inputLayer.getNeurons());
    ann.setOutputNeurons(outputLayer.getNeurons());

    return ann;
  }

  public static NeuralNetwork<?> trainNeuralNetwork(NeuralNetwork<?> ann) {
    int inputSize = 2;
    int outputSize = 1;
    DataSet ds = new DataSet(inputSize, outputSize);

    DataSetRow rOne = new DataSetRow(new double[] { 0, 1 }, new double[] { 1 });

    ds.addRow(rOne);

    DataSetRow rTwo = new DataSetRow(new double[] { 1, 1 }, new double[] { 0 });

    ds.addRow(rTwo);

    DataSetRow rThree = new DataSetRow(new double[] { 0, 0 }, new double[] { 0 });

    ds.addRow(rThree);

    DataSetRow rFour = new DataSetRow(new double[] { 1, 0 }, new double[] { 1 });

    ds.addRow(rFour);

    ann.learn(ds);

    return ann;
  }
}
Teocci
  • 7,189
  • 1
  • 50
  • 48
PleaseHelpMe
  • 1
  • 1
  • 3
  • Welcome to SO :). Please edit your question and paste the text of your code in the question. Also, you should have a look at how to produce a [minimal example](https://stackoverflow.com/help/minimal-reproducible-example) of the problem you're having. – Nanhydrin Jun 05 '19 at 08:20
  • Welcome to Stack Overflow! I edited your question to inline the Exception you got, so more people with knowledge of the subject will see it. I also added and specified the language of a fenced code block - please see the [editing help for more information on formatting](https://stackoverflow.com/editing-help) Good luck! – Teocci Jun 05 '19 at 08:44

1 Answers1

0

This error is due to how the Neuroph API is coded, as you can see you added twice the expression ann.setInputNeurons(inputLayer.getNeurons()); and ann.setOutputNeurons(outputLayer.getNeurons());, but if you print the expression ann.getInputNeurons().size() before and after these expression are called, you will see that each time you "set" the input neurons, you add new neurons.

See the source of the NeuralNetwork class:

/**
 * Sets input neurons
 *
 * @param inputNeurons array of input neurons
 */
public void setInputNeurons(List<Neuron> inputNeurons) {
    for (Neuron neuron : inputNeurons) {
        this.inputNeurons.add(neuron);
    }
}

Theoretically removing these 4 lines should remove the error.