1

我不認爲我的問題是this one的重複,因爲我已經在我的實現中存在偏見。Erlang的感知器在訓練後沒有學習

我嘗試實現感知器及其在Erlang中識別線性​​斜率的訓練。問題在於它沒有得到適當的培訓。它猜測的值在50個紀元後仍然約爲50%。

起始權重的列表[X_weight, Y_Weight, Bias_weight]供給和訓練集是在另一個列表[X,Y,Desired_guess]其中X和Y是整數,並且Desired_guess是-1,如果座標是行或1下供給,如果它是過線。

首先是新的權重計算:

% Exported starting clause 
% Inputs are - List of input values for one perceptron ([X,Y,Bias]), A list of weights corresponding to the inputs [X_weight, Y_weight, Bias_weight], the learning constant and the error (Desired-Guess) 

train_perceptron([InputsH|InputsT], [WeightsH|WeightsT], Learning_constant, Error) -> 
    train_perceptron(InputsT, WeightsT, Learning_constant, Error, 
     [WeightsH + (Learning_constant * Error) * InputsH]). 

% Not exported clause called by train_perceptron/4 This also has a list of the new adjusted weights. 
% When the tail of input lists are empty lists it is the last value, and thereby the Bias 
train_perceptron([InputsH|[]], [WeightsH|[]], Learning_constant, Error, Adjusted_weights) -> 
    train_perceptron([], [], Learning_constant, Error, 
     Adjusted_weights ++ [WeightsH + Learning_constant * Error]); 

%Normal cases, calcualting the new weights and add them to the Adjusted_weights 
train_perceptron([InputsH|InputsT], [WeightsH|WeightsT], Learning_constant,  Error, Adjusted_weights) -> 
    train_perceptron(InputsT, WeightsT,Learning_constant, Error, 
    Adjusted_weights ++ [WeightsH + (Learning_constant * Error) * InputsH]); 

%Base case the lists are empty, no more to do. Return the Adjusted_weights 
train_perceptron([], [],_, _, Adjusted_weights) -> 
    Adjusted_weights. 

這是調用train_perceptron功能

line_trainer(Weights,[],_) -> 
    Weights; 
line_trainer(Weights, [{X,Y,Desired}|TST], Learning_constant)-> 
    Bias = 1, 
    Error = Desired - feedforward([X,Y,Bias],Weights), 
    Adjusted_weights = train_perceptron([X,Y,Bias], Weights, Learning_constant, Error), 
    line_trainer(Adjusted_weights, TST, Learning_constant). 

一個解決方案的功能,可能是,如果有人給我提供了與訓練組這種功能爲每個時期提供三個起始權重和輸出。這可以幫助我自己調試。

+0

你可以查看/編輯你的代碼,這是非常難以閱讀,而第二行有語法錯誤,並沒有什麼作用,如果你抑制額外')' – Pascal

+0

@帕斯卡你說得對。當我從我的編輯複製粘貼時,一條線斷了。試圖格式化和評論的代碼清晰。 –

回答

0

這實際上起作用。我提供的訓練集是很小的。對於較大的訓練集和20時代的gobal誤差收斂到0

+1

你的train_perceptron/4和train_perceptron/5確實減少到一個單獨的2子句arity 4函數BTW:tp([IH],[WH | []],LC,E) - > [WH + LC * E]; tp([IH | InT],[WH | WT],LC,E) - > [WH +(LC * E)* IH | tp(InT,WT,LC,E)]。 – Michael

+0

@邁克爾謝謝,一個很好的解決方案。這是一段時間,因爲我用功能語言做了任何事情。需要改進我的風格,擺脫這種冗餘sluggsihness我現在摸索。 –