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

发布于 2024-12-10 06:03:27 字数 1436 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

悲凉≈ 2024-12-17 06:03:27

生成可重现的结果 ,您需要在代码开头手动将随机数生成器设置为相同的种子/状态。这可以通过数量来完成方式(取决于您拥有的 MATLAB 版本):

旧样式:

rand('twister',1234)

更新样式:

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

A 新函数,简化了最后一次调用:

rng(1234,'twister')

推荐使用后一种语法。

To generate reproducible results, you need to manually set the random number generator to the same seed/state at the beginning of the code. This can be done in a number of ways (depending on what version of MATLAB you have):

The old style:

rand('twister',1234)

The updated style:

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

A new function was introduced in R2011a that simplifies the last call:

rng(1234,'twister')

The latter syntax is the recommended approach.

隱形的亼 2024-12-17 06:03:27

作为旁注,而不是直接答案,有一个叫做 Nguyen Widrow初始化并已在 Matlab 的神经网络工具箱中实现

根据我的经验,它效果很好,可以帮助神经网络更快地收敛。我发现它也使结果更加一致。我建议按照 Amro 的帖子<使用它以及固定随机种子/a>.

As a side note, and not a direct answer, there's something called Nguyen Widrow initialization and it's already implemented in Matlab's Neural Net toolbox.

In my experience it works pretty well and helps the neural net converge faster. I've found that it also makes the results more consistent too. I recommend using it as well as the fixed random seed as per Amro's post.

嗼ふ静 2024-12-17 06:03:27

Matlab 神经网络工具箱结果不同有两个原因: 1-随机数据除法 2-随机权重初始化

对于不同的数据除法问题,使用函数“divideblock”或“divideint”而不是“dividerand”,如下所示:

net.dividefcn ='分割块;
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';

Different Matlab Neural networks toolbox results is because of two reasons: 1-random data division 2-random weight initialization

For different data division problem use function "divideblock" or "divideint" instead of "dividerand" like this:

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

For random weight initialization problem, It seems (I'm not sure) all Matlab initialization functions ("initzero", "initlay”, "initwb”, “initnw”) are almost random. So you should force this functions produce similar results per call.

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

And then use one of them:

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

晨光如昨 2024-12-17 06:03:27
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');
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');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文