虽然您没有提供A Minimal,Complete,and Verifiable Example (MCVE),但是在修复"%[^\n]"
并将其设置为"%lf"
之后,代码的直接问题是您正在限制读循环的结尾在status1 != EOF && status2 != EOF
上。直到您尝试通过在达到EOF
之后尝试printf
的{{1}}值来调用未定义行为后,X,Y
都不是。
此外,将循环设为EOF
会忽略在达到EOF
之前可能导致的 matching 或 input 失败。相反,您总是根据成功转换次数(即返回的数值)来验证EOF
(在您的情况下为scanf
)是成功还是失败。这就是为什么评论中提到您Why is while ( !feof (file) ) always wrong?的原因。
最好根据数字返回值来控制读取循环,例如
f_scanf
或简单地连续循环直到其中一个失败,例如
while (fscanf_s (data_input,"%lf",&X) == 1 &&
fscanf_s(data_output,&Y) == 1) {
鉴于您要完成的任务,可以执行类似以下操作:
while (1) {
...
status1 = fscanf_s (data_input,&X);
if (status1 != 1) {
if (status1 == EOF) {
fputs ("status1-EOF\n",stderr);
break;
}
else if (status1 == 0) {
fputs ("matching or input failure.\n",stderr);
/* matching or input failure occurred,* handle as appropriate (clear to '\n' or break).
*/
}
}
(注意:最后,您的double update_weights_v2 (LOGISTIC_MODEL hModel,FILE* data_input,FILE* data_output)
{
Model* pModel = (Model*)hModel;
double X,Y,error = 0,prediction,W1_deriv = 0,W2_deriv = 0;
int n = 0;
while (fscanf_s (data_input,&Y) == 1) {
prediction = pModel->activation(X,pModel->W1,pModel->W2);
error = Y - prediction;
W1_deriv += (X * error * pModel->rate);
W2_deriv += (error*pModel->rate);
n++;
printf ("Input: %.5lf\nOutput: %.5lf\nGuess: %.5lf\nError: %.5lf\n"
"Weight[1]: %.5lf\nWeight[2]: %.5lf\n"
"***********************************************************\n",X,error,pModel->W2);
}
rewind(data_input);
rewind(data_output);
return error;
}
将掩盖函数中设置的流状态-可能正是您想要的,也可能掩盖数据文件中的问题在您下次致电rewind()
时会再次咬你)
要进一步提供帮助,我们将需要A Minimal,and Verifiable Example (MCVE)。
,
尽管有很多有用的答案,但解决此问题的原因是vs代码中未显示一行。无论出于什么原因,我都无法查看应该限制程序显示的输出量的行,从而不会因我的模型的每一次猜测而向控制台发送垃圾邮件。更新后我重新加载了vs代码,并且该行返回了。我不知道为什么会这样,但是现在有意义。
谢谢。
本文链接:https://www.f2er.com/3164042.html