Skip to content
Back to skills

Optuna Ensemble Weights

ASecurity

Learns optimal per-model blending weights via Optuna optimization, supporting negative weights for error cancellation.

  • 61 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added September 12, 2026
code-qualitypython

Security analysis

A100/100

Scanned September 12, 2026

npx -y skills add wenmin-wu/ds-skills --skill optuna-ensemble-weights --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Optuna Ensemble Weights?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Optuna Ensemble Weights
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/wenmin-wu-optuna-ensemble-weights/badge)](https://www.skillsdirectory.com/skills/wenmin-wu-optuna-ensemble-weights)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: nlp-optuna-ensemble-weights
description: >
  Learns optimal per-model blending weights via Optuna optimization, supporting negative weights for error cancellation.
---
# Optuna Ensemble Weights

## Overview

Instead of equal-weight averaging across models, use Optuna to search for optimal per-model weights that minimize the validation metric. Weights can be negative — allowing error cancellation between correlated models. Optimize separately per target when targets have different optimal blends.

## Quick Start

```python
import optuna
import numpy as np

def optimize_weights(predictions, labels, n_models, target_name):
    def objective(trial):
        weights = [trial.suggest_float(f"w{i}", -0.5, 1.0) for i in range(n_models)]
        blended = np.stack([p * w for p, w in zip(predictions, weights)]).sum(0)
        rmse = np.sqrt(np.mean((blended - labels) ** 2))
        return rmse

    study = optuna.create_study(direction="minimize")
    study.optimize(objective, n_trials=200, show_progress_bar=False)
    return [study.best_params[f"w{i}"] for i in range(n_models)]

# Per-target optimization
content_weights = optimize_weights(content_preds, content_labels, 4, "content")
wording_weights = optimize_weights(wording_preds, wording_labels, 4, "wording")
```

## Workflow

1. Collect OOF predictions from each model architecture
2. Define Optuna objective: weighted sum → RMSE on validation
3. Search weight space (allow negative for error cancellation)
4. Optimize separately per target
5. Apply learned weights at inference time

## Key Decisions

- **Negative weights**: Allow [-0.5, 1.0] range; enables cancellation of correlated errors
- **Per-target weights**: Different targets may prefer different model mixes
- **n_trials**: 200 is usually sufficient for 3-5 models
- **vs grid search**: Optuna's TPE sampler is more efficient in continuous weight space

## References

- CommonLit - Evaluate Student Summaries (Kaggle)
- Source: [infer-2x-t4](https://www.kaggle.com/code/nbroad/infer-2x-t4)

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.