我正在尝试进行基本的神经网络模拟。它由神经元和神经元连接组成。在下面的代码中,神经元 2 的值应该在每次更新神经元 1 的值时发生变化:
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());
}
}
现在,我只得到默认值“0”。
这是 Neuron 和 NeuronConncetion 对象的代码:
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;
}
}
问题是:每当我改变神经元1的输入时,如何让神经元2改变它的值?