2010-08-11 188 views
-73

對於單層神經網絡的實現,我有兩個數據文件。單層神經網絡

In: 
    0.832 64.643 
    0.818 78.843 

Out: 
    0 0 1 
    0 0 1 

以上是2個數據文件的格式。

目標輸出對於相應輸入所屬的特定類爲「1」,對於其餘2個輸出爲「0」。

的問題是如下:

你的單層神經網絡將 發現A(3×2矩陣)和b(3由1個 矢量)在Y = A * X + B式中,Y是[C1, C2,C3]'並且X是[x1,x2]'。

爲了解決上述與 神經網絡的問題,我們可以重新寫出 方程如下:Y = A '* X',其中 A」 = [A B](3×3矩陣)和X '是 [X1,X2,1]'

現在可以使用與 三個輸入節點(一個用於X1,X2,和 1分別地)和三個輸出(C1, C2,C3)神經網絡。由此產生的9個(因爲我們有3個輸入和3個 之間的輸出連接)權重將相當於A'矩陣的 元素。

Basicaly,我試圖做這樣的事情,但它不工作:

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 

我寫在其他一些文件這一功能,把它在我前面的代碼。

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

回答

221

我可以發現代碼的一些問題。主要問題是目標是multi-class(不是binary),所以您需要爲每個類使用3個輸出節點(稱爲1-of-N encoding),或者使用具有不同activation function的單個輸出節點(不僅能夠使用二進制輸出-1/1或0/1)

在下面的溶液中,perceptron具有以下結構:

perceptron_strucutre

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

plot

注意,在實施例中使用上述的數據只包含5個樣品。如果您在每個班級中有更多的培訓實例,您會得到更有意義的結果。