I'm trying to make basic neural.network simulation. It consists of Neurons and NeuronConnections. In code below, the value of neuron2, should change whenever value of neuron1 is updated:
public class Main {
public static void main(String[] args) {
Neuron neuron1 = new Neuron();
Neuron neuron2 = new Neuron();
NeuronConnection neuronConnection = new NeuronConnection(neuron1, neuron2);
neuron1.addInput(20);
System.out.println(neuron2.getOutput());
}
}
For now, I get just "0" which is default value.
Here is code for Neuron and NeuronConncetion objects:
public class Neuron {
private double output;
private List<Double> inputArray;
public Neuron() {
output = 0;
inputArray = new LinkedList<>();
}
public Neuron (double input) {
inputArray = new LinkedList<>();
inputArray.add(input);
output += input;
}
public void addInput(double input) {
inputArray.add(input);
output += input;
}
public void addMultipleInputs(List<Double> inputs) {
inputArray.addAll(inputs);
for (double input: inputs) {
output += input;
}
}
public double getOutput() {
return output;
}
}
public class NeuronConnection {
private double weight;
private Neuron inNeuron;
private Neuron outNeuron;
private double outValue;
public NeuronConnection(Neuron inNeuron, Neuron outNeuron) {
this.inNeuron = inNeuron;
this.outNeuron = outNeuron;
weight = Math.random();
outValue = inNeuron.getOutput()*weight;
outNeuron.addInput(outValue);
}
public double getOutValue() {
return outValue;
}
}
The questions is : How do I make neuron2 change its value, whenever I change input of neuron1?
I am providing you a simple solution. You just have to keep a reference to NeuronConnection
in Neuron
class and add a method setConnection() and call NeuronConnection's update() method
(which we'll add later in NeuronConnection
) from addInput()
or addMultipleInputs()
method of Neuron class
. The new design of Neuron
class:
public class Neuron {
private NeuronConnection conn;
private double output;
private List<Double> inputArray;
public Neuron() {
output = 0;
inputArray = new LinkedList<>();
}
public Neuron (double input) {
inputArray = new LinkedList<>();
inputArray.add(input);
output += input;
}
public void addInput(double input) {
inputArray.add(input);
output += input;
conn.update();
}
public void addMultipleInputs(List<Double> inputs) {
inputArray.addAll(inputs);
for (double input: inputs) {
output += input;
}
conn.update();
}
public double getOutput() {
return output;
}
// i've added this method
public void setConnection(NeuronConnection conn) {
this.conn = conn;
}
}
Now, redesign your NeuronConnection
class:
public class NeuronConnection {
private double weight;
private Neuron inNeuron;
private Neuron outNeuron;
private double outValue;
public NeuronConnection(Neuron inNeuron, Neuron outNeuron) {
this.inNeuron = inNeuron;
this.outNeuron = outNeuron;
// now, setConnection
this.inNeuron.setConnection(this);
this.outNeuron.setConnection(this);
weight = Math.random();
outValue = inNeuron.getOutput()*weight;
outNeuron.addInput(outValue);
}
public double getOutValue() {
return outValue;
}
// i've added this
public void update() {
// this calculation is little flawed
// you've to edit/fix this as you think it will be
// perfect for your neural network
outValue = inNeuron.getOutput() * weight;
outNeuron.addInput(outValue);
}
}
Now, the Main class, there's no change...
public class Main {
public static void main(String[] args) {
Neuron neuron1 = new Neuron();
Neuron neuron2 = new Neuron();
NeuronConnection neuronConnection = new NeuronConnection(neuron1, neuron2);
neuron1.addInput(20);
System.out.println(neuron2.getOutput());
}
}
I haven't tested the code, but it should get you on the right track...
Let me know, if you've any question...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.