【问题标题】:Neural Networks in MATLAB, initial weights [closed]MATLAB 中的神经网络,初始权重
【发布时间】:2011-12-09 10:18:35
【问题描述】:

我在 MATLAB 中使用 newff(...) 制作了神经网络。当您使用相同的输入和输出对其进行训练时,不同运行的训练结果会有所不同。我知道它正在发生,因为每次运行它时权重都不同。我的问题是每次训练我的神经网络时如何使初始权重相同,这样我才能得到相同的结果? 另外,是否可以从训练 No1 中节省一些权重,然后将其用于训练 No2,以及如何?

Tnx

【问题讨论】:

  • 你想实际训练神经网络吗?如果您在每个训练周期中使用相同的权重(即权重不变),那么您就无法训练神经网络……那么您的目标是什么?
  • 是的,我确实想训练神经网络。在我的 .m 文件中,我创建、训练和模拟 NN。但是当我第三次运行它时,我得到了网络训练的最佳性能。所以,我的想法是保存第二次运行的权重,并在下次使用它们作为初始权重(所以我不需要连续运行 3 次)。

标签: matlab neural-network


【解决方案1】:

generate reproducible results,需要在代码开头手动将随机数生成器设置为相同的种子/状态。这可以在number of ways 中完成(取决于您拥有的 MATLAB 版本):

旧式:

rand('twister',1234)

更新后的样式:

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

R2011a 中引入了new function,简化了最后一次调用:

rng(1234,'twister')

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

【讨论】:

    【解决方案2】:

    作为旁注,而不是直接答案,有一个叫做Nguyen Widrow initialization and it's already implemented in Matlab's Neural Net toolbox 的东西。

    根据我的经验,它运行良好,可以帮助神经网络更快地收敛。我发现它也使结果更加一致。我建议按照Amro's post 使用它以及固定随机种子。

    【讨论】:

    • Chris 是对的,请仔细检查默认使用哪种权重初始化,因为在 MATLAB 的最新版本中,默认不是随机初始化,而是 Nguyen Widrow 初始化算法。
    【解决方案3】:

    不同的 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';

    【讨论】:

    • 开始格式化您的代码,失去了耐心 ;-) 请自己编辑并删除反引号
    【解决方案4】:
    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');
    

    【讨论】:

      猜你喜欢
      • 2020-02-08
      • 1970-01-01
      • 2014-10-30
      • 2021-06-25
      • 2019-10-09
      • 2015-06-29
      • 2019-04-10
      • 1970-01-01
      • 2020-03-28
      相关资源
      最近更新 更多