# RuntimeError: 1D target tensor expected, multi-target not supported

- **ID:** `pytorch/invalid-argument-2d-index-target`
- **Domain:** pytorch
- **Category:** shape_error
- **Verification:** ai_generated
- **Fix Rate:** 90%

## Root Cause

A loss function like CrossEntropyLoss received a target tensor with more than one dimension (e.g., one-hot encoded labels) instead of a 1D tensor of class indices.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| pytorch>=1.13.0 | active | — | — |

## Workarounds

1. **Convert one-hot encoded labels to class indices using `torch.argmax(target, dim=1)` before passing to the loss function. Example: `loss = criterion(output, target.argmax(dim=1))`** (95% success)
   ```
   Convert one-hot encoded labels to class indices using `torch.argmax(target, dim=1)` before passing to the loss function. Example: `loss = criterion(output, target.argmax(dim=1))`
   ```
2. **If the target is already 1D but has an extra dimension from batching, squeeze it: `target = target.squeeze()`.** (90% success)
   ```
   If the target is already 1D but has an extra dimension from batching, squeeze it: `target = target.squeeze()`.
   ```
3. **Use `torch.nn.BCEWithLogitsLoss` with proper target shape (same as output) if the task is multi-label classification.** (85% success)
   ```
   Use `torch.nn.BCEWithLogitsLoss` with proper target shape (same as output) if the task is multi-label classification.
   ```

## Dead Ends

- **** — This still produces a 2D tensor; the loss expects 1D indices. (95% fail)
- **** — The error is about dimensionality, not dtype; CrossEntropyLoss expects Long type indices. (90% fail)
- **** — BCEWithLogitsLoss expects a different shape and value range; it will not fix the dimensionality issue. (80% fail)
