我不确定如何引用此神经网络训练方法中的权重

I'm not sure how to refer to the weights in this Neural Network Train method

本文关键字:神经网络 方法 权重 不确定 何引用 引用      更新时间:2023-10-16

我正在用C++编写自己的神经网络类实现。我不确定如何引用此语句的权重:

in = in + (inputs [l] * calcWeights [l]) ;

原因是权重可能于输入。这是我的代码:

void Train (int numInputs, int numOutputs, double inputs [], double outputs []) {
// Set the Random Seed:
srand (time (0)) ;
// Weights (n input(s) * n output(s) = n weight branch(es)):
double calcWeights [numInputs * numOutputs] ;
// Errors (n input(s) * n output(s) = n error branch(es)):
double errors [numInputs * numOutputs] ;
// Set the Weights to random:
for (int j = 0 ; j < numInputs ; j = j + 1) {
calcWeights [j] = ((-1 * numInputs) + (((double) rand ()) % (1 * numInputs))) ;
}
// Train:
int i = 0 ;
double in = 0 ;
double out [numOutputs] ;
while (i < 14999) {
// Get the estimated output:
for (int k = 0 ; k < numOutputs ; k = k + 1) {
for (int l = 0 ; l < numInputs ; l = l + 1) {
in = in + (inputs [l] * calcWeights [l]) ;
}
out [k] = in + GetBias () ;
}
for (int m = 0 ; m < numOutputs ; m = m + 1) {
error [m] = outputs [m] - out [m]
}
// Increment the iterator:
i = i + 1 ;
}
}

从您在评论中的澄清来看,我相信稍微修改一下您的循环会给您想要的东西。

for (int k = 0 ; k < numOutputs ; k = k + 1) {
in = 0; //Reset in to 0 at the beginning of each output loop
for (int l = 0 ; l < numInputs ; l = l + 1) {
in = in + (inputs [l] * calcWeights [l + k*numInputs]) ;
}
out [k] = in + GetBias () ;
}

您还应该确保初始化上述所有权重。

for (int j = 0 ; j < (numInputs * numOutputs) ; j = j + 1) {
calcWeights [j] = ((-1 * numInputs) + (((double) rand ()) % (1 * numInputs))) ;
}

对于几种样式选择,我只想指出,您可以将k = k + 1替换为简单的++k。同样,您可以将in = in + ...;替换为in += ...;