Matlab 单层神经网络

Matlab 单层神经网络,matlab,artificial-intelligence,classification,perceptron,Matlab,Artificial Intelligence,Classification,Perceptron,对于单层神经网络的实现,我有两个数据文件 In: 0.832 64.643 0.818 78.843 Out: 0 0 1 0 0 1 以上是2个数据文件的格式 In: 0.832 64.643 0.818 78.843 Out: 0 0 1 0 0 1 对应输入所属的特定类的目标输出为“1”,其余2个输出的目标输出为“0” 问题如下: 您的单层神经网络将 求A(3乘2矩阵)和b(3乘1 向量)Y=A*X+b,其中Y为[C1

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

In:
    0.832 64.643
    0.818 78.843

Out:
    0 0 1
    0 0 1
以上是2个数据文件的格式

In:
    0.832 64.643
    0.818 78.843

Out:
    0 0 1
    0 0 1
对应输入所属的特定类的目标输出为“1”,其余2个输出的目标输出为“0”

问题如下:

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

要解决上述问题,请使用 神经网络,我们可以重新编写 方程如下:Y=A'*X',其中 A'=[ab](3×3矩阵)和X'是 [x1,x2,1]'

现在,你可以使用神经网络与 三个输入节点(一个用于x1、x2和 分别为1)和三个输出(C1, C2,C3)

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

基本上,我正在尝试做类似的事情,但它不起作用:

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 

我可以发现代码中的一些问题。主要的问题是目标是(不是),所以您需要使用3个输出节点,每个类一个(调用),或者使用一个具有不同输出的输出节点(可以不仅仅是二进制输出-1/1或0/1)

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

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

请注意,上面示例中使用的数据仅包含5个样本。如果在每堂课中有更多的培训实例,您将获得更有意义的结果

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