[英]How do I make object to update its value, based on other objects value change, in Java?
我正在尝试进行基本的神经网络模拟。 它由 Neurons 和 NeuronConnections 组成。 在下面的代码中,neuron2 的值应该在 neuron1 的值更新时发生变化:
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 改变它的值?
我正在为您提供一个简单的解决方案。 您只需在Neuron
class 中保留对NeuronConnection
的引用,并添加一个方法 setConnection() 并从Neuron class
class 的addInput()
或addMultipleInputs()
方法调用NeuronConnection's update() method
(我们稍后将在NeuronConnection
中添加)。 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;
}
}
现在,重新设计您的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);
}
}
现在,Main class,没有变化......
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());
}
}
我没有测试代码,但它应该让你走上正确的轨道......
让我知道,如果你有任何问题...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.