📄 snow.cpp
字号:
*errorStream << endl; for (; it != end; ++it) { *errorStream << setprecision(5); *errorStream << it->id << ":" << setw(14) << it->activation << setw(14) << it->baseActivation; if (ex.FeatureIsLabel(it->id)) *errorStream << "*"; *errorStream << endl; } *errorStream << endl; it = rank.begin(); } if (globalParams.verbosity == VERBOSE_MAX) { *(globalParams.pResultsOutput) << " Ex" << examples << ": "; *(globalParams.pResultsOutput) << "Prediction - " << prediction_stream.str() << " (" << it->activation << ")\n"; } else if ((globalParams.verbosity == VERBOSE_MED) && !(examples % 100)) *(globalParams.pResultsOutput) << examples << " examples presented\n"; break; case WINNERS: if(DEBUG_OUTPUT) { cerr << "##in Output()/WINNERS: prediction stream is " << prediction_stream.str() << "..." << endl; } *(globalParams.pResultsOutput) << prediction_stream.str() << endl; break; case SOFTMAX: *globalParams.pResultsOutput << "Example " << examples; if (globalParams.labelsPresent) { if (ex.features.Targets() > 0) { *globalParams.pResultsOutput << " Label: " << ex.features[0].id; for (i = 1; i < ex.features.Targets() && globalParams.multipleLabels; ++i) *globalParams.pResultsOutput << ", " << ex.features[i].id; } else *globalParams.pResultsOutput << " Not labeled."; } *globalParams.pResultsOutput << endl; for (; it != end; ++it) { *globalParams.pResultsOutput << setprecision(5); //globalParams.pResultsOutput->setf(ios::showpoint); *globalParams.pResultsOutput << it->id << ":" << setw(14) << it->softmax; if (ex.FeatureIsLabel(it->id)) *globalParams.pResultsOutput << "*"; *globalParams.pResultsOutput << endl; } *globalParams.pResultsOutput << endl; break; case ALL_ACTIVATIONS: *globalParams.pResultsOutput << "Example " << examples; if (globalParams.labelsPresent) { if (ex.features.Targets() > 0) { *globalParams.pResultsOutput << " Label: " << ex.features[0].id; for (i = 1; i < ex.features.Targets() && globalParams.multipleLabels; ++i) *globalParams.pResultsOutput << ", " << ex.features[i].id; } else *globalParams.pResultsOutput << " Not labeled."; } *globalParams.pResultsOutput << endl; for (; it != end; ++it) { *globalParams.pResultsOutput << setprecision(5); //globalParams.pResultsOutput->setf(ios::showpoint); *globalParams.pResultsOutput << it->id << ":" << setw(14) << it->activation << setw(14) << it->baseActivation << setw(14) << it->softmax; if (ex.FeatureIsLabel(it->id)) *globalParams.pResultsOutput << "*"; *globalParams.pResultsOutput << endl; } *globalParams.pResultsOutput << endl; break; case ALL_PREDICTIONS: *globalParams.pResultsOutput << "Example " << examples; if (globalParams.labelsPresent) { if (ex.features.Targets() > 0) { *globalParams.pResultsOutput << " Label: " << ex.features[0].id; for (i = 1; i < ex.features.Targets() && globalParams.multipleLabels; ++i) *globalParams.pResultsOutput << ", " << ex.features[i].id; } else *globalParams.pResultsOutput << " Not labeled."; } *globalParams.pResultsOutput << endl; for (; it != end; ++it) { *globalParams.pResultsOutput << it->id << ":\t" << (rank.single_target ? prediction : (it->id == prediction)); if (ex.FeatureIsLabel(it->id)) *globalParams.pResultsOutput << "*"; *globalParams.pResultsOutput << endl; } *globalParams.pResultsOutput << endl; break; case ALL_BOTH: *globalParams.pResultsOutput << "Example " << examples; if (globalParams.labelsPresent) { if (ex.features.Targets() > 0) { *globalParams.pResultsOutput << " Label: " << ex.features[0].id; for (i = 1; i < ex.features.Targets() && globalParams.multipleLabels; ++i) *globalParams.pResultsOutput << ", " << ex.features[i].id; } else *globalParams.pResultsOutput << " Not labeled."; } *globalParams.pResultsOutput << endl; for (; it != end; ++it) { *globalParams.pResultsOutput << setprecision(5); *globalParams.pResultsOutput << it->id << ":\t" << setw(1) << (rank.single_target ? prediction : (it->id == prediction)) << setw(14) << it->activation << setw(14) << it->baseActivation << setw(14) << it->softmax; if (ex.FeatureIsLabel(it->id)) *globalParams.pResultsOutput << "*"; *globalParams.pResultsOutput << endl; } *globalParams.pResultsOutput << endl; break; default: cerr << "SNoW::Output(): ERROR: output mode not recognized." << endl; } // end switch prediction_method}void Snow::FinalOutput( ostream& out, int correct, int suppressed, int examples, int not_labeled ){ if (globalParams.labelsPresent) { if (globalParams.verbosity != VERBOSE_QUIET) { out << examples << " test examples presented\n"; if (suppressed > 0) out << suppressed << " predictions suppressed\n"; } double pctCorrect = (double) correct / (examples - suppressed); double pctPredict = 1.00 - ((double)suppressed / examples); out << "Overall Accuracy - " << setprecision(4) << (pctCorrect * 100.0) << "% (" << correct << " / " << examples - suppressed; if (not_labeled) out << ", " << not_labeled << " of which weren't labeled"; out << ")\n"; if (suppressed > 0) out << " Prediction Rate - " << setprecision(4) << (pctPredict * 100.0) << "%\n"; }}bool Snow::Predict( TargetRanking &ranking ){ const bool DEBUG_PREDICT(false); if(DEBUG_PREDICT) cerr << "##in Snow::Predict()..." << endl; string prediction = "false"; bool prd = false;// return (ranking[0].activation - ranking[1].activation)// >= globalParams.predictionThreshold; // Check if we meet the threshold prd = (ranking[0].activation - ranking[1].activation) >= globalParams.predictionThreshold; if(DEBUG_PREDICT) { cerr << "##Snow::Predict(): activations are 0: " << ranking[0].activation << "; 1: " << ranking[1].activation << "; threshold is " << globalParams.predictionThreshold << "..." << endl; if(prd) prediction = "true"; cerr << "##Snow::Predict(): prediction is " << prediction << "..." << " (true means 'can predict')" << endl; } return prd;}//This version of EvaluateExample takes an Example and a TargetRanking, //evaluates the Example and updates the TargetRanking//mainly added for ShallowParservoid Snow::EvaluateExample(Example & example, TargetRanking & rank){ network->RankTargets(example, rank); return;}//EvaluateExample reads one example, evaluates it, and outputs the results.//Parameters: // testStream: an istream from which the example can be read// correct: a reference to an int counting correct classifications// suppressed: a reference to an int counting non-classifications// examples: a reference to an int counting examples// not_labeled: an int indicating whether or not examples are labeled// errorStream: an ofstream * to which classification mistakes can // be writtenvoid Snow::EvaluateExample(istream * testStream, int & correct, int & suppressed, int & examples, int not_labeled, ofstream * errorStream ) { const bool DEBUG_EE(false); if(DEBUG_EE) cerr << "##evaluateexample(): processing example..." << endl; holdRank->clear(); bool readResult = true; Example example(globalParams); if (globalParams.labelsPresent) readResult = example.ReadLabeled(*testStream); else readResult = example.Read(*testStream); if (!readResult) { cerr << "SNoW: EvaluateExample(): ERROR: " << "Failed reading example " << examples << " from " << "client." << endl; } else { if (globalParams.generateConjunctions == CONJUNCTIONS_ON) example.GenerateConjunctions(); if(DEBUG_EE) cerr << "##evaluateexample(): calling RankTargets()..." << endl; network->RankTargets(example, *holdRank); ++examples; if(DEBUG_EE) cerr << "##evaluateexample(): calling Output()..." << endl; Output(*holdRank, errorStream, example, correct, suppressed, examples, not_labeled); }//end else (example was read successfully) return;}FeatureID Snow::Evaluate(){ Example ex(globalParams); ex.Parse(globalParams.evalExample); if (globalParams.generateConjunctions == CONJUNCTIONS_ON) ex.GenerateConjunctions(); TargetRanking rank(globalParams); FeatureID prediction; // char prediction_text[32]; stringstream prediction_stream(stringstream::out); network->RankTargets(ex, rank); sort( rank.begin(), rank.end(), greater<TargetRank>() ); // check to see if we meet the predictionThreshold, // unless we only have one target if (network->SingleTarget()) { if (rank.begin()->baseActivation >= network->FirstThreshold()) { prediction = 1; prediction_stream << "positive"; } else { prediction = 0; prediction_stream << "negative"; } } else { if (globalParams.targetIds.size() == 1 || Predict(rank)) { prediction = rank.begin()->id; prediction_stream << prediction; } else { prediction = NO_PREDICTION; prediction_stream << "no prediction"; } } *globalParams.pResultsOutput << "For example: "; ex.Show(globalParams.pResultsOutput); *globalParams.pResultsOutput << "Prediction is " << prediction_stream.str() << endl; return prediction;}//added to hide network implementation from server//checks network is there, then calls network's writeAlgorithms fnvoid Snow::WriteAlgorithms(ostream * out) { if(network != 0) { network->WriteAlgorithms(out); } else cout << "ERROR: network not initialized." << endl;}void Snow::Pause(){ cerr << "Press Enter to continue... "; cin.ignore(999,'\n');}void Snow::presentTrainExample(Example * example, ofstream * outputConjunctionStream){ if(!network) { cerr << "ERROR: Snow::presentTrainExample(): " << "no network specified." << endl; exit(1); } else if(!example) { cerr << "ERROR: Snow::presentTrainExample(): " << "no example provided (null ptr)." << endl; exit(1); } if (globalParams.generateConjunctions == CONJUNCTIONS_ON) example->GenerateConjunctions(); if (globalParams.writeConjunctions) { if(!outputConjunctionStream) { cerr << "ERROR: Snow::presentTrainExample(): " << "no outputConjunctionStream given, but writeConjunctions active." << endl; exit(1); } else { example->Write(*outputConjunctionStream); } } if (network->PresentExample(*example)) ++mistakes;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -