Incremental scoring of hypotheses.

Don't score full score vectors in hypotheses, just
the increments. The full score breakdowns (required for
nbest lists) are calculated lazily.
This commit is contained in:
Barry Haddow 2014-08-05 17:57:57 +01:00
parent 58a5b9eae3
commit 6549d24d35
2 changed files with 21 additions and 9 deletions

View File

@ -85,14 +85,13 @@ Hypothesis::Hypothesis(const Hypothesis &prevHypo, const TranslationOption &tran
, m_wordDeleted(false)
, m_totalScore(0.0f)
, m_futureScore(0.0f)
, m_scoreBreakdown(prevHypo.GetScoreBreakdown())
, m_ffStates(prevHypo.m_ffStates.size())
, m_arcList(NULL)
, m_transOpt(transOpt)
, m_manager(prevHypo.GetManager())
, m_id(m_manager.GetNextHypoId())
{
m_scoreBreakdown.PlusEquals(transOpt.GetScoreBreakdown());
m_currScoreBreakdown.PlusEquals(transOpt.GetScoreBreakdown());
// assert that we are not extending our hypothesis by retranslating something
// that this hypothesis has already translated!
@ -214,7 +213,7 @@ void Hypothesis::EvaluateWith(const StatefulFeatureFunction &sfff,
m_ffStates[state_idx] = sfff.EvaluateWhenApplied(
*this,
m_prevHypo ? m_prevHypo->m_ffStates[state_idx] : NULL,
&m_scoreBreakdown);
&m_currScoreBreakdown);
}
}
@ -222,7 +221,7 @@ void Hypothesis::EvaluateWith(const StatelessFeatureFunction& slff)
{
const StaticData &staticData = StaticData::Instance();
if (! staticData.IsFeatureFunctionIgnored( slff )) {
slff.EvaluateWhenApplied(*this, &m_scoreBreakdown);
slff.EvaluateWhenApplied(*this, &m_currScoreBreakdown);
}
}
@ -256,7 +255,7 @@ void Hypothesis::Evaluate(const SquareMatrix &futureScore)
if (! staticData.IsFeatureFunctionIgnored(ff)) {
m_ffStates[i] = ff.EvaluateWhenApplied(*this,
m_prevHypo ? m_prevHypo->m_ffStates[i] : NULL,
&m_scoreBreakdown);
&m_currScoreBreakdown);
}
}
@ -269,7 +268,8 @@ void Hypothesis::Evaluate(const SquareMatrix &futureScore)
m_futureScore = futureScore.CalcFutureScore( m_sourceCompleted );
// TOTAL
m_totalScore = m_scoreBreakdown.GetWeightedScore() + m_futureScore;
m_totalScore = m_currScoreBreakdown.GetWeightedScore() + m_futureScore;
if (m_prevHypo) m_totalScore += m_prevHypo->GetScore();
IFVERBOSE(2) {
m_manager.GetSentenceStats().StopTimeEstimateScore();
@ -315,7 +315,7 @@ void Hypothesis::PrintHypothesis() const
// TRACE_ERR( "\tlanguage model cost "); // <<m_score[ScoreType::LanguageModelScore]<<endl;
// TRACE_ERR( "\tword penalty "); // <<(m_score[ScoreType::WordPenalty]*weightWordPenalty)<<endl;
TRACE_ERR( "\tscore "<<m_totalScore - m_futureScore<<" + future cost "<<m_futureScore<<" = "<<m_totalScore<<endl);
TRACE_ERR( "\tunweighted feature scores: " << m_scoreBreakdown << endl);
TRACE_ERR( "\tunweighted feature scores: " << m_currScoreBreakdown << endl);
//PrintLMScores();
}

View File

@ -25,6 +25,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include <iostream>
#include <memory>
#include <boost/scoped_ptr.hpp>
#include <vector>
#include "Phrase.h"
#include "TypeDef.h"
@ -77,7 +80,9 @@ protected:
bool m_wordDeleted;
float m_totalScore; /*! score so far */
float m_futureScore; /*! estimated future cost to translate rest of sentence */
ScoreComponentCollection m_scoreBreakdown; /*! scores for this hypothesis */
/*! sum of scores of this hypothesis, and previous hypotheses. Lazily initialised. */
mutable boost::scoped_ptr<ScoreComponentCollection> m_scoreBreakdown;
ScoreComponentCollection m_currScoreBreakdown; /*! scores for this hypothesis only */
std::vector<const FFState*> m_ffStates;
const Hypothesis *m_winningHypo;
ArcList *m_arcList; /*! all arcs that end at the same trellis point as this hypothesis */
@ -228,7 +233,14 @@ public:
return m_arcList;
}
const ScoreComponentCollection& GetScoreBreakdown() const {
return m_scoreBreakdown;
if (!m_scoreBreakdown.get()) {
m_scoreBreakdown.reset(new ScoreComponentCollection());
m_scoreBreakdown->PlusEquals(m_currScoreBreakdown);
if (m_prevHypo) {
m_scoreBreakdown->PlusEquals(m_prevHypo->GetScoreBreakdown());
}
}
return *(m_scoreBreakdown.get());
}
float GetTotalScore() const {
return m_totalScore;