Sqrt mse loss

From wenmin-wu/ds-skills · View interactive version

Sqrt mse loss is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Uses square root of MSE as training loss to directly optimize for RMSE evaluation metric alignment.

How to use this skill

Skill file

Sqrt MSE Loss

Overview

When the competition metric is RMSE, training with standard MSE can misalign gradients because MSE squares the errors. Using sqrt(MSE) as the loss directly optimizes the metric you're evaluated on. This often improves final scores by 0.01-0.03 RMSE compared to plain MSE, especially when error magnitudes vary.

Quick Start

import torch
import torch.nn as nn

class RMSELoss(nn.Module):
    def __init__(self, eps=1e-8):
        super().__init__()
        self.mse = nn.MSELoss()
        self.eps = eps

    def forward(self, preds, targets):
        return torch.sqrt(self.mse(preds, targets) + self.eps)

# Usage
criterion = RMSELoss()
loss = criterion(model_output.view(-1), labels.view(-1))
loss.backward()

Workflow

  1. Replace nn.MSELoss() with RMSELoss() in training loop
  2. Add small epsilon inside sqrt for numerical stability
  3. Train as usual — gradients are automatically adjusted

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.