2

I am training an ANN, and I want to have different instances of training. In each instance, I want to find the maximum difference between the actual and predicted output. Then I want to take the average of all these maximums.

My code so far is:

maximum = []; 
k=1;

for k = 1:5
  %Train network
  layers = [ ...
    imageInputLayer([250 1 1])
    reluLayer
    fullyConnectedLayer(100)
    fullyConnectedLayer(100)
    fullyConnectedLayer(1)
    regressionLayer];

  options = trainingOptions('sgdm','InitialLearnRate',0.1, ...
    'MaxEpochs',1000);
  net = trainNetwork(nnntrain,nnnfluidtrain,layers,options);
  net.Layers

  %Test network
  predictedn = predict(net,nnntest);
  maximum = append(maximum, max(abs(predictedn-nnnfluidtest)));

  k=k+1
end

My intent is to produce a list named 'maximum' with five elements (the max of each ANN training instance) that I would then like to take the average of.

However, it keeps giving me the error:

wrong number of input arguments for obsolete matrix-based syntax

when it tries to append. The first input is a list while the second is a 1x1 single.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
ngc1300
  • 138
  • 7
  • `nnntrain` and `nnntest` are not changed at each iteration in your loop, so you would be getting the same results per iteration. I'm unsure as to why you're doing this when you specifically mentioned that different training and test examples appear per iteration. – rayryeng Apr 10 '18 at 20:32
  • I have trained and tested the same data sets on different ocassions and it does not give the same exact number. The weights start randomized each time, so when you apply the gradient descent method it might find a slightly different minimum each time. – ngc1300 Apr 10 '18 at 20:45
  • Right, but what is the purpose of that? Are you simply trying to see what effect the initial weights would be in minimizing the objective function? – rayryeng Apr 10 '18 at 20:47
  • just trying to get a better statistical measure of the performance of the ANN – ngc1300 Apr 10 '18 at 20:50

1 Answers1

2

Appending in MATLAB is a native operation. You append elements by actually building a new vector where the original vector is part of the input.

Therefore:

maximum = [maximum max(abs(predictedn-nnnfluidtest))];

If for some reason you would like to do it in function form, the function you are looking for is cat which is short form for concatenate. The append function is seen in multiple toolboxes but each one of them does not do what you want. cat is what you want but you still need to provide the original input vector as part of the arguments:

maximum = cat(2, maximum, max(abs(predictedn-nnnfluidtest)));

The first argument is the axis you want to append to. To respect the code that you're doing above, you want the columns to increase as you extend your vector so that is the second axis, or the axis being 2.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • You might want to add a note that pre-allocating a matrix is much more efficient runtime-wise than to append matrices like this. Even in unknown-length `while` loops I like to pre-allocate every 1000 steps at least to cut down on computational cost. – Adriaan Apr 10 '18 at 20:38
  • 1
    @Adriaan Good point. Thanks. I didn't preallocate because I am unsure as to the size of the expected outputs. I don't know if it's just a single scalar value or if it's a vector. If you examine `nnntest` and `predictedn`, they are variables with no definition in the code so preallocating would be guess work. Until I get more information from the OP, I'll leave it as concatenating for now. – rayryeng Apr 10 '18 at 20:41
  • I didn't provide the whole code, just the part I needed help with. nnntest and predictedn are defined – ngc1300 Apr 10 '18 at 20:47
  • @PatrickMcAtee In that case, then the above should work. Let me know if it does. – rayryeng Apr 10 '18 at 20:48
  • Then we'll just wait for @PatrickMcAtee to come back with another problem that their code is very slow. Thanks for the explanation Ray – Adriaan Apr 10 '18 at 20:59
  • @Adriaan lmao. no problem. – rayryeng Apr 10 '18 at 20:59