0

I am writing a neural net in Java, and I'm having trouble with an ArrayList.get() not returning a variable.

    double train(ArrayList<Double> inputVector, double desiredOutput) {
    double result = output(inputVector);
    double error = desiredOutput - result;
    double delta = learningRate_ * error * result * (1.0 - result);
    for (int i = 0; i < outputLayer_.weights_.size(); i++) {
        outputLayer_.weights_.get(i).doubleValue() += delta * lastOutput_.get(i);
    }
    ArrayList<Double> hiddenDelta = new ArrayList<Double>();
    for (int j = 0; j < hiddenLayer_.size(); j++) {
        double HiddenDelta = delta * outputLayer_.weights_.get(j + 1) * lastOutput_.get(j + 1) * (1 - lastOutput_.get(j + 1));
        for (int l = 0; l < hiddenLayer_.get(j).weights_.size(); l++) {
            hiddenLayer_.get(j).weights_.get(l) += HiddenDelta * inputVector.get(l);
        }
    }

The Error I get is on both of the lines that try to +=, each inside a for loop (1st and third) It tells me that a variable is expected. I am using JDK 8, with Intellij. I used the following to create the weights list:

ArrayList<Double> weights_ = new ArrayList<Double>();

Every List is initialized in a similar form.

3
  • 2
    Do you see a difference between get and set? Commented Jun 11, 2014 at 22:35
  • This is not allowed. First get the value then add any value in it. Commented Jun 11, 2014 at 22:35
  • I tried using: hiddenLayer_.get(j).weights_.get(l).doubleValue() += HiddenDelta * inputVector.get(l); And it gave me the same error. Commented Jun 11, 2014 at 22:39

1 Answer 1

6

The expression outputLayer_.weights_.get(i).doubleValue() is readOnly. You cannot assign a value to it.

First place its value in a variable:

double newWeight = outputLayer_.weights_.get(i).doubleValue() + delta * lastOutput_.get(i);

Then assign the new calculated value:

outputLayer_.weights_.set(i, newWeight);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.