fix crash in hypergraph mira for hiero

This commit is contained in:
Barry Haddow 2014-12-17 17:41:29 +00:00
parent 71756cca40
commit e5a91812e9
4 changed files with 17 additions and 11 deletions

View File

@ -341,7 +341,7 @@ void Viterbi(const Graph& graph, const SparseVector& weights, float bleuWeight,
HgBleuScorer bleuScorer(references, graph, sentenceId, backgroundBleu);
vector<FeatureStatsType> winnerStats(kBleuNgramOrder*2+1);
for (size_t vi = 0; vi < graph.VertexSize(); ++vi) {
//cerr << "vertex id " << vi << endl;
// cerr << "vertex id " << vi << endl;
FeatureStatsType winnerScore = kMinScore;
const Vertex& vertex = graph.GetVertex(vi);
const vector<const Edge*>& incoming = vertex.GetIncoming();
@ -349,7 +349,7 @@ void Viterbi(const Graph& graph, const SparseVector& weights, float bleuWeight,
//UTIL_THROW(HypergraphException, "Vertex " << vi << " has no incoming edges");
//If no incoming edges, vertex is a dead end
backPointers[vi].first = NULL;
backPointers[vi].second = kMinScore/2;
backPointers[vi].second = kMinScore;
} else {
//cerr << "\nVertex: " << vi << endl;
for (size_t ei = 0; ei < incoming.size(); ++ei) {
@ -357,9 +357,9 @@ void Viterbi(const Graph& graph, const SparseVector& weights, float bleuWeight,
FeatureStatsType incomingScore = incoming[ei]->GetScore(weights);
for (size_t i = 0; i < incoming[ei]->Children().size(); ++i) {
size_t childId = incoming[ei]->Children()[i];
UTIL_THROW_IF(backPointers[childId].second == kMinScore,
HypergraphException, "Graph was not topologically sorted. curr=" << vi << " prev=" << childId);
incomingScore += backPointers[childId].second;
//UTIL_THROW_IF(backPointers[childId].second == kMinScore,
// HypergraphException, "Graph was not topologically sorted. curr=" << vi << " prev=" << childId);
incomingScore = max(incomingScore + backPointers[childId].second, kMinScore);
}
vector<FeatureStatsType> bleuStats(kBleuNgramOrder*2+1);
// cerr << "Score: " << incomingScore << " Bleu: ";
@ -394,9 +394,12 @@ void Viterbi(const Graph& graph, const SparseVector& weights, float bleuWeight,
//update with winner
//if (bleuWeight) {
//TODO: Not sure if we need this when computing max-model solution
bleuScorer.UpdateState(*(backPointers[vi].first), vi, winnerStats);
if (backPointers[vi].first) {
bleuScorer.UpdateState(*(backPointers[vi].first), vi, winnerStats);
}
}
// cerr << "backpointer[" << vi << "] = (" << backPointers[vi].first << "," << backPointers[vi].second << ")" << endl;
}
//expand back pointers

View File

@ -182,6 +182,7 @@ HypergraphHopeFearDecoder::HypergraphHopeFearDecoder
for (fs::directory_iterator di(hypergraphDir); di != dend; ++di) {
const fs::path& hgpath = di->path();
if (hgpath.filename() == kWeights) continue;
// cerr << "Reading " << hgpath.filename() << endl;
Graph graph(vocab_);
size_t id = boost::lexical_cast<size_t>(hgpath.stem().string());
util::scoped_fd fd(util::OpenReadOrThrow(hgpath.string().c_str()));
@ -195,7 +196,7 @@ HypergraphHopeFearDecoder::HypergraphHopeFearDecoder
prunedGraph.reset(new Graph(vocab_));
graph.Prune(prunedGraph.get(), weights, edgeCount);
graphs_[id] = prunedGraph;
//cerr << "Pruning to v=" << graphs_[id]->VertexSize() << " e=" << graphs_[id]->EdgeSize() << endl;
// cerr << "Pruning to v=" << graphs_[id]->VertexSize() << " e=" << graphs_[id]->EdgeSize() << endl;
++fileCount;
if (fileCount % 10 == 0) cerr << ".";
if (fileCount % 400 == 0) cerr << " [count=" << fileCount << "]\n";
@ -325,6 +326,7 @@ void HypergraphHopeFearDecoder::MaxModel(const AvgWeightVector& wv, vector<ValTy
SparseVector weights;
wv.ToSparse(&weights);
vector<ValType> bg(scorer_->NumberOfScores());
//cerr << "Calculating bleu on " << sentenceId << endl;
Viterbi(*(graphs_[sentenceId]), weights, 0, references_, sentenceId, bg, &bestHypo);
stats->resize(bestHypo.bleuStats.size());
/*

View File

@ -235,7 +235,7 @@ void Graph::Prune(Graph* pNewGraph, const SparseVector& weights, size_t minEdgeC
map<size_t,size_t> oldIdToNew;
size_t vi = 0;
for (set<size_t>::const_iterator i = retainedVertices.begin(); i != retainedVertices.end(); ++i, ++vi) {
//cerr << *i << " New: " << vi << endl;
// cerr << *i << " New: " << vi << endl;
oldIdToNew[*i] = vi;
Vertex* vertex = newGraph.NewVertex();
vertex->SetSourceCovered(vertices_[*i].SourceCovered());
@ -255,6 +255,7 @@ void Graph::Prune(Graph* pNewGraph, const SparseVector& weights, size_t minEdgeC
newHead.AddEdge(newEdge);
}
/*
cerr << "New graph" << endl;
for (size_t vi = 0; vi < newGraph.VertexSize(); ++vi) {
@ -274,8 +275,8 @@ void Graph::Prune(Graph* pNewGraph, const SparseVector& weights, size_t minEdgeC
}
cerr << endl;
}
*/
*/
}

View File

@ -41,7 +41,7 @@ namespace MosesTuning {
typedef unsigned int WordIndex;
const WordIndex kMaxWordIndex = UINT_MAX;
const FeatureStatsType kMinScore = -std::numeric_limits<FeatureStatsType>::max();
const FeatureStatsType kMinScore = -1e10;
template <class T> class FixedAllocator : boost::noncopyable {
public: