2017-10-15 71 views
2

試圖在C中實現感知器,無法訓練它。 輸出總是會變成零,我不知道什麼是錯的。 雖然我懷疑它可能是三角函數,或者我錯誤地實現了感知器。C中的感知器未能訓練

在此先感謝大家幫助!

#include<stdio.h> 
#define arrayLength(x) (sizeof(x)/sizeof((x)[0])) 
typedef int bool; 
enum { false, true }; 

int main(){ 
    float trainInputs [2][2] = {{0.0f, 1.0f}, {0.0f, 0.0f}}; 
    float trainOutputs [2][1] = {{1.0f}, {0.0f}}; 
    int amontOfTrainData = 1; 

    float inputs [] = {0.0f, 1.1f}; 
    float outputs [] = {0.0f}; 
    float wights [(arrayLength(inputs) * arrayLength(outputs))] = {0.5f, 0.5f, 0.5f, 0.5f}; 
    float learningRate = 0.01f; 

    float delta(float actual, float want, float wight){ 
     float error = want - actual; 
     float out = error * learningRate * wight; 
     printf(":%.6f:\n", out); 
     return out; 
    } 

    // Run perceptron 

    void run(bool train){ 
     int outputInc = 0; 
     int wightInc = 0; 
     while(outputInc < arrayLength(outputs)){ 
      int inputInc = 0; 

      while(inputInc < arrayLength(inputs)){ 
       if(train){ 
        int x = 0; 
        while(x < amontOfTrainData){ 
         outputs[outputInc] = trainInputs[x][inputInc] * wights[wightInc]; 
         wights[wightInc] = delta(outputs[outputInc], trainOutputs[x][outputInc], wights[wightInc]); 
         x++; 
        } 
       }else{ 
        outputs[outputInc] = inputs[inputInc] * wights[wightInc]; 
       } 
       inputInc++; 
       wightInc++; 
      } 
      //printf("out[%i]: %.5f\n", outputInc, outputs[outputInc]); 
      outputInc++; 
     } 
    } 

    int b = 0; 
    while(b < 100){ 
     run(true); 
     b++; 
    } 
    printf("-----------[ 100 LOOPS DONE ]-----------\n"); 
    run(false); 

    return 0; 
} 
+1

'的typedef整數BOOL;'=>請使用標準的,包括'' – Stargateur

+0

此數組的長度是2,但你提供4個初始化:'浮屍妖[(arrayLength(輸入)* arrayLength(輸出))] = {0.5f,0.5f,0.5f,0.5f};' – MondKin

+1

請注意,C中不允許嵌套函數定義,儘管有一個gcc擴展允許它.... –

回答

1

error如,learningRatewight小於0,表達式error * learningRate * wight將趨於0太。

delta不應該爲重的新的價值,它是變化的量,所以不是:

wights[wightInc] = delta(...); 

嘗試:

wights[wightInc] += delta(...); 

(您正在使用哪個源你感知式公式?)

1

我已經應用了所有的更改,這是最終的工作代碼。 感謝大家幫助我!

#include<stdio.h> 
#include<stdbool.h> 
#define arrayLength(x) (sizeof(x)/sizeof((x)[0])) 

float trainInputs [2][2] = {{0.0f, 1.0f}, {0.0f, 0.0f}}; 
float trainOutputs [2][1] = {{1.0f}, {0.0f}}; 
int amontOfTrainData = 1; 

float inputs [] = {1.0f, 1.0f}; 
float outputs [] = {0.0f}; 
float wights [(arrayLength(inputs) * arrayLength(outputs))] = {0.001f, 0.001f}; 
float learningRate = 0.1f; 


float delta(float actual, float want, float wight) 
{ 
    float error = want - actual; 
    float out = error * learningRate * wight; 
    return out; 
} 

void run(bool train) 
{ 
    int outputInc = 0; 
    int wightInc = 0; 
    while(outputInc < arrayLength(outputs)) 
    { 
     int inputInc = 0; 

     while(inputInc < arrayLength(inputs)) 
     { 
      if(train) 
      { 
       int x = 0; 
       while(x < amontOfTrainData) 
       { 
        outputs[outputInc] = trainInputs[x][inputInc] * wights[wightInc]; 
        wights[wightInc] += delta(outputs[outputInc], trainOutputs[x][outputInc], wights[wightInc]); 
        x++; 
       } 
      } 
      else 
      { 
       outputs[outputInc] = inputs[inputInc] * wights[wightInc]; 
      } 
      inputInc++; 
      wightInc++; 
     } 
     printf("out[%i]: %.5f\n", outputInc, outputs[outputInc]); 
     outputInc++; 
    } 
} 

int main() 
{ 
    // Run neural network 
    int b = 0; 
    int loops = 500; 
    while(b < loops) 
    { 
     run(true); 
     b++; 
    } 
    printf("-----------[ %i LOOPS DONE ]-----------\n", loops); 
    run(false); 

    return 0; 
}