tracel-ai/burn · error

Dropout probability should be between 0 and 1, but got {}

Error message

Dropout probability should be between 0 and 1, but got {}

What it means

DropoutConfig::init validates that the dropout probability is within [0.0, 1.0]. A probability outside this range is statistically meaningless and breaks the Bernoulli sampling math, so construction panics with the invalid value.

Source

Thrown at crates/burn-nn/src/modules/dropout.rs:37

/// The input is also scaled during training to `1 / (1 - prob_keep)`.
///
/// Should be created with [DropoutConfig].
#[derive(Module, Debug)]
#[module(custom_display)]
pub struct Dropout {
    /// The probability of randomly zeroes some elements of the input tensor during training.
    pub prob: f64,
    /// Whether to behave as during training. Cleared by
    /// [`freeze`](burn::module::Module::freeze) and matching
    /// [`freeze_group`](burn::module::Module::freeze_group) traversals.
    pub training: Param<Flag>,
}

impl DropoutConfig {
    /// Initialize a new [dropout](Dropout) module.
    pub fn init(&self) -> Dropout {
        if self.prob < 0.0 || self.prob > 1.0 {
            panic!(
                "Dropout probability should be between 0 and 1, but got {}",
                self.prob
            );
        }
        Dropout {
            prob: self.prob,
            training: Param::from_bool(true),
        }
    }
}

impl Dropout {
    /// Applies the forward pass on the input tensor.
    ///
    /// See [Dropout](Dropout) for more information.
    ///
    /// # Shapes
    ///

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Clamp the probability before constructing: `prob.clamp(0.0, 1.0)`.
  2. Pass a fraction in [0, 1] (e.g. 0.5 for 50% dropout), not a percentage.
  3. Validate config values at load time (e.g. with serde deserialization validation) before init().

Example fix

// before
let config = DropoutConfig::new(30.0); // percent, invalid
// after
let config = DropoutConfig::new(0.3); // or (30.0_f64 / 100.0).clamp(0.0, 1.0)
Defensive patterns

Strategy: validation

Validate before calling

fn validate_dropout_prob(prob: f64) -> f64 {
    assert!((0.0..=1.0).contains(&prob), "dropout prob must be in [0,1], got {prob}");
    prob
}
let config = DropoutConfig::new(validate_dropout_prob(raw_prob));

Type guard

fn is_valid_probability(p: f64) -> bool {
    p.is_finite() && (0.0..=1.0).contains(&p)
}

Try / catch

let result = std::panic::catch_unwind(|| config.init());
match result {
    Ok(dropout) => dropout,
    Err(_) => DropoutConfig::new(config.prob.clamp(0.0, 1.0)).init(),
}

Prevention

When it happens

Trigger: Calling `DropoutConfig::new(prob).init()` (or `init()` on a deserialized config) where prob < 0.0 or prob > 1.0, e.g. DropoutConfig::new(1.5) or new(-0.1).

Common situations: Percent-vs-fraction confusion (passing 30 instead of 0.3); deserializing a JSON/YAML hyperparameter with an out-of-range value; sign or unit mistakes when computing probability programmatically.

Related errors


AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05). Data as JSON: /api/errors/70d569e512bdb9ab. Report an issue: GitHub.