How can I train a neural (pattern recognition) network multiple times in matlab?

783 views Asked by At

I need to train a pattern recognition network in matlab. I have several datasets which shall be used for training. My script looks like this:

%%% train network with a couple of datasets
pathStr = 'Daten_Training';
files = dir(sprintf('%s/*.mat',pathStr)); 

for k = 1:length(files)

    %%% load data for training
    load(sprintf('%s/%s',pathStr, files(k).name));

    %%% manually set targets to train the network with
    Targets = setTargets(Data);

    %%% create and train neural network
    % Create a Pattern Recognition Network
    hiddenLayerSize = 20;
    net = patternnet(hiddenLayerSize);

    % Train the network with our Data
    net = trainNetwork(net,Data,Targets);

end

The trainNetwork function looks like this:

function [ net ] = trainNetwork( net, Data, Targets )

    % calculate features
    [Features, TargetsBlock, blockIdx] = calcFeatures_Training(Data, Targets);

    % split data for training
    net.divideParam.trainRatio = 70/100;
    net.divideParam.valRatio = 15/100;
    net.divideParam.testRatio = 15/100;

    % Train the network
    [net, tr] = train(net, Features, TargetsBlock);

end

Is there a way to train multiple times with the same result as if I would use one training with all datasets in a row? For now it looks like the network is just retrained with the new data and everything before is lost.

1

There are 1 answers

0
vazsonyidl On

Do not now it is actual or not, but maybe help for someone.

You can train a network only one times. If you train again, it is going to be a new network. :) The weights going to be different. If you give the same name, MATLAB going to overwrite every time you run the script.

The best way - i think - to do this is :

  1. Train your network with your own inputs,layers,and options.
  2. Save your network into a file with the following function : save('myFilename.mat','myNetname');
  3. Load your net from the file : load('myFilename.mat');
  4. Change the inputs - maybe you have multiple datasets.
  5. Update weights on your net , and comment the train option (%)! Do not train the network again. You can update with the following function : myNetname = predictAndUpdateState(myNetname,someInput);

Hope this helps for somebody:)