c - C 中的感知器无法训练

标签 c neural-network

尝试用 C 语言实现感知器,但无法训练它。 输出总是为零,我不知道出了什么问题。 不过,我确实怀疑这可能是 delta 函数,或者是我错误地实现了感知器。

提前感谢所有提供帮助的人!

#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;
}

最佳答案

由于 errorlearningRatewight 小于 0,表达式 error *learningRate * wight 将也趋于0。

delta 不应该是权重的新值,它是变化量,因此而不是:

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

尝试:

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

(您的感知器公式使用哪个来源?)

关于c - C 中的感知器无法训练,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46752160/

相关文章:

python - Keras卷积滤波器大小问题

c - Web 程序集 - 类型错误 : NetworkError when attempting to fetch resource

neural-network - 人工神经网络的深度

c# - 模式识别霍普菲尔德

c - 机器级内存理论

machine-learning - 神经网络中的投影层是什么?

machine-learning - 为什么不在训练数据集上优化超参数?

c - 当两个子进程都通过管道与父进程通信时,一个子进程会阻塞另一个子进程

c - 在 C 中的 pragma 宏中插入双引号

c++ - 为支持 CNAME 记录的 A 记录配置 TTL