0

我正在尝试进行基本的神经网络模拟。它由神经元和神经元连接组成。在下面的代码中,神经元 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改变它的值?

4

1 回答 1

0

我为您提供了一个简单的解决方案。您只需要NeuronConnectionNeuron类中保留对的引用并添加一个方法 setConnection() 并调用NeuronConnection's update() method(我们将在稍后添加NeuronConnection) from addInput()oraddMultipleInputs()的方法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);
    }
}

现在,Main 类,没有变化……

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());
    }
}

我还没有测试过代码,但它应该能让你走上正确的轨道......

让我知道,如果你有任何问题...

于 2020-08-03T12:02:58.717 回答