## PyTorch torch.dsplit
`torch.dsplit` is a PyTorch function used to split a tensor into multiple sub-tensors depth-wise (along the third dimension, which corresponds to `dim=2`).
It is equivalent to calling `torch.tensor_split` with `dim=2`, but it provides a more intuitive and convenient API when working with multi-dimensional data (such as 3D or 4D tensors) where depth-wise partitioning is required.
---
## Syntax
```python
torch.dsplit(input, indices_or_sections)
```
### Parameters
| Parameter | Type | Description |
| :--- | :--- | :--- |
| `input` | `Tensor` | The tensor to be split. Must be at least 3-dimensional. |
| `indices_or_sections` | `int` or `list` / `tuple` of `ints` | **If an integer $N$:** Splits the tensor into $N$ equal parts along the third dimension. The size of the third dimension must be divisible by $N$.
**If a list/tuple of indices:** Splits the tensor at the specified indices along the third dimension. |
### Return Value
* Returns a list of sub-tensors split from the original tensor.
---
## Code Examples
### Example 1: Splitting a 3D Tensor into Equal Parts
In this example, we create a 3D tensor of shape `(2, 3, 4)` and split it into 2 equal parts along the depth dimension (`dim=2`).
```python
import torch
# Create a 3D tensor of shape (2, 3, 4)
x = torch.arange(24).reshape(2, 3, 4)
print("Original 3D Tensor:")
print(x)
# Split depth-wise into 2 equal parts
result = torch.dsplit(x, 2)
print("\nSplit into 2 parts along depth:")
for i, t in enumerate(result):
print(f" Chunk {i}:\n{t}")
```
**Output:**
```text
Original 3D Tensor:
tensor([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
Split into 2 parts along depth:
Chunk 0:
tensor([[[ 0, 1],
[ 4, 5],
[ 8, 9]],
[[12, 13],
[16, 17],
[20, 21]]])
Chunk 1:
tensor([[[ 2, 3],
[ 6, 7],
[10, 11]],
[[14, 15],
[18, 19],
[22, 23]]])
```
---
### Example 2: Splitting at Specific Indices
You can also pass a list of indices to split the tensor at specific positions. For example, passing `[1, 3]` splits the tensor along the third dimension at indices `1` and `3`, resulting in three slices: `[:1]`, `[1:3]`, and `[3:]`.
```python
import torch
x = torch.arange(24).reshape(2, 3, 4)
# Split at indices [1, 3]
result = torch.dsplit(x, [1, 3])
print("Split at indices [1, 3]:")
for i, t in enumerate(result):
print(f" Chunk {i}:\n{t}")
```
**Output:**
```text
Split at indices [1, 3]:
Chunk 0:
tensor([[,
,
],
[,
,
]])
Chunk 1:
tensor([[[ 1, 2],
[ 5, 6],
[ 9, 10]],
[[13, 14],
[17, 18],
[21, 22]]])
Chunk 2:
tensor([[,
,
],
[,
,
]])
```
---
### Example 3: Splitting a 4D Tensor
`torch.dsplit` works on any tensor with 3 or more dimensions. Here, we apply it to a 4D tensor of shape `(2, 2, 4, 2)`. The split still occurs along the third dimension (index `2`).
```python
import torch
# Create a 4D tensor of shape (2, 2, 4, 2)
y = torch.arange(32).reshape(2, 2, 4, 2)
print("Original 4D Tensor shape:", y.shape)
# Split into 2 parts along the third dimension
result = torch.dsplit(y, 2)
print("Split into 2 parts along the third dimension:")
for i, t in enumerate(result):
print(f" Chunk {i} shape: {t.shape}")
```
**Output:**
```text
Original 4D Tensor shape: torch.Size([2, 2, 4, 2])
Split into 2 parts along the third dimension:
Chunk 0 shape: torch.Size([2, 2, 2, 2])
Chunk 1 shape: torch.Size([2, 2, 2, 2])
```
---
## Considerations
1. **Dimensionality Requirement**: The input tensor must be at least 3-dimensional. Passing a 1D or 2D tensor to `torch.dsplit` will raise a `RuntimeError`. For 1D and 2D tensors, use `torch.hsplit` or `torch.vsplit` instead.
2. **Divisibility**: If `indices_or_sections` is passed as an integer $N$, the size of the third dimension of the input tensor must be exactly divisible by $N$. If it is not divisible, PyTorch will throw a `RuntimeError`. If you need to split a tensor into unequal parts, pass a list of indices instead.
3. **Memory View**: Similar to other splitting functions in PyTorch, `torch.dsplit` returns views of the original tensor whenever possible, meaning that modifying a split chunk will also modify the original tensor.
π Categories
- β‘ JavaScript (1589)
- π PHP (872)
- π Python3 (810)
- π HTML (691)
- βοΈ C# (650)
- π Python (594)
- β Java (552)
- βοΈ PyTorch (534)
- π§ Linux (472)
- βοΈ C (432)
- π¦ jQuery (406)
- π¨ CSS (377)
- π XML (259)
- π¦ jQuery UI (231)
- π― Bootstrap (220)
- βοΈ C++ (215)
- π °οΈ Angular (205)
- π HTML DOM (201)
- π΄ Redis (188)
- π Web Building (142)
- π Vue.js (141)
- π R (131)
- πΌ Pandas (124)
- ποΈ SQL (105)
- βοΈ Docker (86)
- βοΈ TypeScript (73)
- βοΈ Highcharts (70)
- π AI Agent (70)
- βοΈ React (68)
- π Node.js (65)
- βοΈ Machine Learning (60)
- π Git (59)
- π΅ Go (58)
- π Markdown (58)
- π’ NumPy (55)
- π§ͺ Flask (54)
- βοΈ Scala (53)
- ποΈ SQLite (52)
- π JSTL (52)
- βοΈ VS Code (51)
- π MongoDB (49)
- π Perl (48)
- π Ruby (47)
- π Matplotlib (47)
- βοΈ Uncategorized (46)
- π Swift (46)
- ποΈ PostgreSQL (46)
- βοΈ Data Structures (46)
- π Playwright (46)
- π iOS (45)
- ποΈ MySQL (44)
- βοΈ LangChain (43)
- π FastAPI (40)
- βοΈ Ionic (38)
- π Design Patterns (37)
- βοΈ Eclipse (37)
- π¨ CSS3 (34)
- π Lua (34)
- βοΈ Codex (34)
- πΈ Django (32)
- βοΈ OpenCV (32)
- π Rust (31)
- π JSP (31)
- βοΈ Claude Code (31)
- π Pillow (30)
- βοΈ OpenCode (28)
- π AI Skills (27)
- π Flutter (26)
- π Maven (26)
- π¨ Tailwind CSS (25)
- π§ TensorFlow (25)
- π Servlet (24)
- π Dart (23)
- π Assembly (23)
- βοΈ Memcached (22)
- βοΈ SVG (22)
- βοΈ Electron (22)
- π NLP (22)
- π Regex (21)
- π Android (20)
- π£ Kotlin (19)
- π Julia (19)
- π SOAP (17)
- π Selenium (17)
- π PowerShell (17)
- π Sass (16)
- π HTTP (16)
- π Zig (15)
- π AI (15)
- π AJAX (14)
- π Swagger (14)
- βοΈ Scikit-learn (13)
- βοΈ ECharts (13)
- βοΈ Chart.js (13)
- βοΈ Cursor (13)
- βοΈ SciPy (12)
- π RDF (12)
- π Ollama (12)
- π Next.js (12)
- π Plotly Dash (12)
- π JSON (11)
- π RESTful API (11)
- π WSDL (9)
- βοΈ CMake (8)
- π Firebug (7)
- π Nginx (6)
- βΈοΈ Kubernetes (6)
- π Jupyter (6)
- π LaTeX (4)
- π UniApp (4)
- ποΈ SQL Server (1)
YouTip