Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MATLAB中的神经网络,初始权值_Matlab_Neural Network - Fatal编程技术网

MATLAB中的神经网络,初始权值

MATLAB中的神经网络,初始权值,matlab,neural-network,Matlab,Neural Network,我用newff(…)在MATLAB中制作了神经网络。当您使用相同的输入和输出对其进行训练时,在不同的运行中,训练结果是不同的。我知道这是因为每次我跑步的重量都不一样。我的问题是,如何在每次训练神经网络时使初始权重相同,以便获得相同的结果? 此外,是否有可能从1号训练中节省一些重量,然后再将其用于2号训练,以及如何进行 Tnx要执行此操作,您需要手动将随机数生成器设置为代码开头的相同种子/状态。这可以在a中完成(取决于您拥有的MATLAB版本): 旧式: rand('twister',1234)

我用newff(…)在MATLAB中制作了神经网络。当您使用相同的输入和输出对其进行训练时,在不同的运行中,训练结果是不同的。我知道这是因为每次我跑步的重量都不一样。我的问题是,如何在每次训练神经网络时使初始权重相同,以便获得相同的结果? 此外,是否有可能从1号训练中节省一些重量,然后再将其用于2号训练,以及如何进行

Tnx

要执行此操作,您需要手动将随机数生成器设置为代码开头的相同种子/状态。这可以在a中完成(取决于您拥有的MATLAB版本):

旧式:

rand('twister',1234)
更新的样式:

RandStream.setGlobalStream( RandStream('mt19937ar','Seed',1234) );
R2011a中引入了一种简化最后一次调用的方法:

rng(1234,'twister')

后一种语法是推荐的方法。

作为旁注,不是直接的答案,有一种叫做


根据我的经验,它工作得很好,帮助神经网络更快地收敛。我发现这也使结果更加一致。我建议将其与固定随机种子一起使用。

不同的Matlab神经网络工具箱结果是因为两个原因: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'

你想实际训练神经网络吗?如果在每个训练周期中使用相同的权重(即权重不变),则无法训练神经网络。。。那么你的目标是什么?是的,我确实想训练NN。在我的.m文件中,我创建、训练和模拟NN。但是,当我第三次运行网络时,我获得了网络训练的最佳性能。所以,我的想法是保存第二次运行的权重,并在下次使用它们作为初始权重(因此我不需要连续运行3次)。Chris是对的,仔细检查哪种权重的初始化被用作默认值,因为在最新版本的MATLAB中,默认值不是随机初始化,而是Nguyen Widrow init算法。开始格式化代码时,失去耐心;-)请自己编辑并删除背景
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');