{"record":{"id":"70d569e512bdb9ab","repo":"tracel-ai/burn","slug":"dropout-probability-should-be-between-0-and-1-but","errorCode":null,"errorMessage":"Dropout probability should be between 0 and 1, but got {}","messagePattern":"Dropout probability should be between 0 and 1, but got (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-nn/src/modules/dropout.rs","lineNumber":37,"sourceCode":"/// The input is also scaled during training to `1 / (1 - prob_keep)`.\n///\n/// Should be created with [DropoutConfig].\n#[derive(Module, Debug)]\n#[module(custom_display)]\npub struct Dropout {\n    /// The probability of randomly zeroes some elements of the input tensor during training.\n    pub prob: f64,\n    /// Whether to behave as during training. Cleared by\n    /// [`freeze`](burn::module::Module::freeze) and matching\n    /// [`freeze_group`](burn::module::Module::freeze_group) traversals.\n    pub training: Param<Flag>,\n}\n\nimpl DropoutConfig {\n    /// Initialize a new [dropout](Dropout) module.\n    pub fn init(&self) -> Dropout {\n        if self.prob < 0.0 || self.prob > 1.0 {\n            panic!(\n                \"Dropout probability should be between 0 and 1, but got {}\",\n                self.prob\n            );\n        }\n        Dropout {\n            prob: self.prob,\n            training: Param::from_bool(true),\n        }\n    }\n}\n\nimpl Dropout {\n    /// Applies the forward pass on the input tensor.\n    ///\n    /// See [Dropout](Dropout) for more information.\n    ///\n    /// # Shapes\n    ///","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-nn/src/modules/dropout.rs#L19-L55","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Clamp the probability before constructing: `prob.clamp(0.0, 1.0)`.","Pass a fraction in [0, 1] (e.g. 0.5 for 50% dropout), not a percentage.","Validate config values at load time (e.g. with serde deserialization validation) before init()."],"exampleFix":"// before\nlet config = DropoutConfig::new(30.0); // percent, invalid\n// after\nlet config = DropoutConfig::new(0.3); // or (30.0_f64 / 100.0).clamp(0.0, 1.0)","handlingStrategy":"validation","validationCode":"fn validate_dropout_prob(prob: f64) -> f64 {\n    assert!((0.0..=1.0).contains(&prob), \"dropout prob must be in [0,1], got {prob}\");\n    prob\n}\nlet config = DropoutConfig::new(validate_dropout_prob(raw_prob));","typeGuard":"fn is_valid_probability(p: f64) -> bool {\n    p.is_finite() && (0.0..=1.0).contains(&p)\n}","tryCatchPattern":"let result = std::panic::catch_unwind(|| config.init());\nmatch result {\n    Ok(dropout) => dropout,\n    Err(_) => DropoutConfig::new(config.prob.clamp(0.0, 1.0)).init(),\n}","preventionTips":["Always express dropout as a fraction (0.3), never a percentage (30).","Clamp probabilities at the config boundary before init().","Validate hyperparameters during deserialization (serde validators) rather than at layer init."],"tags":["rust","panic","dropout","config-validation","range-check"],"backgroundTag":"invalid-probability-range","analyzedSha":"d16f7ba2ed0d41408189384044cc886fb4c8f957","analyzedAt":"2026-09-05T13:19:14.260Z","contentChangedAt":"2026-09-05T13:19:14.260Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}