double MaxEntModel::getObsCounts(EventSet& events, vector<double>& obsCounts) { vector<double> probs; obsCounts.clear(); obsCounts.assign(_lambda.size(), 0); double maxFtSum = 0; for (unsigned int i = 0; i<events.size(); i++) { MaxEntEvent& e = *events[i]; unsigned long c = e.classId(); double count = e.count(); double ftSum = 0; for (unsigned long j=0; j<e.size(); j++) { FtMap::iterator it = _index.find(e[j]); if (it!=_index.end()) obsCounts[it->second+c] += count; else { // new feature, need to expand obsCounts and _lambda for (unsigned int k = 0; k<_classes; k++) obsCounts.push_back(0); obsCounts[_lambda.size()+c] += count; addFeature(e[j]); } ftSum++; } if (ftSum>maxFtSum) maxFtSum = ftSum; } return maxFtSum; }
// In any order many at a time: bool waitForEventSet(const EventSet& evts, Duration timeout=Duration::seconds(5)) { EventSet read_evts; for(uint idx = 0; idx < evts.size(); idx++) { Event read_evt; bool got_event = _events.blockingPop(read_evt, timeout); TS_ASSERT(got_event); if (!got_event) return false; read_evts.insert(read_evt); } for(EventSet::iterator evt_it = evts.begin(), read_evt_it = read_evts.begin(); evt_it != evts.end(), read_evt_it != read_evts.end(); evt_it++, read_evt_it++) { TS_ASSERT_EQUALS(*evt_it, *read_evt_it); if (*evt_it != *read_evt_it) return false; } return true; }
double MaxEntTrainer::test(EventSet& events, MaxEntModel& model) { vector<double> probs; double total=0, error = 0; for (unsigned int i = 0; i<events.size(); i++) { int c = model.getProbs(*events[i], probs); if (c!=(int)events[i]->classId()) { error++; if (_printDetails) cerr << '*'; } if (_printDetails) { cerr << className(events[i]->classId()) << '\t'; for (unsigned int cl = 0; cl<probs.size(); cl++) { cerr << className(cl) << ' ' << probs[cl] << '\t'; } cerr << endl; } total++; } return error/total; }
double MaxEntModel::getExpects(EventSet& events, vector<double>& expects) { expects.clear(); expects.assign(_lambda.size(), 0); double sumLogProb = 0; for (unsigned int i = 0; i<events.size(); i++) { MaxEntEvent& e = *events[i]; vector<double> probs; getProbs(e, probs); for (unsigned long c = 0; c < _classes; ++c) { double count = probs[c] * e.count(); for (unsigned long j = 0; j < e.size(); j++) { FtMap::iterator it = _index.find(e[j]); if (it!=_index.end()) expects[it->second + c] += count; } } sumLogProb += log(probs[e.classId()]); } return sumLogProb; }