{"record":{"id":"47073226a3228d74","repo":"deepfakes/faceswap","slug":"name-is-not-a-valid-loss-function-choose-from","errorCode":null,"errorMessage":"'{name}' is not a valid Loss function. Choose from: {list(valid)}","messagePattern":"'(.+?)' is not a valid Loss function\\. Choose from: (.+?)","errorType":"validation","errorClass":"FaceswapError","httpStatus":null,"severity":"error","filePath":"lib/model/losses/__init__.py","lineNumber":46,"sourceCode":"    The requested Torch Loss function\n    \"\"\"\n    valid = {\"ffl\": FocalFrequencyLoss,\n             \"flip\": LDRFLIPLoss,\n             \"gmsd\": GMSDLoss,\n             \"l_inf_norm\": LInfNorm,\n             \"laploss\": LaplacianPyramidLoss,\n             \"logcosh\": LogCosh,\n             \"lpips_alex\": LPIPSLoss,\n             \"lpips_squeeze\": LPIPSLoss,\n             \"lpips_vgg16\": LPIPSLoss,\n             \"ms_ssim\": MSSIMLoss,\n             \"mae\": nn.L1Loss,\n             \"mse\": nn.MSELoss,\n             \"pixel_gradient_diff\": GradientLoss,\n             \"ssim\": SSIMLoss,\n             \"smooth_loss\": GeneralizedLoss}\n    if name not in valid:\n        raise FaceswapError(f\"'{name}' is not a valid Loss function. Choose from: {list(valid)}\")\n\n    kwargs: dict[str, T.Any] = {}\n    if name in (\"mae\", \"mse\"):\n        kwargs[\"reduction\"] = \"none\"\n    if name == \"flip\" or name.startswith(\"lpips_\"):\n        kwargs[\"color_order\"] = color_order\n    if name.startswith(\"lpips_\"):\n        kwargs[\"trunk_network\"] = name.rsplit(\"_\", maxsplit=1)[-1]\n        kwargs[\"crop\"] = True\n    return valid[name](**kwargs)\n","sourceCodeStart":28,"sourceCodeEnd":57,"githubUrl":"https://github.com/deepfakes/faceswap/blob/f530cb7508ae670f6474f8a7d9c4df94705cf96b/lib/model/losses/__init__.py#L28-L57","documentation":"FaceswapError raised by the loss-function factory in lib/model/losses/__init__.py when the requested loss name is not in the valid mapping (mae, mse, ssim, ms_ssim, lpips_*, flip, logcosh, laploss, pixel_gradient_diff, smooth_loss, etc.). It is a configuration-validation error raised at model build time.","triggerScenarios":"Typoing a loss name in train configuration (e.g. 'ms-ssim' instead of 'ms_ssim', 'l1' instead of 'mae'); using a loss name from an older/newer faceswap version that no longer exists; case sensitivity ('MSE' vs 'mse').","commonSituations":"Hand-editing the model config file or CLI overrides with an invalid loss; copying configs from tutorials for a different faceswap version.","solutions":["Use one of the names listed in the error message exactly (lowercase, underscores).","Check the installed version's valid mapping if unsure — the error prints the full list.","Update stale config files after upgrading faceswap."],"exampleFix":"# model config\n# before\nloss_function = ms-ssim\n# after\nloss_function = ms_ssim","handlingStrategy":"validation","validationCode":"VALID_LOSSES = {'mae','mse','ssim','ms_ssim','lpips_alex','lpips_squeeze',\n                'lpips_vgg16','flip','logcosh','laploss','pixel_gradient_diff',\n                'smooth_loss'}\nassert config.loss_function in VALID_LOSSES, f'pick from {sorted(VALID_LOSSES)}'","typeGuard":"def is_valid_loss(name: str) -> bool:\n    return isinstance(name, str) and name.lower() in VALID_LOSSES","tryCatchPattern":"try:\n    loss_func = LossFactory(config.loss_function)\nexcept FaceswapError as err:\n    if 'not a valid Loss function' in str(err):\n        config.loss_function = 'mse'  # safe default\n        loss_func = LossFactory(config.loss_function)\n    else:\n        raise","preventionTips":["Validate loss names against the printed list at config load time.","Use lowercase underscore names exactly as documented.","Re-validate custom configs after upgrading faceswap."],"tags":["config","training","loss-function","validation"],"backgroundTag":null,"analyzedSha":"f530cb7508ae670f6474f8a7d9c4df94705cf96b","analyzedAt":"2026-08-15T02:59:26.626Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}