我试着做基本的神经网络模拟。它由神经元和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更改其值?
发布于 2020-08-03 12:02:58
我为你提供了一个简单的解决方案。您只需在Neuron类中保留对Neuron的引用,并从addInput()或Neuron class的addMultipleInputs()方法中添加方法setConnection()并调用NeuronConnection's update() method(稍后我们将在NeuronConnection中添加)。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);
}
}现在,主要的课程,没有变化.
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());
}
}我还没有测试代码,但它会让你走上正确的轨道.
告诉我,如果你有什么问题.
https://stackoverflow.com/questions/63228302
复制相似问题