matlab中的神经网络,MATLAB中的神经网络

我写了一个简单的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]范围:

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个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值