首页 文章

MATLAB中的神经网络

提问于
浏览
6

我在Matlab中训练了xor神经网络并得到了这些权重:

iw: [-2.162 2.1706; 2.1565 -2.1688]

lw: [-3.9174 -3.9183]

b{1} [2.001; 2.0033]

b{2} [3.8093]

仅仅是出于好奇,我试图编写MATLAB代码来计算这个网络的输出(隐藏层中的2个神经元,输出中的1个,TANSIG激活函数) .

我得到的代码:

l1w = [-2.162 2.1706; 2.1565 -2.1688];
l2w = [-3.9174 -3.9183];
b1w = [2.001 2.0033];
b2w = [3.8093];

input = [1, 0];

out1 = tansig (input(1)*l1w(1,1) + input(2)*l1w(1,2) + b1w(1));
out2 = tansig (input(1)*l1w(2,1) + input(2)*l1w(2,2) + b1w(2));
out3 = tansig (out1*l2w(1) + out2*l2w(2) + b2w(1))

问题是当输入是[1,1]时,它输出 -0.9989 ,当[0,1] 0.4902 时 . 模拟用MATLAB输出生成的网络是充分的 0.000558750.99943 .

我做错了什么?

2 回答

  • -1

    我写了一个简单的XOR网络示例 . 我使用 newpr ,默认为隐藏和输出层的 tansig 传递函数 .

    input = [0 0 1 1; 0 1 0 1];               %# each column is an input vector
    ouputActual = [0 1 1 0];
    
    net = newpr(input, ouputActual, 2);       %# 1 hidden layer with 2 neurons
    net.divideFcn = '';                       %# use the entire input for training
    
    net = init(net);                          %# initialize net
    net = train(net, input, ouputActual);     %# train
    outputPredicted = sim(net, input);        %# predict
    

    然后我们通过自己计算输出来检查结果 . 要记住的重要一点是,默认情况下,输入/输出是[-1,1]范围的 scaled

    scaledIn = (2*input - 1);           %# from [0,1] to [-1,1]
    for i=1:size(input,2)
        in = scaledIn(:,i);             %# i-th input vector
        hidden(1) = tansig( net.IW{1}(1,1)*in(1) + net.IW{1}(1,2)*in(2) + net.b{1}(1) );
        hidden(2) = tansig( net.IW{1}(2,1)*in(1) + net.IW{1}(2,2)*in(2) + net.b{1}(2) );
        out(i) = tansig( hidden(1)*net.LW{2,1}(1) + hidden(2)*net.LW{2,1}(2) + net.b{2} );
    end
    scaledOut = (out+1)/2;              %# from [-1,1] to [0,1]
    

    或更有效地表示为一行中的矩阵产品:

    scaledIn = (2*input - 1);           %# from [0,1] to [-1,1]
    out = tansig( net.LW{2,1} * tansig( net.IW{1}*scaledIn + repmat(net.b{1},1,size(input,2)) ) + repmat(net.b{2},1,size(input,2)) );
    scaledOut = (1 + out)/2;            %# from [-1,1] to [0,1]
    
  • 10

    你通常不在输出层使用sigmoid - 你确定你应该在out3上使用tansig吗?您确定要查看经过适当培训的网络的权重吗?看起来你有一个训练过的网络在[1,1] [1,-1] [-1,1]和[-1,-1]上做XOR,其中1表示“xor”和-1含义“相同” .

相关问题