PyTorch torch.dsplit Function


Pytorch torch 参考手册Pytorch torch Reference Manual

torch.dsplitIt is a function in PyTorch used to split a tensor along the depth (third dimension).

Function Definition

torch.dsplit(input, indices_or_sections)

Usage Example

Example

import torch

# Split a 3D tensor along the depth dimension
x = torch.arange(24).reshape(2, 3, 4)
print("Original 3D tensor:")
print(x)

result = torch.dsplit(x, 2)
print("Split into 2 parts along depth:")
for i, t in enumerate(result):
    print(f" Block {i}:n{t}")

# Split by indices
result = torch.dsplit(x, [1, 3])
print("nSplit by indices [1, 3]:")
for i, t in enumerate(result):
    print(f" Block {i}:n{t}")

# Split a 4D tensor along the depth dimension
y = torch.arange(32).reshape(2, 2, 4, 2)
print("n4D tensor shape:", y.shape)

result = torch.dsplit(y, 2)
print("Split into 2 parts along the third dimension:")
for i, t in enumerate(result):
    print(f" Block {i} shape: {t.shape}")

The output result is:

原始三维张量:
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]]])
沿深度分为 2 份:
  块 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]]])

按索引 [1, 3] 分割:
  块 0:
tensor([[[ 0],
         [ 4],
         [ 8]],

        [[12],
         [16],
         [20]]])
  Chunk 1:
tensor([[[ 1,  2],
         [ 5,  6],
         [ 9, 10]],

        [[13, 14],
         [17, 18],
         [21, 22]]])
  Chunk 2:
tensor([[[ 3],
         [ 7],
         [11]],

        [[15],
         [19],
         [23]]])

四维张量形状: torch.Size([2, 2, 4, 2])
沿第三维分为 2 份:
  块 0 形状: torch.Size([2, 2, 2, 2])
  Chunk 1 形状: torch.Size([2, 2, 2, 2])

Pytorch torch 参考手册Pytorch torch Reference Manual

Other Extensions