{"record":{"id":"0bce53882838db44","repo":"hankcs/HanLP","slug":"unsupported-dim-x-dim-only-2d-t-c-or-3d-b","errorCode":null,"errorMessage":"Unsupported dim: {x.dim()}. Only 2d (T,C) or 3d (B,T,C) is supported","messagePattern":"Unsupported dim: (.+?)\\. Only 2d \\(T,C\\) or 3d \\(B,T,C\\) is supported","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"hanlp/layers/dropout.py","lineNumber":158,"sourceCode":"        return items\n\n\nclass LockedDropout(nn.Module):\n    def __init__(self, dropout_rate=0.5):\n        super(LockedDropout, self).__init__()\n        self.dropout_rate = dropout_rate\n\n    def forward(self, x):\n        if not self.training or not self.dropout_rate:\n            return x\n\n        if x.dim() == 3:\n            mask = x.new(x.size(0), 1, x.size(2)).bernoulli_(1 - self.dropout_rate) / (1 - self.dropout_rate)\n            mask = mask.expand_as(x)\n        elif x.dim() == 2:\n            mask = torch.empty_like(x).bernoulli_(1 - self.dropout_rate) / (1 - self.dropout_rate)\n        else:\n            raise ValueError(f'Unsupported dim: {x.dim()}. Only 2d (T,C) or 3d (B,T,C) is supported')\n        return mask * x\n","sourceCodeStart":140,"sourceCodeEnd":160,"githubUrl":"https://github.com/hankcs/HanLP/blob/ddb1299bddff079e447af52ec12549c50636bfa8/hanlp/layers/dropout.py#L140-L160","documentation":"HanLP's custom dropout (spatial dropout over feature columns) only supports 2-D (T,C) or 3-D (B,T,C) tensors: for 3-D it samples a per-feature mask broadcast over timesteps, for 2-D a plain elementwise mask. Any other rank is rejected because the masking scheme (masking whole channels) is undefined there.","triggerScenarios":"Calling this dropout module's forward on a 1-D vector, a 4-D tensor (e.g. conv feature maps), or a batched 4-D input from a CNN/transformer pipeline.","commonSituations":"Reusing the layer on image or audio tensors; applying it to flattened logits or scalars; inserting it before an unsqueeze/reshape that was expected but omitted.","solutions":["Reshape input to (B,T,C) or (T,C) before this dropout","For 4-D conv activations, use nn.Dropout2d / standard Dropout instead","Move the dropout after a view/reshape that yields 2-D or 3-D"],"exampleFix":"# before\ny = dropout(x)  # x is (B, C, H, W) -> error\n# after\nB, C, H, W = x.shape\ny = dropout(x.permute(0,2,3,1).reshape(B, H*W, C)).reshape(B, H, W, C).permute(0,3,1,2)","handlingStrategy":"type-guard","validationCode":"if x.dim() not in (2, 3):\n    x = x.reshape(x.shape[0], -1, x.shape[-1])","typeGuard":"def dropout_supported(x: torch.Tensor) -> bool:\n    return x.dim() in (2, 3)","tryCatchPattern":null,"preventionTips":["Reshape to (B,T,C) before feature-level dropout","Use nn.Dropout2d for 4-D activations","Add asserts on rank in model forward"],"tags":["hanlp","pytorch","dropout","tensor-rank"],"backgroundTag":"unsupported-tensor-rank","analyzedSha":"ddb1299bddff079e447af52ec12549c50636bfa8","analyzedAt":"2026-08-27T03:36:54.287Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}