[英]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.