matlab 单层神经网络,matlab – 单层神经网络

我可以发现代码中的一些问题.主要问题是目标是

multi-class(而不是

binary),因此您需要为每个类使用一个输出节点(称为

1-of-N encoding),或者使用具有不同

activation function的单个输出节点(某些功能不仅仅是二进制)输出-1/1或0/1)

在下面的解决方案中,perceptron具有以下结构:

%# load your data

input = [

0.832 64.643

0.818 78.843

1.776 45.049

0.597 88.302

1.412 63.458

];

target = [

0 0 1

0 0 1

0 1 0

0 0 1

0 0 1

];

%# parameters of the learning algorithm

LEARNING_RATE = 0.1;

MAX_ITERATIONS = 100;

MIN_ERROR = 1e-4;

[numInst numDims] = size(input);

numClasses = size(target,2);

%# three output nodes connected to two-dimensional input nodes + biases

weights = randn(numClasses, numDims+1);

isDone = false; %# termination flag

iter = 0; %# iterations counter

while ~isDone

iter = iter + 1;

%# for each instance

err = zeros(numInst,numClasses);

for i=1:numInst

%# compute output: Y = W*X + b, then apply threshold activation

output = ( weights * [input(i,:)';1] >= 0 ); %#'

%# error: err = T - Y

err(i,:) = target(i,:)' - output; %#'

%# update weights (delta rule): delta(W) = alpha*(T-Y)*X

weights = weights + LEARNING_RATE * err(i,:)' * [input(i,:) 1]; %#'

end

%# Root mean squared error

rmse = sqrt(sum(err.^2,1)/numInst);

fprintf(['Iteration %d: ' repmat('%f ',1,numClasses) '\n'], iter, rmse);

%# termination criteria

if ( iter >= MAX_ITERATIONS || all(rmse < MIN_ERROR) )

isDone = true;

end

end

%# plot points and one-against-all decision boundaries

[~,group] = max(target,[],2); %# actual class of instances

gscatter(input(:,1), input(:,2), group), hold on

xLimits = get(gca,'xlim'); yLimits = get(gca,'ylim');

for i=1:numClasses

ezplot(sprintf('%f*x + %f*y + %f', weights(i,:)), xLimits, yLimits)

end

title('Perceptron decision boundaries')

hold off

您提供的五个样本的培训结果:

Iteration 1: 0.447214 0.632456 0.632456

Iteration 2: 0.000000 0.447214 0.447214

...

Iteration 49: 0.000000 0.447214 0.447214

Iteration 50: 0.000000 0.632456 0.000000

Iteration 51: 0.000000 0.447214 0.000000

Iteration 52: 0.000000 0.000000 0.000000

请注意,上例中使用的数据仅包含5个样本.如果每个班级有更多的培训实例,您将获得更有意义的结果.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值