PyTorch torch.hstack 関数
torch.hstackPyTorch でテンソルを水平(列方向)にスタックするための関数です。
関数定義
torch.hstack(tensors, *, out=None)
使用例
実例
import torch
# 1次元テンソルの水平スタック
x1 = torch.tensor([1, 2, 3])
x2 = torch.tensor([4, 5, 6])
result = torch.hstack([x1, x2])
print("1次元テンソルのスタック:")
print(f" x1: {x1}")
print(f" x2: {x2}")
print(f" hstack: {result}")
# 2次元テンソルの水平スタック
y1 = torch.tensor([[1, 2], [3, 4]])
y2 = torch.tensor([[5, 6], [7, 8]])
result = torch.hstack([y1, y2])
print("n2次元テンソルの水平スタック:")
print(f" y1:n{y1}")
print(f" y2:n{y2}")
print(f" hstack:n{result}")
# 複数のテンソルのスタック
z1 = torch.tensor([1, 2])
z2 = torch.tensor([3, 4])
z3 = torch.tensor([5, 6])
result = torch.hstack([z1, z2, z3])
print("n複数の1次元テンソルのスタック:")
print(f" result: {result}")
# 1次元テンソルの水平スタック
x1 = torch.tensor([1, 2, 3])
x2 = torch.tensor([4, 5, 6])
result = torch.hstack([x1, x2])
print("1次元テンソルのスタック:")
print(f" x1: {x1}")
print(f" x2: {x2}")
print(f" hstack: {result}")
# 2次元テンソルの水平スタック
y1 = torch.tensor([[1, 2], [3, 4]])
y2 = torch.tensor([[5, 6], [7, 8]])
result = torch.hstack([y1, y2])
print("n2次元テンソルの水平スタック:")
print(f" y1:n{y1}")
print(f" y2:n{y2}")
print(f" hstack:n{result}")
# 複数のテンソルのスタック
z1 = torch.tensor([1, 2])
z2 = torch.tensor([3, 4])
z3 = torch.tensor([5, 6])
result = torch.hstack([z1, z2, z3])
print("n複数の1次元テンソルのスタック:")
print(f" result: {result}")
出力結果は:
一维张量堆叠:
x1: tensor([1, 2, 3])
x2: tensor([4, 5, 6])
hstack: tensor([1, 2, 3, 4, 5, 6])
二维张量水平堆叠:
y1:
tensor([[1, 2],
[3, 4]])
y2:
tensor([[5, 6],
[7, 8]])
hstack:
tensor([[1, 2, 5, 6],
[3, 4, 7, 8]])
多个一维张量堆叠:
result: tensor([1, 2, 3, 4, 5, 6])
その他の拡張
Pytorch torch リファレンスマニュアル