📄 network.cpp
字号:
.VCIterator; if (ConstraintClassificationUpdate(ex, currentCloud, subordinateCloud, ex.features[currentID].id, ex.features[subordinateID].id)) mistakes = true; } if (currentID < targets) { // Find the cloud that corresponds to the remaining target ID. if (currentID > 0) currentCloud = subordinateCloud; else currentCloud = TargetIDToCloud[ex.features[currentID].id] .VCIterator; // Find out which targets are active first. for (i = 0; i < clouds.size(); ++i) TargetIDToCloud[globalParams.targetIdsArray[i]] .not_active = true; for (currentID = 0; currentID < ex.features.Targets(); ++currentID) TargetIDToCloud[ex.features[currentID].id].not_active = false; // Run through all target IDs that weren't looked at previously, // treating them as subordinate to the remaining target ID. for (i = 0; i < clouds.size(); ++i) { if (TargetIDToCloud[globalParams.targetIdsArray[i]] .not_active) { subordinateCloud = TargetIDToCloud[globalParams.targetIdsArray[i]] .VCIterator; if (ConstraintClassificationUpdate(ex, currentCloud, subordinateCloud, ex.features[currentID].id, ex.features[subordinateID].id)) mistakes = true; } } } } } return mistakes;}bool Network::PresentInteractiveExample( Example& ex ){ CloudVector::iterator it = clouds.begin(); CloudVector::iterator clouds_end = clouds.end(); TargetIdSet::iterator targets_end = ex.targets.end(); bool targets_empty = ex.targets.empty(); bool mistakes = false; if (targets_empty) return mistakes; for (; it != clouds_end; ++it) { if (ex.targets.find(it->Id()) != targets_end) { // The following call to PresentExample will always return false if // globalParams.constraintClassification = true, OR // globalParams.runMode == MODE_INTERACTIVE if (it->PresentExample(ex)) mistakes = true; if (ex.command == 'p') // promote it->Update(ex, true); else if (ex.command == 'd') // demote it->Update(ex, false); } } return mistakes;}bool Network::ConstraintClassificationUpdate(Example& ex, CloudVector::iterator& currentCloud, CloudVector::iterator& subordinateCloud, FeatureID currentID, FeatureID subordinateID){ bool mistakes = false; if (currentCloud == clouds.end() || subordinateCloud == clouds.end()) { cerr << "Fatal Error:\n"; if (currentCloud == clouds.end()) cerr << " Target " << currentID << " specified in target order, but not found in target set.\n"; if (subordinateCloud == clouds.end()) cerr << " Target " << subordinateID << " specified in target order, but not found in target set.\n"; exit(1); } // Update if the cloud that should have been higher turned out to be lower.#ifdef TRAIN_WITH_NORMALIZED_ACTIVATION if (subordinateCloud->NormalizedActivation() >= currentCloud->NormalizedActivation())#else if (subordinateCloud->Activation() >= currentCloud->Activation())#endif { if (globalParams.verbosity >= VERBOSE_MED) { if (globalParams.verbosity == VERBOSE_MED) *globalParams.pResultsOutput << endl; *globalParams.pResultsOutput << "Target " << subordinateID << " is more activated (" << subordinateCloud->Activation() << ", " << subordinateCloud->NormalizedActivation() << ") than target " << currentID << " (" << currentCloud->Activation() << ", " << currentCloud->NormalizedActivation() << ").\n" << "Demoting target " << subordinateID << " and promoting target " << currentID << ".\n"; if (globalParams.verbosity > VERBOSE_MED) *globalParams.pResultsOutput << endl; } subordinateCloud->Update(ex, false); // false = demotion currentCloud->Update(ex, true); // true = promotion mistakes = true; } return mistakes;}void Network::TrainingComplete(){ // Walk through clouds CloudVector::iterator it = clouds.begin(); CloudVector::iterator end = clouds.end(); for (; it != end; ++it) it->TrainingComplete();}void Network::PerformPercentageEligibility(){ // Walk through clouds CloudVector::iterator it = clouds.begin(); CloudVector::iterator end = clouds.end(); for (; it != end; ++it) it->PerformPercentageEligibility();}void Network::Discard(){ if (globalParams.discardMethod != DISCARD_NONE) { if (globalParams.discardMethod == DISCARD_ABS) { CloudVector::iterator it = clouds.begin(); CloudVector::iterator end = clouds.end(); for (; it != end; ++it) it->Discard(); } else { // NOTE: This method really only makes sense for Winnow. We'll allow it // for others but it probably won't yield good results for others. CloudVector::iterator it = clouds.begin(); CloudVector::iterator end = clouds.end();#if defined(FEATURE_HASH) && !defined(WIN32) hash_set<FeatureID> allFeatures; hash_set<FeatureID>::const_iterator feat;#else set<FeatureID> allFeatures; set<FeatureID>::const_iterator feat;#endif for (; it != end; ++it) it->CollectFeatures(allFeatures); for (feat = allFeatures.begin(); feat != allFeatures.end(); ++feat) { it = clouds.begin(); double min = 1.0e300; int dups = 0; for (it = clouds.begin(); it != end; ++it) it->FindMinimumWeight(*feat, min, dups); if (!dups) { for (it = clouds.begin(); it != end; ++it) it->Discard(*feat, min); } } } }}double Network::getNorm(){ CloudVector::iterator it = clouds.begin(); CloudVector::iterator end = clouds.end(); double norm = 0; for (; it != end; ++it) norm += it->getNormSquared(); return sqrt(norm);}double Network::getNorm(FeatureID target){ CloudVector::iterator it = clouds.begin(); CloudVector::iterator end = clouds.end(); for (; it != end && it->Id() != target; ++it); return sqrt(it->getNormSquared());}void Network::RankTargets( Example& ex, TargetRanking& rank ){ const bool DEBUG_NETWORK(false); if(DEBUG_NETWORK) cerr << "##in Network::RankTargets()..." << endl; /* The Naive Bayes PrepareToRank function currently does nothing. NaiveBayes nb; AlgorithmVector::iterator algIt = algorithms.begin(); AlgorithmVector::iterator algEnd = algorithms.end(); for (; algIt != algEnd; ++algIt) if (typeid(nb) == typeid(**algIt)) dynamic_cast<NaiveBayes*>(*algIt)->PrepareToRank(); */ CloudVector::iterator it = clouds.begin(); CloudVector::iterator end = clouds.end(); double softmaxDenominator = 0; TargetIdSet::iterator targets_end = ex.targets.end(); int size = ex.targets.size(), ranked_targets = 0; bool empty = ex.targets.empty(); for (; it != end && (empty || ranked_targets < size); ++it) { if (empty || ex.targets.find(it->Id()) != targets_end) { ++ranked_targets; it->PreparetoRank(ex); softmaxDenominator += exp(it->Activation()); } } for (ranked_targets = 0, it = clouds.begin(); it != end && (empty || ranked_targets < size); ++it) { if (empty || ex.targets.find(it->Id()) != targets_end) { ++ranked_targets; // get the activation for the current cloud and insert it into the // ranking TargetRank tr(it->Id(), it->NormalizedActivation(), it->Activation(), exp(it->Activation()) / softmaxDenominator); rank.push_back(tr); } } if(DEBUG_NETWORK) cerr << "##leaving Network::RankTargets()..." << endl;}void Network::Show(ostream* out){ AlgorithmVector::iterator algIt = algorithms.begin(); AlgorithmVector::iterator algEnd = algorithms.end(); for (; algIt != algEnd; ++algIt) (*algIt)->Show(out); CloudVector::iterator it = clouds.begin(); CloudVector::iterator end = clouds.end(); for (; it != end; ++it) it->Show(out);}void Network::Read( ifstream& in ){ string Flag; Target* tempTarget; CloudVector::iterator cloudIt = clouds.begin(); CloudVector::iterator cloudEnd = clouds.end(); while (!in.fail()) { operator>>(in, Flag); if ((Flag == "conjunctions") && (!in.fail())) { string status; operator>>(in, status); if (status == "on") globalParams.generateConjunctions = CONJUNCTIONS_ON; else if (status == "off") globalParams.generateConjunctions = CONJUNCTIONS_OFF; } else if ((Flag == "target") && (!in.fail())) { tempTarget = new Target(globalParams); tempTarget->Read(in, algorithms); globalParams.targetIds.insert(tempTarget->Id()); // step through Clouds, looking for a matching targetID for (cloudIt = clouds.begin(), cloudEnd = clouds.end(); (cloudIt != cloudEnd) && (cloudIt->Id() != tempTarget->Id()); ++cloudIt); if (cloudIt == cloudEnd) { // no Cloud exists with the correct targetID, so create one and add // the Target to it clouds.push_back(Cloud(tempTarget->Id(), globalParams)); clouds.back().AddTarget(*tempTarget); } else { // we've found a Cloud with the correct targetID, so just add the new // Target to it cloudIt->AddTarget(*tempTarget); } delete tempTarget; } } if (globalParams.onlineLearning && globalParams.constraintClassification) { globalParams.targetIdsArray = new FeatureID[clouds.size()]; CloudVector::iterator it; CloudVector::iterator end = clouds.end(); int i; for (i = 0, it = clouds.begin(); it != end; ++it, ++i) { TargetIDToCloud[it->Id()] = VectorCloudIterator_Bool(it); globalParams.targetIdsArray[i] = it->Id(); } }}void Network::Write( ofstream& out ){ if (globalParams.generateConjunctions == CONJUNCTIONS_ON)#ifdef WIN32 // This looks weird, but it is a work around for the NT version. Due to a // bug in MSVC 5.0 you have to call the insertion operator explicitly. operator<<(out, "conjunctions on\n");#else out << "conjunctions on\n";#endif // Walk through clouds and write them out CloudVector::iterator it = clouds.begin(); CloudVector::iterator end = clouds.end(); for (; it != end; ++it) { it->Write(out); out << endl; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -