.. _program_listing_file_src_layers_loss.cpp: Program Listing for File loss.cpp ================================= |exhale_lsh| :ref:`Return to documentation for file ` (``src/layers/loss.cpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp #include "layers/loss.h" namespace marian { // @TODO, simplify this. Currently here for back-compat Ptr newLoss(Ptr options, bool inference) { float smoothing = inference ? 0.f : options->get("label-smoothing"); float factorWeight = options->get("factor-weight", 1.0f); std::string costType = options->get("cost-type", "ce-mean"); bool unlikelihood = options->get("unlikelihood-loss", false); if(costType == "ce-rescore") { // per-batch-item scores (while ce-mean reduces over batch) bool wordScores = options->get("word-scores", false); return New(wordScores); } else if(unlikelihood) { ABORT_IF( !options->hasAndNotEmpty("data-weighting") && options->get("data-weighting-type") != "word", "Unlikelihood loss training requires error annotation in form of per-target-label scores"); return New( smoothing, factorWeight); // this is a mix of CE-loss and unlikelihood less depending on // values given for data-weighting } else { // same as ce-mean --@TODO: better check all allowed values, and fail for invalid ones. // E.g. what about ce-sum? return New(smoothing, factorWeight); } } // see loss.h for detailed explanations of each class Ptr newMultiLoss(Ptr options) { std::string multiLossType = options->get("multi-loss-type", "sum"); if(multiLossType == "sum") // sum of sums return New(); else if(multiLossType == "scaled") // sum of scaled sums, first element is reference scale return New(); else if(multiLossType == "mean") // sum of means return New(); else ABORT("Unknown multi-loss-type {}", multiLossType); } } // namespace marian