单层神经网络 [英] Single layer neural network

查看:160
本文介绍了单层神经网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于实现单层神经网络,我有两个数据文件.

For the implementation of single layer neural network, I have two data files.

In:
    0.832 64.643
    0.818 78.843

Out:
    0 0 1
    0 0 1

上面是2个数据文件的格式.

The above is the format of 2 data files.

对于相应输入所属的特定类别,目标输出为"1",对于其余2个输出,目标输出为"0".

The target output is "1" for a particular class that the corresponding input belongs to and "0" for the remaining 2 outputs.

问题如下:

您的单层神经网络将 找出A(3 x 2矩阵)和b(3 x 1 向量)在Y = A * X + b中,其中Y为[C1, C2,C3]',X为[x1,x2]'.

Your single layer neural network will find A (3 by 2 matrix) and b (3 by 1 vector) in Y = A*X + b where Y is [C1, C2, C3]' and X is [x1, x2]'.

要解决上述问题,请使用 神经网络,我们可以重写 等式如下:Y = A'* X'其中 A'= [A b](3 x 3矩阵),X'为 [x1,x2,1]'

To solve the problem above with a neural network, we can re-write the equation as follow: Y = A' * X' where A' = [A b] (3 by 3 matrix) and X' is [x1, x2, 1]'

现在您可以将神经网络用于 三个输入节点(一个用于x1,x2和 1个)和三个输出(C1, C2,C3).

Now you can use a neural network with three input nodes (one for x1, x2, and 1 respectively) and three outputs (C1, C2, C3).

结果9(因为我们有9 3个输入和3个之间的连接 输出)权重将等于 A'矩阵的元素.

The resulting 9 (since we have 9 connections between 3 inputs and 3 outputs) weights will be equivalent to elements of A' matrix.

基本而言,我正在尝试执行类似操作,但无法正常工作:

Basicaly, I am trying to do something like this, but it is not working:

function neuralNetwork   
    load X_Q2.data
    load T_Q2.data
    x = X_Q2(:,1);
    y = X_Q2(:,2);

    learningrate = 0.2;
    max_iteration = 50;

    % initialize parameters
    count = length(x);
    weights = rand(1,3); % creates a 1-by-3 array with random weights
    globalerror = 0;
    iter = 0;
    while globalerror ~= 0 && iter <= max_iteration
        iter = iter + 1;
        globalerror = 0;
        for p = 1:count
            output = calculateOutput(weights,x(p),y(p));
            localerror = T_Q2(p) - output
            weights(1)= weights(1) + learningrate *localerror*x(p);
            weights(2)= weights(1) + learningrate *localerror*y(p);
            weights(3)= weights(1) + learningrate *localerror;
            globalerror = globalerror + (localerror*localerror);
        end 
    end 

我在其他文件中编写了此函数,并在我之前的代码中调用了它.

I write this function in some other file and calling it in my previous code.

function result = calculateOutput (weights, x, y)
    s = x * weights(1) + y * weights(2) + weights(3);
    if s >= 0
        result = 1;
    else
        result = -1;
    end

推荐答案

我可以发现代码的一些问题.主要问题是目标是多类(而不是

I can spot a few problems with the code. The main issue is that the target is multi-class (not binary), so you need to either use 3 output nodes one for each class (called 1-of-N encoding), or use a single output node with a different activation function (something capable of more than just binary output -1/1 or 0/1)

在以下解决方案中,感知器具有以下结构:

In the solution below, the perceptron has the following structure:

%# 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

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

The results of training over the five sample you provided:

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个样本.如果每个班级都有更多的训练实例,您将获得更有意义的结果.

Note that the data used in the example above only contains 5 samples. You would get more meaningful results if you had more training instances in each class.

这篇关于单层神经网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆