{"record":{"id":"b6fd4c36239d25dc","repo":"WZMIAOMIAO/deep-learning-for-image-processing","slug":"not-support-data-format-self-data-format","errorCode":null,"errorMessage":"not support data format '{self.data_format}'","messagePattern":"not support data format '(.+?)'","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pytorch_classification/ConvNeXt/model.py","lineNumber":56,"sourceCode":"    def forward(self, x):\n        return drop_path(x, self.drop_prob, self.training)\n\n\nclass LayerNorm(nn.Module):\n    r\"\"\" LayerNorm that supports two data formats: channels_last (default) or channels_first.\n    The ordering of the dimensions in the inputs. channels_last corresponds to inputs with\n    shape (batch_size, height, width, channels) while channels_first corresponds to inputs\n    with shape (batch_size, channels, height, width).\n    \"\"\"\n\n    def __init__(self, normalized_shape, eps=1e-6, data_format=\"channels_last\"):\n        super().__init__()\n        self.weight = nn.Parameter(torch.ones(normalized_shape), requires_grad=True)\n        self.bias = nn.Parameter(torch.zeros(normalized_shape), requires_grad=True)\n        self.eps = eps\n        self.data_format = data_format\n        if self.data_format not in [\"channels_last\", \"channels_first\"]:\n            raise ValueError(f\"not support data format '{self.data_format}'\")\n        self.normalized_shape = (normalized_shape,)\n\n    def forward(self, x: torch.Tensor) -> torch.Tensor:\n        if self.data_format == \"channels_last\":\n            return F.layer_norm(x, self.normalized_shape, self.weight, self.bias, self.eps)\n        elif self.data_format == \"channels_first\":\n            # [batch_size, channels, height, width]\n            mean = x.mean(1, keepdim=True)\n            var = (x - mean).pow(2).mean(1, keepdim=True)\n            x = (x - mean) / torch.sqrt(var + self.eps)\n            x = self.weight[:, None, None] * x + self.bias[:, None, None]\n            return x\n\n\nclass Block(nn.Module):\n    r\"\"\" ConvNeXt Block. There are two equivalent implementations:\n    (1) DwConv -> LayerNorm (channels_first) -> 1x1 Conv -> GELU -> 1x1 Conv; all in (N, C, H, W)\n    (2) DwConv -> Permute to (N, H, W, C); LayerNorm (channels_last) -> Linear -> GELU -> Linear; Permute back","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/WZMIAOMIAO/deep-learning-for-image-processing/blob/1ec3fe6f374fc9969973a61f819de25658595afa/pytorch_classification/ConvNeXt/model.py#L38-L74","documentation":"LayerNorm (ConvNeXt variant) validates its data_format argument at construction. Only 'channels_last' and 'channels_first' are supported; any other string fails immediately in __init__. This guards the forward pass, which branches on that exact value.","triggerScenarios":"Calling LayerNorm(normalized_shape, eps, data_format='channel_last') or any misspelled/None value instead of 'channels_last' or 'channels_first'.","commonSituations":"Typos like 'channels_last ' (trailing space), 'channel_last', or copying code from a version where the argument was renamed; passing a config value that defaults to None.","solutions":["Pass data_format='channels_last' (NCHW tensors should use 'channels_first').","Check spelling and case of the data_format string against ['channels_last','channels_first'].","Fix any config/dict lookup that supplies a wrong or missing data_format default."],"exampleFix":"// before\nnorm = LayerNorm(dim, eps=1e-6, data_format='channel_last')\n// after\nnorm = LayerNorm(dim, eps=1e-6, data_format='channels_last')","handlingStrategy":"validation","validationCode":"def make_ln(dim, data_format):\n    assert data_format in (\"channels_last\", \"channels_first\"), f\"bad data_format: {data_format!r}\"\n    return LayerNorm(dim, eps=1e-6, data_format=data_format)","typeGuard":"def is_valid_data_format(f) -> bool:\n    return isinstance(f, str) and f in (\"channels_last\", \"channels_first\")","tryCatchPattern":"try:\n    norm = LayerNorm(dim, eps=1e-6, data_format=fmt)\nexcept ValueError as e:\n    print(f\"bad data_format {fmt!r}, defaulting to channels_last\")\n    norm = LayerNorm(dim, eps=1e-6, data_format=\"channels_last\")","preventionTips":["Keep data_format values as module-level constants instead of inline strings","Validate config dicts at load time before instantiating models","Remember NCHW tensors need 'channels_first' in ConvNeXt stems"],"tags":["pytorch","config-validation","argument-error"],"backgroundTag":"invalid-argument-value","analyzedSha":"1ec3fe6f374fc9969973a61f819de25658595afa","analyzedAt":"2026-08-30T09:19:11.901Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}