Neural networks in MATLAB, initial weights

I created the Neural Network in MATLAB using newff (...). When you train it with the same inputs and outputs, the learning outcomes are different in different series. I understand that this is because the scales are different for each run. My question is how to make the initial weights the same every time I train my NN to get the same results? In addition, is it possible to save several weights from training No. 1, and the latter to use it for training No. 2 and how?

Tpx

+4
source share
4 answers

, / . ( MATLAB):

:

rand('twister',1234)

:

RandStream.setGlobalStream( RandStream('mt19937ar','Seed',1234) );
R2011a, :
rng(1234,'twister')

- .

+6
+2

Matlab Neural Networks : 1- 2-

"divideblock" "divideint" "dividerand" :

net.dividefcn='divideblock;  net.divideparam.trainratio=.7;  net.divideparam.valratio=.15;  net.divideparam.testratio=.15;

. ( ) Matlab ( "initzero", "initlay", "initwb", "initnw" ) . .

RandStream.setGlobalStream (RandStream ('mrg32k3a','Seed', 1234));

:

net.initFcn='initlay'; net.layers{i}.initFcn='initnw';

+2
source
If you really want to have the weights before and after the training of NN you can use these codes :

for n1=4:8
    wb1=rand(n1,n_input);
    wb2=rand(n_output,n1);
    bb1=rand(n1,1);
    bb2=rand(n_output,1);

    wb=[wb1(:);wb2(:);bb1;bb2]';

    xlswrite(['weight' num2str(n1) '.xlsx'],wb,'Sheet1',num2str(n1));

end


if n1==4
        wb = xlsread(['weight' num2str(n1) '.xlsx']);
        i1 = n1*n_input;
        i2 = n_output*n1;
        i3 = n1;
        i4 = n_output;

        f1=wb(1:i1);
        f2=wb(i1+1:i1+i2);
        f3=wb(i1+i2+1:i1+i2+i3);
        f4=wb(i1+i2+i3+1:i1+i2+i3+i4);

        wb1=reshape(f1,n1,n_input);
        wb2=reshape(f2,n_output,n1);
        bb1=reshape(f3,n1,1);
        bb2=reshape(f4,n_output,1);

    elseif n1==5
        wb=xlsread(['weight' num2str(n1) '.xlsx']);
        i1=n1*n_input;
        i2=n_output*n1;
        i3=n1;
        i4=n_output;

        f1=wb(1:i1);
        f2=wb(i1+1:i1+i2);
        f3=wb(i1+i2+1:i1+i2+i3);
        f4=wb(i1+i2+i3+1:i1+i2+i3+i4);

        wb1=reshape(f1,n1,n_input);
        wb2=reshape(f2,n_output,n1);
        bb1=reshape(f3,n1,1);
        bb2=reshape(f4,n_output,1);

    elseif n1==6
        wb=xlsread(['weight' num2str(n1) '.xlsx']);
        i1=n1*n_input;
        i2=n_output*n1;
        i3=n1;
        i4=n_output;

        f1=wb(1:i1);
        f2=wb(i1+1:i1+i2);
        f3=wb(i1+i2+1:i1+i2+i3);
        f4=wb(i1+i2+i3+1:i1+i2+i3+i4);

        wb1=reshape(f1,n1,n_input);
        wb2=reshape(f2,n_output,n1);
        bb1=reshape(f3,n1,1);
        bb2=reshape(f4,n_output,1);

    elseif n1==7
        wb=xlsread(['weight' num2str(n1) '.xlsx']);
        i1=n1*n_input;
        i2=n_output*n1;
        i3=n1;
        i4=n_output;

        f1=wb(1:i1);
        f2=wb(i1+1:i1+i2);
        f3=wb(i1+i2+1:i1+i2+i3);
        f4=wb(i1+i2+i3+1:i1+i2+i3+i4);

        wb1=reshape(f1,n1,n_input);
        wb2=reshape(f2,n_output,n1);
        bb1=reshape(f3,n1,1);
        bb2=reshape(f4,n_output,1);

    elseif n1==8
        wb=xlsread(['weight' num2str(n1) '.xlsx']);
        i1=n1*n_input;
        i2=n_output*n1;
        i3=n1;
        i4=n_output;

        f1=wb(1:i1);
        f2=wb(i1+1:i1+i2);
        f3=wb(i1+i2+1:i1+i2+i3);
        f4=wb(i1+i2+i3+1:i1+i2+i3+i4);

        wb1=reshape(f1,n1,n_input);
        wb2=reshape(f2,n_output,n1);
        bb1=reshape(f3,n1,1);
        bb2=reshape(f4,n_output,1);
    end

    net = newff(inputs,targets,4,{'tansig','purelin'},'trainlm');
    n.IW{1,1}=wb1;
    n.LW{2,1}=wb2;
    n.b{1}=bb1;
    n.b{2}=bb2;


And after training for saving the network you want :

[net tr] = train(net,inputs,targets);

wb11=n.IW{1,1};
    wb22=n.LW{2,1};
    bb11=n.b{1};
    bb22=n.b{2};

    wbzz=[wb11(:);wb22(:);bb11;bb22]';

    xlswrite('weight.xlsx',wbzz,'Sheet1');
-1
source

All Articles