【发布时间】:2011-11-25 00:41:18
【问题描述】:
我有一个小函数,它应该基于机器学习算法进行预测。该函数不工作,所以我放了一个打印语句来检查值,突然它开始工作了。当我注释掉打印行时,它再次停止工作。关于为什么会发生这种情况,我有什么遗漏吗?
int makePrediction( const InstanceT & instance, bool biased ){
double dotProduct = ( biased ? instance * _weights + _bias : instance * _weights );
std::cout << "dotProduct = " << dotProduct << std::endl;
return ( dotProduct > 0 ? 1 : -1 );
}
由于某种原因会产生不同的结果
int makePrediction( const InstanceT & instance, bool biased ){
double dotProduct = ( biased ? instance * _weights + _bias : instance * _weights );
return ( dotProduct > 0 ? 1 : -1 );
}
为了表明在相同输入的情况下结果不同,我将这个函数称为:
std::vector<InstanceT> _instances = populate_data() //this works for both versions
for ( int i = 0; i < _instances.size(); i++ ){
std::cout << "prediction: " << makePrediction( _instances[i], true ) << std::endl;
}
有什么想法吗?
【问题讨论】:
-
codereview.stackexchange.com 的好问题
-
“没有工作”是什么意思?预期和观察到的行为是什么?请指定准确的输入和输出。
-
发布出现问题的完整(但最少)代码。发布结果。解释你的期望。请参阅此 FAQ item,了解如何发帖。它不是为 SO 制作的,但它也适用于这里。
-
您的应用程序是多线程的吗? I/O 有时可以“解决”并发问题,因为它引入了延迟(可能是因为要写入控制台的互斥锁等)。当然,问题只是隐藏起来,根本没有解决。
-
“不起作用”不是一个好的问题描述。