{"record":{"id":"e39bb563189447d8","repo":"microsoft/qlib","slug":"unsupported-data-shape","errorCode":null,"errorMessage":"Unsupported data shape.","messagePattern":"Unsupported data shape\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"qlib/contrib/model/pytorch_general_nn.py","lineNumber":199,"sourceCode":"        data : torch.Tensor\n            input data which maybe 3 dimension or 2 dimension\n            - 3dim: [batch_size, time_step, feature_dim]\n            - 2dim: [batch_size, feature_dim]\n\n        Returns\n        -------\n        Tuple[torch.Tensor, torch.Tensor]\n        \"\"\"\n        if data.dim() == 3:\n            # it is a time series dataset\n            feature = data[:, :, 0:-1].to(self.device)\n            label = data[:, -1, -1].to(self.device)\n        elif data.dim() == 2:\n            # it is a tabular dataset\n            feature = data[:, 0:-1].to(self.device)\n            label = data[:, -1].to(self.device)\n        else:\n            raise ValueError(\"Unsupported data shape.\")\n        return feature, label\n\n    def train_epoch(self, data_loader):\n        self.dnn_model.train()\n\n        for data, weight in data_loader:\n            feature, label = self._get_fl(data)\n\n            pred = self.dnn_model(feature.float())\n            loss = self.loss_fn(pred, label, weight.to(self.device))\n\n            self.train_optimizer.zero_grad()\n            loss.backward()\n            torch.nn.utils.clip_grad_value_(self.dnn_model.parameters(), 3.0)\n            self.train_optimizer.step()\n\n    def test_epoch(self, data_loader):\n        self.dnn_model.eval()","sourceCodeStart":181,"sourceCodeEnd":217,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/contrib/model/pytorch_general_nn.py#L181-L217","documentation":"Raised by DNNModelPytorch._get_fl when a batch tensor passed to training/prediction is neither 2-D (tabular: [batch, feature_dim]) nor 3-D (time series: [batch, step, feature_dim]). The method slices features and the label off the last axis, which is only meaningful for those two shapes. Any other dimensionality (most commonly 1-D or 4-D) is rejected immediately.","triggerScenarios":"Feeding the model with a dataset whose prepared arrays collapse to 1-D (e.g. only one column so a squeeze happened, or a misconfigured TSDatasetH step size), or reusing the class's train_epoch with a custom data loader yielding 4-D tensors. Also triggered if num_features is set such that the ConcatDataset yields per-sample 1-D vectors while the model expects tabular batches.","commonSituations":"Switching a workflow from DatasetH (tabular) to TSDatasetH (time series) or vice versa without matching the model input; a custom data handler that returns squeezed arrays; edge case where step=1 in TS processing produces an unexpected shape.","solutions":["Inspect data.shape right before _get_fl: for tabular it must be (batch, n_features+1); for time series (batch, step, n_features+1) with the label in the last column of the last step.","Use TSDatasetH with the matching handler when you want 3-D input, and plain DatasetH for 2-D input; do not mix.","If you have a legitimate 4-D input (e.g. multi-horizon), subclass and override _get_fl to flatten or slice appropriately."],"exampleFix":"# before: tabular dataset but handler returns 1-D rows\nfor data, weight in data_loader:\n    feature, label = self._get_fl(data)  # data.dim()==1 -> ValueError\n\n# after: ensure batched 2-D tensor\nassert data.dim() in (2, 3)\nfeature, label = self._get_fl(data)","handlingStrategy":"validation","validationCode":"sample = next(iter(train_loader))[0]\nassert sample.dim() in (2, 3), f\"expected 2-D tabular or 3-D time-series batch, got shape {tuple(sample.shape)}\"","typeGuard":"def has_supported_batch_shape(t) -> bool:\n    return t.dim() in (2, 3)","tryCatchPattern":"try:\n    feature, label = model._get_fl(data)\nexcept ValueError:\n    raise ValueError(f\"batch shape {tuple(data.shape)} unsupported; use DatasetH (2-D) or TSDatasetH (3-D)\")","preventionTips":["Log one batch's .shape at the start of every experiment.","Match the dataset handler type (DatasetH vs TSDatasetH) to the model variant you configured."],"tags":["pytorch","qlib","data-shape","tensor"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}