I am very interested in ai and wanted to create my own, so I watched some videos and found a good library. For my first ai, it is very simple. I did this to get a grip on the library and how it works for more complex things in the future. All this ai is supposed to do is get an input of four small numbers, and the output is only 1 if all four inputs are 1. (dont worry about the AI and AIType classes) here is the code, please let me know if i am doing something wrong because i've looked in a lot of places and couldn't find anything:
AI aitest = new AI(AIType.FIRST_AI);
DataSet ds = new DataSet(4,1);
ds.add(new double[]{0,0,0,0},new double[]{0});
ds.add(new double[]{0,0,0,1},new double[]{0});
ds.add(new double[]{0,0,1,0},new double[]{0});
ds.add(new double[]{1,1,0,0},new double[]{0});
ds.add(new double[]{1,0.1,1,3},new double[]{0});
ds.add(new double[]{1,1,1,1},new double[]{1});
ds.add(new double[]{1,1,1,1},new double[]{1});
ds.add(new double[]{0,1,2,5},new double[]{0});
Layer l = new Layer();
InputLayer il = new InputLayer(4);
aitest.getAI().getNN().setInputNeurons(il.getNeurons());
for(int ignored :new int[]{1,2,3,4}){
Neuron n = new Neuron();
for(Neuron all: aitest.getAI().getNN().getInputNeurons()) {
n.addInputConnection(all);
}
l.addNeuron(n);
}
aitest.getAI().getNN().addLayer(l);
aitest.getAI().train(ds);
It throws a NPE on the last line with this error:
Exception in thread "main" java.lang.NullPointerException
at org.neuroph.core.NeuralNetwork.learn(NeuralNetwork.java:297)
at me.ai.aicore.SuperAI.train(SuperAI.java:31) // these last two lines just call the .learn method
at me.ai.Core.main(Core.java:38)
(there are more error lines but those aren't relevant)
The line in neuroph (NeuralNetwork class) which this error originates is marked:
public void setWeights(double[] weights) {
int i = 0;
Iterator var3 = this.layers.iterator();
while(var3.hasNext()) {
Layer layer = (Layer)var3.next();
Iterator var5 = layer.getNeurons().iterator();
while(var5.hasNext()) {
Neuron neuron = (Neuron)var5.next();
// the line below is where the error comes from
for(Iterator var7 = neuron.getInputConnections().iterator(); var7.hasNext(); ++i) {
Connection conn = (Connection)var7.next();
conn.getWeight().setValue(weights[i]);
}
}
}
}
All help is appreciated.
EDIT: I added the full stacktrace, but everything besides the first like is useless as its just the ones calling methods. by the way, the bottom codeblock is from the library. I updated it to show the full method