求和给出随机错误数字

Summation giving random wrong numbers

我正在尝试为考试实现一个简单的反向传播算法(我是一名初级程序员)。 我有一组数组,我生成随机权重来启动算法。 我按照数学公式实现了激活函数:

formula

(其中 x 索引用于输入,y 索引用于隐藏神经元输入)

我的问题是我得到的一些求和结果具有非常高的指数值,与我的预期不相容。

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#define INPUT_NEURONS   4
#define HIDDEN_NEURONS  7
#define OUTPUT_NEURONS  3
#define MAX_SAMPLES     150
#define LEARNING_RATE   0.1
#define RAND_WEIGHT ((double)rand()/(RAND_MAX+1))

double IHweight[INPUT_NEURONS][HIDDEN_NEURONS];  /* in->hid weight */
double HOweight[HIDDEN_NEURONS][OUTPUT_NEURONS]; /* hid->out weight */
//activation
double inputs[MAX_SAMPLES][INPUT_NEURONS];
double hidden[HIDDEN_NEURONS];
double target[MAX_SAMPLES][OUTPUT_NEURONS];
double actual[OUTPUT_NEURONS];
//errors
double errO[OUTPUT_NEURONS];
double errH[HIDDEN_NEURONS];
double Error = 0.0;
int sample = 0;

typedef struct {
    double sepal_lenght;
    double sepal_width;
    double petal_lenght;
    double petal_width;
    double output[OUTPUT_NEURONS];
} IRIS;
IRIS samples[MAX_SAMPLES] = {
    {   5.1,    3.5,    1.4,    0.2,    0.0,    0.0,    1.0 },
    {   4.9,    3.0,    1.4,    0.2,    0.0,    0.0,    1.0 },
    {   4.7,    3.2,    1.3,    0.2,    0.0,    0.0,    1.0 },
    {...},
};

double sigmoid(double val) {
    return (1.0 / (1.0 + exp(-val)));
}
double dsigmoid(double val) {
    return (val * (1.0 - val));
}
void assignRandomWeights() {
    int hid, inp, out;
    printf("Initializing weights...\n\n");
    for (inp = 0; inp < INPUT_NEURONS; inp++) {
        for (hid = 0; hid < HIDDEN_NEURONS; hid++) {
            IHweight[inp][hid] = RAND_WEIGHT;
            printf("Weights : input %d -> hidden %d: %f\n",
                   inp, hid, IHweight[inp][hid]);
        }
    }
    for (hid = 0; hid < HIDDEN_NEURONS; hid++) {
        for (out = 0; out < OUTPUT_NEURONS; out++) {
            HOweight[hid][out] = RAND_WEIGHT;
            printf("hidden %d -> output %d: %f\n", 
                   hid, out, HOweight[hid][out]);
        }
    }
    system("pause");
}

void activation() {
    int hid, inp, out;
    double sumH[HIDDEN_NEURONS] ;
    double sumO[OUTPUT_NEURONS];

    for (hid = 0; hid < HIDDEN_NEURONS; hid++) {
        for (inp = 0; inp < INPUT_NEURONS; inp++) {
            sumH[hid] += (inputs[sample][inp] * IHweight[inp][hid]);
            printf("\n%d Input %d = %.1f Weight = %f sumH = %g",
                   sample, inp, inputs[sample][inp], IHweight[inp][hid], sumH[hid]);
        }
        hidden[hid] = sigmoid(sumH[hid]);
        printf("\nHidden neuron %d activation = %f", hid, hidden[hid]);
    }
    for (out = 0; out < OUTPUT_NEURONS; out++) {
        for (hid = 0; hid < HIDDEN_NEURONS; hid++) {
            sumO[out] += (hidden[hid] * HOweight[hid][out]);
            printf("\n%d Hidden %d = %f Weight = %f sumO = %g",
                   sample, hid, hidden[hid], HOweight[hid][out], sumO[out]);
        }
        actual[out] = sigmoid(sumO[out]);
        printf("\nOutput neuron %d activation = %f", out, actual[out]);
    }
}

main () {
    srand(time(NULL));
    assignRandomWeights();
    for (int epoch = 0; epoch < 1; epoch++) {
        for (int i = 0; i < 1; i++) {
            sample = rand() % MAX_SAMPLES;
            inputs[sample][0] = samples[sample].sepal_lenght;
            inputs[sample][1] = samples[sample].sepal_width;
            inputs[sample][2] = samples[sample].petal_lenght;
            inputs[sample][3] = samples[sample].petal_width;
            target[sample][0] = samples[sample].output[0];
            target[sample][1] = samples[sample].output[1];
            target[sample][2] = samples[sample].output[2];
            activation();
        }
    }
}

我使用了很多 printf() 来检查我的结果,我得到了

...
41 Input 0 = 4.5 Weight = 0.321014 sumH = 1.31886e+267
41 Input 1 = 2.3 Weight = 0.772369 sumH = 1.31886e+267
41 Input 2 = 1.3 Weight = 0.526123 sumH = 1.31886e+267
41 Input 3 = 0.3 Weight = 0.271881 sumH = 1.31886e+267
Hidden neuron 6 activation = 1.000000
...
41 Hidden 0 = 0.974952 Weight = 0.343445 sumO = 1.24176e+267
41 Hidden 1 = 0.917789 Weight = 0.288361 sumO = 1.24176e+267
41 Hidden 2 = 0.999188 Weight = 0.972168 sumO = 1.24176e+267
41 Hidden 3 = 0.989726 Weight = 0.082642 sumO = 1.24176e+267
41 Hidden 4 = 0.979063 Weight = 0.531799 sumO = 1.24176e+267
41 Hidden 5 = 0.972474 Weight = 0.552521 sumO = 1.24176e+267
41 Hidden 6 = 1.000000 Weight = 0.707153 sumO = 1.24176e+267
Output neuron 1 activation = 1.000000

据我所知,assignRandomweights()sigmoid() 功能没问题,问题出在 activation().

请帮助我了解为什么会发生这种情况以及如何解决它。

你的问题在这几行

double sumH[HIDDEN_NEURONS];
double sumO[OUTPUT_NEURONS];

您在使用前不初始化它们。从技术上讲,程序行为是未定义的。您友好的编译器似乎将未初始化的变量设置为较大的值。 (其他平台如 Itanium 将捕获 "Not a Thing")。

一个简单的补救方法是使用double sumH[HIDDEN_NEURONS] = {0}; ,这会将每个元素设置为零。

检查这一行:

double sumH[HIDDEN_NEURONS] ;

和这一行:

sumH[hid] += (inputs[sample][inp] * IHweight[inp][hid]);

你已经声明了 sumH[] 而没有将它的每个成员设置为零,因此,它从任意值开始,因为没有 sumH[hid]

的定义

您可以使用: for(unsigned int i=0; i<HIDDEN_NEURONS; i++) sumH[i] = 0; 在使用它之前(如果您不喜欢 malloc()ZeroMemory()),例如...