给出随机错误数字的求和

时间:2016-01-18 14:43:16

标签: c math sum neural-network backpropagation

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

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()

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

2 个答案:

答案 0 :(得分:1)

您的问题出在这些方面

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

使用前请勿初始化它们。从技术上讲,程序行为是未定义的。友好的编译器似乎将未初始化的变量设置为较大的值。 (其他平台,如Itanium将陷入&#34; Not a Thing&#34;)。

一个简单的补救措施是使用double sumH[HIDDEN_NEURONS] = {0}; 等。将每个元素设置为零。

答案 1 :(得分:1)

检查此行:

 while(true)
 {
  int start; 
  if (lines.Substring(0,1) == "[")
    start = 0;
  else
    start = lines.IndexOf(Environment.NewLine + "[") + Environment.NewLine.Length;
  int end = lines.IndexOf("] ");
  if (start == -1 || end == -1)
    break;
  string subString = lines.Substring(start, end + 2 - start);
  lines = lines.Replace(subString, "");
 }

和这一行:

double sumH[HIDDEN_NEURONS] ;

你已经声明了sumH []而没有将每个成员设置为零,因此,它从任意值开始,因为没有sumH[hid] += (inputs[sample][inp] * IHweight[inp][hid]); 的定义

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