我正在用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 ;
}
}
答案 0 :(得分: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 += ...;