k0b0's record.

Computer Engineering, Arts and Books

PytorchのTensorを試してみる

PytorchのTensorを試してみる

torch.empty()

>>> x = torch.empty(5,5)
>>> x
tensor([[ 0.0000e+00,  0.0000e+00,  1.2111e-37,  1.4013e-45, -2.1667e+20],
        [ 4.5779e-41, -2.1667e+20,  4.5779e-41, -2.1668e+20,  4.5779e-41],
        [ 5.9737e-07,  6.4104e-10,  1.3567e-19,  6.4094e-10,  1.4585e-19],
        [ 6.4899e-07,  2.5038e-12,  4.1638e-11,  6.4097e-10,  9.3319e-40],
        [ 1.4013e-45,  0.0000e+00,  0.0000e+00,  0.0000e+00,  0.0000e+00]])

torch.rand()

>>> x = torch.rand(5,5)
>>> x
tensor([[0.9336, 0.5033, 0.3661, 0.8221, 0.9486],
        [0.5814, 0.9131, 0.1426, 0.1064, 0.5611],
        [0.9195, 0.7638, 0.8882, 0.8382, 0.1704],
        [0.5920, 0.3543, 0.7149, 0.0117, 0.4379],
        [0.7179, 0.5070, 0.2185, 0.8193, 0.3506]])

torch.zeros()

>>> x = torch.zeros(5,5)
>>> x
tensor([[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]])

torch.tensor([])

>>> x = torch.tensor([1.1, 2.2, 3.3, 4.4, 5.5])
>>> x
tensor([1.1000, 2.2000, 3.3000, 4.4000, 5.5000])

new_ones()

>>> x = x.new_ones(5,5)
>>> x
tensor([[1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.]])

dtype(データタイプの指定)

>>> x = torch.rand(5, 5 ,dtype=torch.double)
>>> x
tensor([[0.9080, 0.8823, 0.4970, 0.5745, 0.2689],
        [0.7482, 0.8182, 0.7628, 0.5347, 0.9337],
        [0.0443, 0.5242, 0.8650, 0.5006, 0.6526],
        [0.1058, 0.1484, 0.8282, 0.5445, 0.9101],
        [0.9393, 0.4800, 0.7741, 0.8051, 0.0800]], dtype=torch.float64)

shape, size() (tensorのサイズを取得)

>>> x = torch.rand(5,3)
>>> x.shape
torch.Size([5, 3])
>>> x.size()
torch.Size([5, 3])
>>> x.size(0)
5
>>> x.size(1)
3

演算

>>> x = torch.rand(3,3)
>>> y = torch.rand(3,3)
>>> x
tensor([[0.4449, 0.2112, 0.6244],
        [0.5940, 0.6442, 0.0575],
        [0.4617, 0.6969, 0.9881]])
>>> y
tensor([[0.2623, 0.3369, 0.9841],
        [0.8398, 0.8506, 0.4659],
        [0.5126, 0.6361, 0.9784]])
>>> torch.add(x,y)
tensor([[0.7072, 0.5481, 1.6085],
        [1.4338, 1.4948, 0.5233],
        [0.9743, 1.3329, 1.9665]])
>>> x + y
tensor([[0.7072, 0.5481, 1.6085],
        [1.4338, 1.4948, 0.5233],
        [0.9743, 1.3329, 1.9665]])

演算(出力先を指定)

>>> x = torch.rand(3,3)
>>> y = torch.rand(3,3)
>>> result = torch.empty(3,3)
>>> torch.add(x, y, out=result)
tensor([[0.4299, 1.6843, 1.1674],
        [1.6310, 1.2367, 1.2494],
        [0.8625, 0.9575, 1.4197]])

スライシング

>>> x = torch.rand(5,5)
>>> x
tensor([[0.8801, 0.0515, 0.8298, 0.0895, 0.2612],
        [0.2635, 0.1787, 0.7860, 0.2423, 0.9221],
        [0.7556, 0.4374, 0.7314, 0.6534, 0.4783],
        [0.9463, 0.7781, 0.0977, 0.1606, 0.2483],
        [0.1573, 0.9691, 0.6578, 0.9738, 0.9746]])
>>> x[:, 1]
tensor([0.0515, 0.1787, 0.4374, 0.7781, 0.9691])

変換(行列 -> ベクトル、ベクトル -> 行列)

>>> x = torch.rand(4,4)
>>> x
tensor([[0.0995, 0.0718, 0.3917, 0.5440],
        [0.9479, 0.1995, 0.3759, 0.1117],
        [0.1568, 0.4121, 0.2951, 0.3687],
        [0.0139, 0.3464, 0.4001, 0.7129]])
>>> y = x.view(16) # 要素数16のベクトルに変換
>>> y
tensor([0.0995, 0.0718, 0.3917, 0.5440, 0.9479, 0.1995, 0.3759, 0.1117, 0.1568,
        0.4121, 0.2951, 0.3687, 0.0139, 0.3464, 0.4001, 0.7129])
>>> z = x.view(-1, 8) # 要素数8のベクトル(2つ)に変換
>>> z
tensor([[0.0995, 0.0718, 0.3917, 0.5440, 0.9479, 0.1995, 0.3759, 0.1117],
        [0.1568, 0.4121, 0.2951, 0.3687, 0.0139, 0.3464, 0.4001, 0.7129]])

tensorから値を取り出す

ただし、item()は要素数1のtensorしか実行できない。

>>> x = torch.rand(1)
>>> x
tensor([0.0833])
>>> x.item()
0.08332055807113647

torch tensorからnumpy arrayに変換

>>> a = torch.ones(5)
>>> a
tensor([1., 1., 1., 1., 1.])
>>> b = a.numpy() # numpy arrayに変換(参照渡し)
>>> b
array([1., 1., 1., 1., 1.], dtype=float32)
>>> a.add_(1)
tensor([2., 2., 2., 2., 2.])
>>> b # 参照渡しなので値が更新される
array([2., 2., 2., 2., 2.], dtype=float32)

numpy arrayからtorhc tensorに変換

>>> a = np.ones(5)
>>> a
array([1., 1., 1., 1., 1.])
>>> b = torch.from_numpy(a)
>>> b
tensor([1., 1., 1., 1., 1.], dtype=torch.float64)
>>> np.add(a, 1, out=a)
array([2., 2., 2., 2., 2.])
>>> b
tensor([2., 2., 2., 2., 2.], dtype=torch.float64)