我正在用 C++ 编写一个神经网络,以使用具有 5 个隐藏神经元的单个隐藏层来逼近xSin(x)函数。隐藏神经元使用tanh激活,输出层使用线性激活。我在 10,000 个 epoch 中使用了 30 个训练示例。
直到我洗牌我的数据,这就是我得到的:( 红色:预测数据,绿色:实际数据),MSE 也接近 0
但是当我打乱训练示例的索引并验证我的打乱确实打乱时,我得到了可怕的结果:
和错误与时代为:
什么可能出错?洗牌可以对此负责吗?
这是供参考的简单代码
//Shuffle Function
void shuffle(int *array, size_t n)
{
if (n > 1) //If no. of training examples > 1
{
size_t i;
for (i = 0; i < n - 1; i++)
{
size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
int t = array[j];
array[j] = array[i];
array[i] = t;
}
}
}
int main(int argc, const char * argv[])
{
//Some other actions
///FOR INDEX SHUFFLING
int trainingSetOrder[numTrainingSets];
for(int j=0; j<numTrainingSets; ++j)
trainingSetOrder[j] = j;
///TRAINING
//std::cout<<"start train\n";
vector<double> performance, epo; ///STORE MSE, EPOCH
for (int n=0; n < epoch; n++)
{
shuffle(trainingSetOrder,numTrainingSets);
for (int i=0; i<numTrainingSets; i++)
{
int x = trainingSetOrder[i];
//cout<<" "<<"("<<training_inputs[x][0]<<","<<training_outputs[x][0] <<")";
/// Forward pass
for (int j=0; j<numHiddenNodes; j++)
{
double activation=hiddenLayerBias[j];
//std::cout<<"Training Set :"<<x<<"\n";
for (int k=0; k<numInputs; k++) {
activation+=training_inputs[x][k]*hiddenWeights[k][j];
}
hiddenLayer[j] = tanh(activation);
}
for (int j=0; j<numOutputs; j++) {
double activation=outputLayerBias[j];
for (int k=0; k<numHiddenNodes; k++)
{
activation+=hiddenLayer[k]*outputWeights[k][j];
}
outputLayer[j] = lin(activation);
}
/// Backprop
/// For V
double deltaOutput[numOutputs];
for (int j=0; j<numOutputs; j++) {
double errorOutput = (training_outputs[i][j]-outputLayer[j]);
deltaOutput[j] = errorOutput*dlin(outputLayer[j]);
}
/// For W
//Some Code
///Updation
/// For V and b
///Some Code
/// For W and c
for (int j=0; j<numHiddenNodes; j++) {
//c
hiddenLayerBias[j] += deltaHidden[j]*lr;
//W
for(int k=0; k<numInputs; k++) {
hiddenWeights[k][j]+=training_inputs[i][k]*deltaHidden[j]*lr;
}
}
}
}
return 0;
}