private static double CalculateBleu(ThotSmtParameters parameters, IReadOnlyList <IReadOnlyList <string> > sourceCorpus, IReadOnlyList <IReadOnlyList <string> > tuneTargetCorpus) { IEnumerable <IReadOnlyList <string> > translations = GenerateTranslations(parameters, sourceCorpus); double bleu = Evaluation.CalculateBleu(translations, tuneTargetCorpus); double penalty = 0; for (int i = 0; i < parameters.ModelWeights.Count; i++) { if (i == 0 || i == 2 || i == 7) { continue; } if (parameters.ModelWeights[i] < 0) { penalty += parameters.ModelWeights[i] * 1000 * -1; } } return((1.0 - bleu) + penalty); }
public ThotSmtParameters Tune(ThotSmtParameters parameters, IReadOnlyList <IReadOnlyList <string> > tuneSourceCorpus, IReadOnlyList <IReadOnlyList <string> > tuneTargetCorpus, SmtBatchTrainStats stats, IProgress <ProgressStatus> progress) { IntPtr weightUpdaterHandle = Thot.llWeightUpdater_create(); try { var iterQualities = new List <double>(); double bestQuality = double.MinValue; ThotSmtParameters bestParameters = null; int iter = 0; HashSet <TranslationInfo>[] curNBestLists = null; float[] curWeights = parameters.ModelWeights.ToArray(); while (true) { progress.Report(new ProgressStatus(iter, MaxIterations)); ThotSmtParameters newParameters = parameters.Clone(); newParameters.ModelWeights = curWeights; newParameters.Freeze(); IList <TranslationInfo>[] nbestLists = GetNBestLists(newParameters, tuneSourceCorpus).ToArray(); double quality = Evaluation.CalculateBleu(nbestLists.Select(nbl => nbl.First().Translation), tuneTargetCorpus); iterQualities.Add(quality); if (quality > bestQuality) { bestQuality = quality; bestParameters = newParameters; } iter++; if (iter >= MaxIterations || IsTuningConverged(iterQualities)) { break; } if (curNBestLists == null) { curNBestLists = nbestLists.Select(nbl => new HashSet <TranslationInfo>(nbl)).ToArray(); } else { for (int i = 0; i < nbestLists.Length; i++) { curNBestLists[i].UnionWith(nbestLists[i]); } } UpdateWeights(weightUpdaterHandle, tuneTargetCorpus, curNBestLists, curWeights); } if (iter < MaxIterations) { progress.Report(new ProgressStatus(1.0)); } stats.TranslationModelBleu = bestQuality; return(bestParameters); } finally { Thot.llWeightUpdater_close(weightUpdaterHandle); } }