記事ディレクトリ
テンソルの基本操作
100以上のテンソル関連演算子のドキュメント
TensorとNumPyはndarray
似ています。
新着
この方法で新しいテンソルを作成できます:
- 初期化されていない新しい5x3マトリックスを作成します。
x = torch.empty(5, 3)
print(x)
出力:
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
- ランダムに初期化された行列を作成します。
x = torch.rand(5, 3)
print(x)
出力:
tensor([[0.4494, 0.6230, 0.8681],
[0.0780, 0.2643, 0.0934],
[0.1205, 0.0813, 0.9454],
[0.4212, 0.2899, 0.8791],
[0.1500, 0.6572, 0.4772]])
- 新しい0行列を作成し、データ型が長い
x = torch.zeros(5, 3, dtype = torch.long)
print(x)
出力:
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
- 新しい行列を作成して直接割り当てる
x = torch.tensor([5.5, 3])
print(x)
出力:
tensor([5.5000, 3.0000])
または、既存のテンソルから新しいテンソルを構築することもできます。新しい値(dtypeなど)を割り当てない場合は、以前の属性が使用されます。
x = x.new_ones(5, 3, dtype = torch.double)
print(x)
x = torch.randn_like(x, dtype = torch.float)
print(x)
出力:
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
tensor([[ 0.2552, 2.0007, 0.0682],
[-0.8530, -0.1174, -0.6569],
[-1.1001, 0.8416, -1.3575],
[-1.0513, 0.4601, 0.6628],
[ 2.0841, -0.4303, 0.3235]])
出力テンソルサイズ
print(x.size())
出力は、ここではtorch.Size
実際にはタプルなので、すべてのタプル操作をサポートします
torch.Size([5, 3])
追加
- x + y
- torch.add(x、y)
- torch.add(x、y、out = result)は、出力を保存する引数を設定できます
result = torch.empty(5, 3)
torch.add(x, y, out = result)
print(result)
出力:
tensor([[ 0.7021, 2.1474, 0.0886],
[-0.5905, 0.0338, 0.2445],
[-0.9172, 1.5455, -1.1381],
[-0.6434, 0.5016, 1.0220],
[ 2.9464, -0.1195, 1.0920]])
- y.add_(x)
ヒント:演算子の後に表示される場合があります
_
。現時点では、テンソルを操作すると、コピー操作は行われず、その位置で値が直接変更されます。これは、「in-situ操作」と同等です。 "(英語のインプレースバージョン、アウトオブプレースバージョンに対応)。add
両方のバージョンなどの一部の操作を使用できますが、これはすべての操作に当てはまるnarrow
わけではありません。インプレースバージョン.narrow_
がない場合、それは存在しません。同様に、fill_
アウトオブプレースバージョン.fill
がないため、存在しません。
# add x to y
y.add_(x)
print(y)
numpyのようなテンソルを操作することもできます。
print(x[:, 1])
見る
主な関数はresizeでありresize()
、numpy の関数と同等です。同時に、x.resize_(2, 3)
形状変更操作にも使用できます。
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8) # the size -1 is inferred from other dimensions
print(x.size(), y.size(), z.size())
パラメータでは、-1はテンソルを1次元テンソルに展開します(他の次元が0でない場合、それに応じて変化します)
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
項目
テンソルの一つだけの要素がある場合に使用することができる.item()
値を得ることができます。
x = torch.randn(1)
print(x)
print(x.item())
出力:
tensor([0.5746])
0.5746164917945862
NumPyブリッジ
トーチテンソル-> NumPy配列
a = torch.ones(5)
print(a)
出力:
tensor([1., 1., 1., 1., 1.])
b = a.numpy()
print(b)
出力:
[1. 1. 1. 1. 1.]
a.add_(1)
print(a)
print(b)
出力:
tensor([2., 2., 2., 2., 2.])
[2. 2. 2. 2. 2.]
NumPy配列->トーチテンソル
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out = a)
print(a)
print(b)
出力:
[2. 2. 2. 2. 2.]
tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
bの値は、aを変更すると変化することに注意してください。
CUDAテンソル
.to
メソッドを介してテンソルを任意のデバイスに移動できます。
# let us run this cell only if CUDA is available
# We will use ``torch.device`` objects to move tensors in and out of GPU
if torch.cuda.is_available():
device = torch.device("cuda") # a CUDA device object
y = torch.ones_like(x, device=device) # directly create a tensor on GPU
x = x.to(device) # or just use strings ``.to("cuda")``
z = x + y
print(z)
print(z.to("cpu", torch.double)) # ``.to`` can also change dtype together!
まとめこの