我正在尝试进行基本的神经网络仿真。它由神经元和神经元连接组成。在下面的代码中,每当神经元1的值更新时,神经元2的值都应更改:
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;
}
}
问题是:每当我更改Neuron1的输入时,如何使Neuron2更改其值?
答案 0 :(得分:0)
我正在为您提供一个简单的解决方案。您只需要在NeuronConnection
类中保留对Neuron
的引用,并添加方法setConnection()并从{调用NeuronConnection's update() method
(稍后将在NeuronConnection
中添加) addInput()
的{1}}或addMultipleInputs()
方法。 Neuron class
类的新设计:
Neuron
现在,重新设计您的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;
}
}
类:
NeuronConnection
现在,主班级没有变化...
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);
}
}
我还没有测试代码,但是它应该让您走上正确的路...
让我知道,如果您有任何问题...