0

I am trying to code a simple backpropagation network in Matlab, and I am getting the following error:

Subscript indices must either be real positive integers
or logicals.

in line 144 of my code, which is during this section:

for l = 1:net.layerCount,
         if l == 1, % From input layer
             for i = round(1:net.inputSize),
                 i % i = 1 and
                 l % l = 1 when I get the error.
                 % error in this next line:
                 net.weight{l}(i,:) = net.weight{l}(i,:) ...
                       - sum(lrate .* net.delta{l} .* net.layerOutput{l-1}(i)) ...
                       - (momentum .* net.previousWeightDelta{l}(i,:));
                 net.previousWeightDelta{l}(i,:) = net.weight{l}(i,:);
             end
         else
            for i = 1:net.layerSize{l-1},
                net.weight{l}(i,:) = net.weight{l}(i,:) ...
                       - sum(lrate .* net.delta{l} .* net.layerOutput{l-1}(i)) ...
                       - (momentum .* net.previousWeightDelta{l}(i,:));
                net.previousWeightDelta{l}(i,:) = net.weight{l}(i,:);
            end
         end
     end

The error persists even if I surround 1:net.layerCount and the other loop vectors with round(). Any idea why this is the case?

Thanks!

David Myers
  • 103
  • 1
  • 1
    Which line returns this error? "144" doesn't tell much here… – liori Jun 12 '14 at 20:15
  • `dbstop if error`, and `which` everything in that line to check you haven't got any stray shadowing variables - I'd put money on `sum` being a variable here if your i/l values are what you say they are. See the dupe. – nkjt Jun 13 '14 at 08:54

1 Answers1

2

In the l == 1 case, you illegally try to use

net.layerOutput{l-1}

0 is not positive.

The input layer needs to use the inputs, not inter-layer connections.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • 1
    Gosh thank you. This is why you don't copy paste code. – David Myers Jun 12 '14 at 20:35
  • 2
    @DavidMyers: In the future, use "divide and conquer". Whatever line or expression is failing, put small pieces of it into the interactive command window, until you find which one is the problem. – Ben Voigt Jun 12 '14 at 20:42