PyTorch torch.dsplit 関数
torch.dsplitPyTorch において、深さ(第3次元)に沿ってテンソルを分割するための関数です。
関数定義
torch.dsplit(input, indices_or_sections)
使用例
例
import torch
# 3次元テンソルの深さ方向への分割
x = torch.arange(24).reshape(2, 3, 4)
print("元の3次元テンソル:")
print(x)
result = torch.dsplit(x, 2)
print("深さ方向に2つに分割:")
for i, t in enumerate(result):
print(f" ブロック {i}:n{t}")
# インデックスによる分割
result = torch.dsplit(x, [1, 3])
print("nインデックス [1, 3] で分割:")
for i, t in enumerate(result):
print(f" ブロック {i}:n{t}")
# 4次元テンソルの深さ方向への分割
y = torch.arange(32).reshape(2, 2, 4, 2)
print("n4次元テンソルの形状:", y.shape)
result = torch.dsplit(y, 2)
print("第3次元に沿って2つに分割:")
for i, t in enumerate(result):
print(f" ブロック {i} 形状: {t.shape}")
# 3次元テンソルの深さ方向への分割
x = torch.arange(24).reshape(2, 3, 4)
print("元の3次元テンソル:")
print(x)
result = torch.dsplit(x, 2)
print("深さ方向に2つに分割:")
for i, t in enumerate(result):
print(f" ブロック {i}:n{t}")
# インデックスによる分割
result = torch.dsplit(x, [1, 3])
print("nインデックス [1, 3] で分割:")
for i, t in enumerate(result):
print(f" ブロック {i}:n{t}")
# 4次元テンソルの深さ方向への分割
y = torch.arange(32).reshape(2, 2, 4, 2)
print("n4次元テンソルの形状:", y.shape)
result = torch.dsplit(y, 2)
print("第3次元に沿って2つに分割:")
for i, t in enumerate(result):
print(f" ブロック {i} 形状: {t.shape}")
出力結果:
原始三维张量:
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]]])
チャンク1:
tensor([[[ 2, 3],
[ 6, 7],
[10, 11]],
[[14, 15],
[18, 19],
[22, 23]]])
按索引 [1, 3] 分割:
块 0:
tensor([[[ 0],
[ 4],
[ 8]],
[[12],
[16],
[20]]])
チャンク1:
tensor([[[ 1, 2],
[ 5, 6],
[ 9, 10]],
[[13, 14],
[17, 18],
[21, 22]]])
チャンク2:
tensor([[[ 3],
[ 7],
[11]],
[[15],
[19],
[23]]])
四维张量形状: torch.Size([2, 2, 4, 2])
沿第三维分为 2 份:
块 0 形状: torch.Size([2, 2, 2, 2])
チャンク1 形状: torch.Size([2, 2, 2, 2])
その他の拡張機能
Pytorch torch リファレンスマニュアル