2008-06-11 14:52:57 +04:00
|
|
|
// $Id$
|
2010-09-17 18:25:08 +04:00
|
|
|
#include <vector>
|
2010-10-07 02:06:49 +04:00
|
|
|
|
2008-06-11 14:52:57 +04:00
|
|
|
#include "ScoreComponentCollection.h"
|
|
|
|
#include "StaticData.h"
|
|
|
|
|
2010-10-07 02:06:49 +04:00
|
|
|
using namespace std;
|
|
|
|
|
2008-10-09 03:51:26 +04:00
|
|
|
namespace Moses
|
|
|
|
{
|
2011-11-09 21:16:02 +04:00
|
|
|
|
|
|
|
ScoreComponentCollection::ScoreIndexMap ScoreComponentCollection::s_scoreIndexes;
|
|
|
|
size_t ScoreComponentCollection::s_denseVectorSize = 0;
|
|
|
|
|
|
|
|
ScoreComponentCollection::ScoreComponentCollection() : m_scores(s_denseVectorSize)
|
2008-06-11 14:52:57 +04:00
|
|
|
{}
|
|
|
|
|
2010-09-28 19:13:50 +04:00
|
|
|
|
2011-11-09 21:16:02 +04:00
|
|
|
void ScoreComponentCollection::RegisterScoreProducer
|
|
|
|
(const ScoreProducer* scoreProducer)
|
|
|
|
{
|
2011-12-09 13:30:48 +04:00
|
|
|
CHECK(scoreProducer->GetNumScoreComponents() != ScoreProducer::unlimited);
|
2011-11-09 21:16:02 +04:00
|
|
|
size_t start = s_denseVectorSize;
|
|
|
|
size_t end = start + scoreProducer->GetNumScoreComponents();
|
2011-11-16 17:08:24 +04:00
|
|
|
VERBOSE(1, "ScoreProducer: " << scoreProducer->GetScoreProducerDescription() << " start: " << start << " end: " << end << endl);
|
2011-11-09 21:16:02 +04:00
|
|
|
s_scoreIndexes[scoreProducer] = pair<size_t,size_t>(start,end);
|
|
|
|
s_denseVectorSize = end;
|
|
|
|
}
|
|
|
|
|
2011-11-16 15:49:31 +04:00
|
|
|
void ScoreComponentCollection::UnregisterScoreProducer
|
|
|
|
(const ScoreProducer* scoreProducer)
|
|
|
|
{
|
2011-12-09 13:30:48 +04:00
|
|
|
CHECK(scoreProducer->GetNumScoreComponents() != ScoreProducer::unlimited);
|
2011-11-16 15:49:31 +04:00
|
|
|
ScoreIndexMap::iterator iter = s_scoreIndexes.find(scoreProducer);
|
2011-12-09 13:30:48 +04:00
|
|
|
CHECK(iter != s_scoreIndexes.end());
|
2011-11-16 15:49:31 +04:00
|
|
|
s_scoreIndexes.erase(iter);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-04-08 21:16:10 +04:00
|
|
|
float ScoreComponentCollection::GetWeightedScore() const
|
|
|
|
{
|
2010-10-07 02:06:49 +04:00
|
|
|
return m_scores.inner_product(StaticData::Instance().GetAllWeights().m_scores);
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2010-08-10 17:12:00 +04:00
|
|
|
void ScoreComponentCollection::ZeroAllLM(const LMList& lmList)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2010-10-07 02:06:49 +04:00
|
|
|
for (LMList::const_iterator i = lmList.begin(); i != lmList.end(); ++i) {
|
|
|
|
Assign(*i, 0);
|
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2010-08-10 17:12:00 +04:00
|
|
|
void ScoreComponentCollection::PlusEqualsAllLM(const LMList& lmList, const ScoreComponentCollection& rhs)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2010-10-07 02:06:49 +04:00
|
|
|
for (LMList::const_iterator i = lmList.begin(); i != lmList.end(); ++i) {
|
|
|
|
PlusEquals(*i,rhs);
|
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
2010-09-16 13:54:13 +04:00
|
|
|
|
2010-09-17 19:20:55 +04:00
|
|
|
void ScoreComponentCollection::MultiplyEquals(float scalar)
|
|
|
|
{
|
|
|
|
m_scores *= scalar;
|
|
|
|
}
|
2010-10-28 16:41:33 +04:00
|
|
|
|
2011-11-24 23:27:12 +04:00
|
|
|
// Multiply all weights of this sparse producer by a given scalar
|
|
|
|
void ScoreComponentCollection::MultiplyEquals(const ScoreProducer* sp, float scalar) {
|
|
|
|
assert(sp->GetNumScoreComponents() == ScoreProducer::unlimited);
|
2012-01-09 16:04:18 +04:00
|
|
|
std::string prefix = sp->GetScoreProducerDescription() + FName::SEP;
|
2011-11-24 23:27:12 +04:00
|
|
|
for(FVector::FNVmap::const_iterator i = m_scores.cbegin(); i != m_scores.cend(); i++) {
|
|
|
|
std::stringstream name;
|
|
|
|
name << i->first;
|
|
|
|
if (name.str().substr( 0, prefix.length() ).compare( prefix ) == 0)
|
|
|
|
m_scores[i->first] = i->second * scalar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-01 16:22:12 +04:00
|
|
|
// Count weights belonging to this sparse producer
|
|
|
|
size_t ScoreComponentCollection::GetNumberWeights(const ScoreProducer* sp) {
|
|
|
|
assert(sp->GetNumScoreComponents() == ScoreProducer::unlimited);
|
|
|
|
std::string prefix = sp->GetScoreProducerDescription() + FName::SEP;
|
|
|
|
size_t weights = 0;
|
|
|
|
for(FVector::FNVmap::const_iterator i = m_scores.cbegin(); i != m_scores.cend(); i++) {
|
|
|
|
std::stringstream name;
|
|
|
|
name << i->first;
|
|
|
|
if (name.str().substr( 0, prefix.length() ).compare( prefix ) == 0)
|
|
|
|
weights++;
|
|
|
|
}
|
|
|
|
return weights;
|
|
|
|
}
|
|
|
|
|
2010-10-28 16:41:33 +04:00
|
|
|
void ScoreComponentCollection::DivideEquals(float scalar)
|
|
|
|
{
|
|
|
|
m_scores /= scalar;
|
|
|
|
}
|
2011-08-06 18:10:43 +04:00
|
|
|
|
2012-03-14 21:59:59 +04:00
|
|
|
void ScoreComponentCollection::CoreDivideEquals(float scalar)
|
|
|
|
{
|
|
|
|
m_scores.coreDivideEquals(scalar);
|
|
|
|
}
|
|
|
|
|
2012-03-13 20:34:48 +04:00
|
|
|
void ScoreComponentCollection::DivideEquals(const ScoreComponentCollection& rhs)
|
|
|
|
{
|
|
|
|
m_scores.divideEquals(rhs.m_scores);
|
|
|
|
}
|
|
|
|
|
2010-09-17 19:33:21 +04:00
|
|
|
void ScoreComponentCollection::MultiplyEquals(const ScoreComponentCollection& rhs)
|
|
|
|
{
|
|
|
|
m_scores *= rhs.m_scores;
|
|
|
|
}
|
|
|
|
|
2010-09-16 11:06:42 +04:00
|
|
|
std::ostream& operator<<(std::ostream& os, const ScoreComponentCollection& rhs)
|
|
|
|
{
|
2010-10-07 02:06:49 +04:00
|
|
|
os << "<<" << rhs.m_scores;
|
2010-09-16 11:06:42 +04:00
|
|
|
return os << ">>";
|
|
|
|
}
|
2010-09-28 19:13:50 +04:00
|
|
|
void ScoreComponentCollection::L1Normalise() {
|
|
|
|
m_scores /= m_scores.l1norm();
|
|
|
|
}
|
|
|
|
|
2011-11-16 15:49:31 +04:00
|
|
|
float ScoreComponentCollection::GetL1Norm() const {
|
2011-05-31 19:39:48 +04:00
|
|
|
return m_scores.l1norm();
|
|
|
|
}
|
|
|
|
|
2011-11-16 15:49:31 +04:00
|
|
|
float ScoreComponentCollection::GetL2Norm() const {
|
2011-04-23 16:13:43 +04:00
|
|
|
return m_scores.l2norm();
|
|
|
|
}
|
|
|
|
|
2011-12-23 14:55:54 +04:00
|
|
|
float ScoreComponentCollection::GetLInfNorm() const {
|
|
|
|
return m_scores.linfnorm();
|
|
|
|
}
|
|
|
|
|
2011-11-16 15:49:31 +04:00
|
|
|
void ScoreComponentCollection::Save(ostream& out) const {
|
2011-11-16 13:13:17 +04:00
|
|
|
ScoreIndexMap::const_iterator iter = s_scoreIndexes.begin();
|
|
|
|
for (; iter != s_scoreIndexes.end(); ++iter ) {
|
2011-11-16 15:49:31 +04:00
|
|
|
string name = iter->first->GetScoreProducerDescription();
|
|
|
|
IndexPair ip = iter->second; // feature indices
|
2011-11-17 18:55:10 +04:00
|
|
|
if (ip.second-ip.first == 1) {
|
|
|
|
out << name << " " << m_scores[ip.first] << endl;
|
|
|
|
} else {
|
|
|
|
for (size_t i=ip.first; i < ip.second; ++i) {
|
|
|
|
ostringstream fullname;
|
|
|
|
fullname << name << "_" << (i + 1 - ip.first);
|
|
|
|
out << fullname.str() << " " << m_scores[i] << endl;
|
|
|
|
}
|
2011-11-16 15:49:31 +04:00
|
|
|
}
|
2011-11-16 13:13:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// write sparse features
|
|
|
|
m_scores.write(out);
|
2011-11-16 15:49:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScoreComponentCollection::Save(const string& filename) const {
|
|
|
|
ofstream out(filename.c_str());
|
|
|
|
if (!out) {
|
|
|
|
ostringstream msg;
|
|
|
|
msg << "Unable to open " << filename;
|
|
|
|
throw runtime_error(msg.str());
|
|
|
|
}
|
|
|
|
Save(out);
|
2011-11-16 13:13:17 +04:00
|
|
|
out.close();
|
|
|
|
}
|
|
|
|
|
2011-09-20 19:32:26 +04:00
|
|
|
void ScoreComponentCollection::Assign(const ScoreProducer* sp, const string line) {
|
2011-12-09 13:30:48 +04:00
|
|
|
CHECK(sp->GetNumScoreComponents() == ScoreProducer::unlimited);
|
2011-09-20 19:32:26 +04:00
|
|
|
istringstream istr(line);
|
|
|
|
while(istr) {
|
|
|
|
string namestring;
|
|
|
|
FValue value;
|
|
|
|
istr >> namestring;
|
|
|
|
if (!istr) break;
|
|
|
|
istr >> value;
|
|
|
|
FName fname(sp->GetScoreProducerDescription(), namestring);
|
|
|
|
m_scores[fname] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-09 03:51:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|