pytorch学習ノート(1)-Tensorの作成から


文書ディレクトリ
  • Tensor
  • の作成
  • 1.基本作成
  • 2.特殊値
  • を作成
  • 3.ランダム値と特定のシーケンスTensor
  • の作成
  • 4.Numpy回転Tensor
  • 5.Tensor回転Numpy



  • Tensorの作成
    pytorch学習の最も基礎的な学習はテンソルの作成から始まる.
    1.基本作成
    import torch
    #     
    a = torch.Tensor([[1, 2],[3, 4]])
    print(a)
    
    #      ,    
    b = torch.Tensor(2, 2)
    print(b)
    
    d = torch.tensor(((1, 2), (3, 4)))
    print(d.type())
    print(d.type_as(a))
    

    tensor([[1., 2.], [3., 4.]]) torch.FloatTensor tensor([[1., 2.], [3., 4.]]) torch.LongTensor tensor([[1., 2.], [3., 4.]])
    2.特別な値の作成
    #   Tensor
    d = torch.empty(2,3)
    print(d.type())
    print(d.type_as(a))
    
    #  0 Tensor
    d = torch.zeros(2,3)
    print(d.type())
    print(d.type_as(a))
    
    #    0 Tensor
    d = torch.zeros_like(d)
    print(d.type())
    print(d.type_as(a))
    
    #    1
    d = torch.eye(2, 2)
    print(d.type())
    print(d.type_as(a))
    
    # 1
    d = torch.ones(2, 2)
    print(d.type())
    print(d.type_as(a))
    
    d = torch.ones_like(d)
    print(d.type())
    print(d.type_as(a))
    

    torch.FloatTensor tensor([[0., 0., 0.], [0., 0., 0.]])
    torch.FloatTensor tensor([[0., 0., 0.], [0., 0., 0.]])
    torch.FloatTensor tensor([[0., 0., 0.], [0., 0., 0.]])
    torch.FloatTensor tensor([[1., 0.], [0., 1.]])
    torch.FloatTensor tensor([[1., 1.], [1., 1.]])
    torch.FloatTensor tensor([[1., 1.], [1., 1.]])
    3.ランダム値と特定シーケンスTensorの作成
    #0-1   
    d = torch.rand(2, 3)
    print(d.type())
    print(d.type_as(a))
    
    d = torch.arange(2, 10, 2)
    print(d.type())
    print(d.type_as(a))
    
    d = torch.linspace(10, 2, 3)
    print(d.type())
    print(d.type_as(a))
    
    dd = torch.normal(mean=0, std=1, size=(2, 3), out=b)
    print(b)
    print(dd)
    
    d = torch.normal(mean=torch.rand(5), std=torch.rand(5))
    print(d.type())
    print(d.type_as(a))
    
    
    d = torch.Tensor(2, 2).uniform_(-1, 1)
    print(d.type())
    print(d.type_as(a))
    
    
    d = torch.randperm(10)
    print(d.type())
    print(d.type_as(a))
    
    

    torch.FloatTensor tensor([[0.6432, 0.4434, 0.3289], [0.6581, 0.7615, 0.6703]])
    torch.LongTensor tensor([2., 4., 6., 8.])
    torch.FloatTensor tensor([10., 6., 2.])
    tensor([[-0.4051, -0.5710, -1.3798], [ 0.3047, -0.3695, -0.2271]])
    tensor([[-0.4051, -0.5710, -1.3798], [ 0.3047, -0.3695, -0.2271]])
    torch.FloatTensor tensor([0.4624, 1.2237, 1.1937, 1.3881, 0.5219])
    torch.FloatTensor tensor([[ 0.9237, 0.2990], [-0.5562, -0.2350]])
    torch.LongTensor tensor([2., 7., 4., 3., 5., 6., 9., 1., 0., 8.])
    4.Numpy回転Tensor
    a = torch.ones(5)
    b = a.numpy()    
    a.add_(1)
    print(a)
    print(b)
    

    tensor([2., 2., 2., 2., 2.]) [2. 2. 2. 2. 2.]
    5.Tensor回転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)