首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使对象在Java中根据其他对象值的变化来更新其值?

如何使对象在Java中根据其他对象值的变化来更新其值?
EN

Stack Overflow用户
提问于 2020-08-03 11:06:40
回答 1查看 39关注 0票数 0

我试着做基本的神经网络模拟。它由神经元和NeuronConnections组成。在下面的代码中,只要更新了neuron2的值,neuron1的值就会改变:

代码语言:javascript
复制
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对象的代码:

代码语言:javascript
复制
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;
    }
}
代码语言:javascript
复制
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更改其值?

EN

回答 1

Stack Overflow用户

发布于 2020-08-03 12:02:58

我为你提供了一个简单的解决方案。您只需在Neuron类中保留对Neuron的引用,并从addInput()Neuron classaddMultipleInputs()方法中添加方法setConnection()并调用NeuronConnection's update() method(稍后我们将在NeuronConnection中添加)。Neuron类的新设计:

代码语言:javascript
复制
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类:

代码语言:javascript
复制
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);
    }
}

现在,主要的课程,没有变化.

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63228302

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档