PyTorch torch.repeat_interleave 関数
torch.repeat_interleave指定した次元に沿って要素を繰り返すための PyTorch の関数です。
関数定義
torch.repeat_interleave(input, repeats, dim=None, *, output_size=None)
使用例
例
import torch
# dim を指定しない場合、すべての要素に対して繰り返す
x = torch.tensor([1, 2, 3])
result = torch.repeat_interleave(x, 2)
print("各要素を2回繰り返す:")
print(result)
# dim=0 で繰り返す
y = torch.tensor([[1, 2], [3, 4]])
print("n元のテンソル:")
print(y)
result = torch.repeat_interleave(y, 2, dim=0)
print("dim=0 に沿って2回繰り返す:")
print(result)
# 要素ごとに異なる繰り返し回数
result = torch.repeat_interleave(y, torch.tensor([1, 2]), dim=0)
print("ndim=0 に沿って異なる繰り返し回数 [1, 2]:")
print(result)
# dim=1 で繰り返す
result = torch.repeat_interleave(y, 3, dim=1)
print("ndim=1 に沿って3回繰り返す:")
print(result)
# output_size を返す
result = torch.repeat_interleave(x, 2, output_size=9)
print("noutput_size=9 を指定:")
print(result)
# dim を指定しない場合、すべての要素に対して繰り返す
x = torch.tensor([1, 2, 3])
result = torch.repeat_interleave(x, 2)
print("各要素を2回繰り返す:")
print(result)
# dim=0 で繰り返す
y = torch.tensor([[1, 2], [3, 4]])
print("n元のテンソル:")
print(y)
result = torch.repeat_interleave(y, 2, dim=0)
print("dim=0 に沿って2回繰り返す:")
print(result)
# 要素ごとに異なる繰り返し回数
result = torch.repeat_interleave(y, torch.tensor([1, 2]), dim=0)
print("ndim=0 に沿って異なる繰り返し回数 [1, 2]:")
print(result)
# dim=1 で繰り返す
result = torch.repeat_interleave(y, 3, dim=1)
print("ndim=1 に沿って3回繰り返す:")
print(result)
# output_size を返す
result = torch.repeat_interleave(x, 2, output_size=9)
print("noutput_size=9 を指定:")
print(result)
出力結果は次のとおりです:
每个元素重复 2 次:
tensor([1, 1, 2, 2, 3, 3])
原始张量:
tensor([[1, 2],
[3, 4]])
沿 dim=0 重复 2 次:
tensor([[1, 2],
[1, 2],
[3, 4],
[3, 4]])
沿 dim=0 不同重复次数 [1, 2]:
tensor([[1, 2],
[3, 4],
[3, 4]])
沿 dim=1 重复 3 次:
tensor([[1, 1, 1, 2, 2, 2],
[3, 3, 3, 4, 4, 4]])
指定 output_size=9:
tensor([1, 1, 2, 2, 3, 3, 1, 2, 3])
その他の拡張
Pytorch torch リファレンスマニュアル