我有一個小函數,應該根據機器學習算法進行預測。該函數不起作用,所以我在打印語句中檢查值,並突然開始工作。當我註釋掉打印行時,它會再次停止工作。有什麼我錯過了爲什麼會發生?爲什麼打印一個變量會改變它的值?
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 –
你是什麼意思的「不工作」?預期和觀察到的行爲是什麼?請指定確切的輸入和輸出。 –
std :: endl通常也會刷新。您的調用代碼是否產生取決於cout緩衝區狀態的輸出?似乎不太可能。輸出實際上有多不同;什麼是「makePrediction」的正確值,它在錯誤時給出了什麼? –