PyTorch Tensor Basic Usage

  • Create Tensor
  • Indexing, Joining, Slicing, Squeezing
  • Initialization
  • Math Operations

Import Required Libraries

In [1]:
!pip3 install torch torchvision
import torch
Requirement already satisfied: torch in /usr/local/lib/python3.6/dist-packages (1.1.0)
Requirement already satisfied: torchvision in /usr/local/lib/python3.6/dist-packages (0.3.0)
Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from torch) (1.16.4)
Requirement already satisfied: pillow>=4.1.1 in /usr/local/lib/python3.6/dist-packages (from torchvision) (4.3.0)
Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages (from torchvision) (1.12.0)
Requirement already satisfied: olefile in /usr/local/lib/python3.6/dist-packages (from pillow>=4.1.1->torchvision) (0.46)

1. Create Tensor

1) Random Numbers

  • torch.rand()
  • torch.randn()
  • torch.randint()
In [2]:
# https://pytorch.org/docs/stable/torch.html?highlight=rand#torch.rand
# torch.rand(sizes) -> [0,1)
# 0¿¡¼­ 1»çÀÌÀÇ ·£´ýÇÑ ¼ýÀÚ
x = torch.rand(2,3)
x
Out[2]:
tensor([[0.9312, 0.4167, 0.9262],
        [0.2511, 0.3974, 0.0930]])
In [3]:
# https://pytorch.org/docs/stable/torch.html?highlight=randn#torch.randn
# Random Normal
# Á¤±ÔºÐÆ÷¿¡¼­ »ùÇøµÇÑ °ª
x = torch.randn(2,3)
x
Out[3]:
tensor([[ 2.1308, -0.3603, -0.4545],
        [-0.0652,  0.6919,  0.4636]])
In [4]:
# https://pytorch.org/docs/stable/torch.html?highlight=rand#torch.randint
# Random Integer
# ½ÃÀÛ°ú ³¡ »çÀÌÀÇ ·£´ýÇÑ ÀÚ¿¬¼ö
x = torch.randint(2,5,size=(2,3))
x
Out[4]:
tensor([[3, 2, 3],
        [3, 3, 3]])

2) zeros & ones

  • torch.ones()
  • torch.ones_like()
  • torch.zeros()
  • torch.zeros_like()
In [5]:
# https://pytorch.org/docs/stable/torch.html?highlight=zeros#torch.zeros
# torch.zeros(2,3) -> [[0,0,0],[0,0,0]]
# 0À¸·Î ä¿öÁø ÅÙ¼­
x = torch.zeros(2,3)
x
Out[5]:
tensor([[0., 0., 0.],
        [0., 0., 0.]])
In [6]:
# https://pytorch.org/docs/stable/torch.html?highlight=zeros#torch.zeros_like
# same shape tensor filled with zeros
# ÀÎÀÚ·Î µé¾î¿À´Â ÅÙ¼­¿Í ÇüÅ°¡ °°Àº 0À¸·Î ä¿öÁø ÅÙ¼­
ref = torch.rand(4,5)
x = torch.zeros_like(ref)
x
Out[6]:
tensor([[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]])
In [7]:
# https://pytorch.org/docs/stable/torch.html?highlight=ones#torch.ones
# torch.ones(2,3) -> [[1,1,1],[1,1,]]
# 1·Î ä¿öÁø ÅÙ¼­
x = torch.ones(2,3)
x
Out[7]:
tensor([[1., 1., 1.],
        [1., 1., 1.]])
In [8]:
# https://pytorch.org/docs/stable/torch.html?highlight=ones_like#torch.ones_like
# same shape tensor filled with ones
# ÀÎÀÚ·Î µé¾î¿À´Â ÅÙ¼­¿Í ÇüÅ°¡ °°Àº 1·Î ä¿öÁø ÅÙ¼­
ref = torch.rand(4,5)
x = torch.ones_like(ref)
x
Out[8]:
tensor([[1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.]])

3) Tensor Data Type

  • tensor.type()
  • tensor.type_as()
In [9]:
# https://pytorch.org/docs/stable/tensors.html?highlight=type#torch.Tensor.type
# without dtype, returns type of tensor
# tensor.type()Àº ÇØ´ç ÅÙ¼­ÀÇ Å¸ÀÔÀ» ¸®ÅÏÇÏ°í type(tensor)´Â ÅäÄ¡ÀÇ ÅÙ¼­ Ŭ·¡½º¶ó´Â °ÍÀ» ¸®ÅÏÇÔ

x = torch.rand(2,3)
print(x.type())
print(type(x))
torch.FloatTensor
<class 'torch.Tensor'>
In [10]:
# with dtype, cast the object to the dtype
# tensor.type()À» dtype°ú ÇÔ²² »ç¿ëÇϸé ÅÙ¼­ÀÇ µ¥ÀÌÅÍ Å¸ÀÔÀ» dtype¿¡ ³Ö¾îÁØ µ¥ÀÌÅÍ Å¸ÀÔÀ¸·Î ¹Ù²ãÁÝ´Ï´Ù.
double_x = x.type(dtype=torch.DoubleTensor)
print(double_x.type())
torch.DoubleTensor
In [11]:
# https://pytorch.org/docs/stable/tensors.html?highlight=type#torch.Tensor.type_as
# À§ÀÇ type ÇÔ¼ö¿Í À¯»çÇÏ°Ô type_as¶ó´Â ÇÔ¼ö¸¦ »ç¿ëÇØ µ¥ÀÌÅÍŸÀÔÀ» ¹Ù²Ü ¼ö ÀÖ½À´Ï´Ù.
int_x = x.type_as(torch.IntTensor())
print(int_x.type())
torch.IntTensor

4) Numpy to Tensor, Tensor to Numpy

  • torch.from_numpy()
  • tensor.numpy()
In [12]:
# https://pytorch.org/docs/stable/torch.html?highlight=from_numpy#torch.from_numpy
# torch.from_numpy(ndarray) -> tensor
# from_numpy ÇÔ¼ö¸¦ ÀÌ¿ëÇØ ³ÑÆÄÀÌ ¹è¿­À» ÅäÄ¡ÅÙ¼­·Î ¹Ù²Ü ¼ö ÀÖ½À´Ï´Ù.

import numpy as np

x1 = np.ndarray(shape=(2,3), dtype=int,buffer=np.array([1,2,3,4,5,6]))
x2 = torch.from_numpy(x1)

x2,x2.type()
Out[12]:
(tensor([[1, 2, 3],
         [4, 5, 6]]), 'torch.LongTensor')
In [13]:
# https://pytorch.org/docs/stable/tensors.html?highlight=numpy#torch.Tensor.numpy
# tensor.numpy() -> ndarray
# ¹Ý´ë·Î ÅäÄ¡ ÅÙ¼­¸¦ .numpy() ¸¦ ÅëÇØ ³ÑÆÄÀÌ ¹è¿­·Î ¹Ù²Ü ¼ö ÀÖ½À´Ï´Ù.

x3 = x2.numpy()
x3
Out[13]:
array([[1, 2, 3],
       [4, 5, 6]])

5) Tensor on CPU & GPU

In [14]:
# ¹è¿­À» ÀÎÀÚ·Î ³Ö¾î ÅäÄ¡ ÅÙ¼­¸¦ »ý¼ºÇÒ ¼ö ÀÖ½À´Ï´Ù.

x = torch.FloatTensor([[1,2,3],[4,5,6]])
x
Out[14]:
tensor([[1., 2., 3.],
        [4., 5., 6.]])
In [15]:
# https://pytorch.org/docs/stable/cuda.html?highlight=device#torch.cuda.device
# device ÇÔ¼ö¸¦ »ç¿ëÇØ ÅÙ¼­¸¦ ¿øÇÏ´Â ÀåÄ¡·Î À̵¿½Ãų ¼ö ÀÖ½À´Ï´Ù.
cpu = torch.device('cpu')
gpu = torch.device('cuda')

# https://pytorch.org/docs/stable/cuda.html?highlight=available#torch.cuda.is_available
# gpu°¡ »ç¿ë °¡´ÉÇÑÁö üũÇØÁÝ´Ï´Ù.
if torch.cuda.is_available():
  # https://pytorch.org/docs/stable/tensors.html?highlight=#torch.Tensor.to
  # .to ÇÔ¼ö¸¦ ÀÌ¿ëÇØ ÁöÁ¤ÇÑ ÀåÄ¡·Î À̵¿½ÃÄÑÁÝ´Ï´Ù.
  x_gpu = x.to(gpu)
  print(x_gpu)
  
x_cpu = x_gpu.to(cpu)
print(x_cpu)
tensor([[1., 2., 3.],
        [4., 5., 6.]], device='cuda:0')
tensor([[1., 2., 3.],
        [4., 5., 6.]])

6) Tensor Size

In [16]:
# https://pytorch.org/docs/stable/tensors.html?highlight=size#torch.Tensor.size
# size ÇÔ¼ö¸¦ ÀÌ¿ëÇØ ÅÙ¼­ÀÇ ÇüŸ¦ ¾Ë ¼ö ÀÖ½À´Ï´Ù.
x = torch.FloatTensor(10,12,3,3)
x.size(), x.size()[1:2]
Out[16]:
(torch.Size([10, 12, 3, 3]), torch.Size([12]))

2. Indexing, Joining, Slicing & Squeezing

1) Indexing

  • torch.index_select()
  • torch.masked_select()
In [17]:
# ÀÓÀÇÀÇ ÅÙ¼­¸¦ »ý¼ºÇÕ´Ï´Ù.
x = torch.randn(4,3)
print(x)
tensor([[-1.1821,  0.9164, -0.2556],
        [-0.5557,  1.4337,  1.5086],
        [ 0.7669, -0.1645,  0.2401],
        [-0.7632,  1.3450,  0.0454]])
In [18]:
# middle 2 rows
# ÅäÄ¡ ÅÙ¼­ ¿ª½Ã ±âÁ¸ ÆÄÀ̽ã À妽ÌÀ» ¶È°°ÀÌ ¾µ ¼ö ÀÖ½À´Ï´Ù.
x[1:3,:]
Out[18]:
tensor([[-0.5557,  1.4337,  1.5086],
        [ 0.7669, -0.1645,  0.2401]])
In [19]:
# https://pytorch.org/docs/stable/torch.html?highlight=index_select#torch.index_select
# index select along dimension 1
# index_select ÇÔ¼ö¸¦ »ç¿ëÇØ ÁöÁ¤ÇÑ Â÷¿ø ±âÁØÀ¸·Î ¿øÇÏ´Â °ªµéÀ» »Ì¾Æ³¾ ¼ö ÀÖ½À´Ï´Ù.

selected = torch.index_select(x,dim=1,index=torch.LongTensor([0,2]))
selected
Out[19]:
tensor([[-1.1821, -0.2556],
        [-0.5557,  1.5086],
        [ 0.7669,  0.2401],
        [-0.7632,  0.0454]])
In [20]:
# https://pytorch.org/docs/stable/torch.html?highlight=masked_select#torch.masked_select
# torch.masked_select(input, mask)
# masked_select¸¦ ÅëÇØ »Ì°íÀÚ ÇÏ´Â °ªµéÀ» ¸¶½ºÅ·Çؼ­ ¼±ÅÃÇÒ ¼ö ÀÖ½À´Ï´Ù.

x = torch.randn(2,3)
mask = torch.ByteTensor([[0,0,1],[0,1,0]])
out = torch.masked_select(x,mask)

print(x, mask, out, sep="\n\n")
tensor([[-0.0036,  0.7174, -0.0733],
        [-0.9133, -0.6957, -0.7995]])

tensor([[0, 0, 1],
        [0, 1, 0]], dtype=torch.uint8)

tensor([-0.0733, -0.6957])

2) Joining

  • torch.cat()
  • torch.stack()
In [21]:
# https://pytorch.org/docs/stable/torch.html?highlight=torch%20cat#torch.cat
# torch.cat(seq, dim=0) -> concatenate tensor along dim
# cat ÇÔ¼ö¸¦ ÀÌ¿ëÇØ ÅÙ¼­¸¦ ¿øÇϴ´ë·Î ºÙÀÏ ¼ö ÀÖ½À´Ï´Ù.
# catÀº concatenateÀÇ ¾àÀÚÀÔ´Ï´Ù.

x = torch.FloatTensor([[1,2,3],[4,5,6]])
y = torch.FloatTensor([[-1,-2,-3],[-4,-5,-6]])
z1 = torch.cat([x,y],dim=0)
z2 = torch.cat([x,y],dim=1)

print(x,y,z1,z2,sep="\n\n")
tensor([[1., 2., 3.],
        [4., 5., 6.]])

tensor([[-1., -2., -3.],
        [-4., -5., -6.]])

tensor([[ 1.,  2.,  3.],
        [ 4.,  5.,  6.],
        [-1., -2., -3.],
        [-4., -5., -6.]])

tensor([[ 1.,  2.,  3., -1., -2., -3.],
        [ 4.,  5.,  6., -4., -5., -6.]])
In [22]:
# https://pytorch.org/docs/stable/torch.html?highlight=torch%20stack#torch.stack
# torch.stack(sequence,dim=0) -> stack along new dim
# stack ÇÔ¼ö¸¦ ÅëÇØ ÅÙ¼­¸¦ ºÙÀϼöµµ ÀÖ½À´Ï´Ù.

x = torch.FloatTensor([[1,2,3],[4,5,6]])
x_stack = torch.stack([x,x,x,x],dim=0)

print(x_stack,x_stack.size(),sep="\n\n")
tensor([[[1., 2., 3.],
         [4., 5., 6.]],

        [[1., 2., 3.],
         [4., 5., 6.]],

        [[1., 2., 3.],
         [4., 5., 6.]],

        [[1., 2., 3.],
         [4., 5., 6.]]])

torch.Size([4, 2, 3])

3) Slicing

  • torch.chunk()
  • torch.split()
In [23]:
# https://pytorch.org/docs/stable/torch.html?highlight=chunk#torch.chunk
# torch.chunk(tensor, chunks, dim=0) -> tensor into number of chunks
# chunk ÇÔ¼ö¸¦ ÅëÇØ ÅÙ¼­¸¦ ¿øÇÏ´Â chunk °³¼ö¸¸Å­À¸·Î ºÐ¸®ÇÒ ¼ö ÀÖ½À´Ï´Ù.

x_1, x_2 = torch.chunk(z1,2,dim=0)
y_1, y_2, y_3 = torch.chunk(z1,3,dim=1)

print(z1,x_1,x_2,z1,y_1,y_2,y_3,sep="\n")
tensor([[ 1.,  2.,  3.],
        [ 4.,  5.,  6.],
        [-1., -2., -3.],
        [-4., -5., -6.]])
tensor([[1., 2., 3.],
        [4., 5., 6.]])
tensor([[-1., -2., -3.],
        [-4., -5., -6.]])
tensor([[ 1.,  2.,  3.],
        [ 4.,  5.,  6.],
        [-1., -2., -3.],
        [-4., -5., -6.]])
tensor([[ 1.],
        [ 4.],
        [-1.],
        [-4.]])
tensor([[ 2.],
        [ 5.],
        [-2.],
        [-5.]])
tensor([[ 3.],
        [ 6.],
        [-3.],
        [-6.]])
In [24]:
# https://pytorch.org/docs/stable/torch.html?highlight=split#torch.split
# torch.split(tensor,split_size,dim=0) -> split into specific size
# split ÇÔ¼ö¸¦ ÅëÇØ ¿øÇÏ´Â »çÀÌÁî·Î ÅÙ¼­¸¦ ÀÚ¸¦ ¼ö ÀÖ½À´Ï´Ù.

x1,x2 = torch.split(z1,2,dim=0)
y1 = torch.split(z1,2,dim=1) 

print(z1,x1,x2,sep="\n")

print("\nThis is y1:")
for i in y1:
  print(i)
tensor([[ 1.,  2.,  3.],
        [ 4.,  5.,  6.],
        [-1., -2., -3.],
        [-4., -5., -6.]])
tensor([[1., 2., 3.],
        [4., 5., 6.]])
tensor([[-1., -2., -3.],
        [-4., -5., -6.]])

This is y1:
tensor([[ 1.,  2.],
        [ 4.,  5.],
        [-1., -2.],
        [-4., -5.]])
tensor([[ 3.],
        [ 6.],
        [-3.],
        [-6.]])

4) squeezing

In [25]:
# https://pytorch.org/docs/stable/torch.html?highlight=squeeze#torch.squeeze
# torch.squeeze(input,dim=None) -> reduce dim by 1
# squeeze ÇÔ¼ö¸¦ ÅëÇØ ±æÀÌ°¡ 1ÀÎ Â÷¿øµéÀ» ¾ÐÃà½Ãų ¼ö ÀÖ½À´Ï´Ù.

x1 = torch.FloatTensor(10,1,3,1,4)
x2 = torch.squeeze(x1)

print(x1.size(),x2.size(),sep="\n")
torch.Size([10, 1, 3, 1, 4])
torch.Size([10, 3, 4])
In [26]:
# https://pytorch.org/docs/stable/torch.html#torch.unsqueeze
# torch.unsqueeze(input,dim=None) -> add dim by 1
# squeeze¿Í ¹Ý´ë·Î unsqueeze¸¦ ÅëÇØ Â÷¿øÀ» ´Ã¸±¼ö ÀÖ½À´Ï´Ù.

x1 = torch.FloatTensor(10,3,4)
x2 = torch.unsqueeze(x1,dim=0)

print(x1.size(),x2.size(),sep="\n")
torch.Size([10, 3, 4])
torch.Size([1, 10, 3, 4])

3. Initialization

  • ÅÙ¼­ °ª ÃʱâÈ­
In [27]:
# _(underbar) refers to in-place operation -> https://discuss.pytorch.org/t/what-is-in-place-operation/16244
import torch.nn.init as init

# https://pytorch.org/docs/stable/nn.html?highlight=init%20uniform#torch.nn.init.uniform_
# uniform ºÐÆ÷¸¦ µû¶ó ÅÙ¼­¸¦ ÃʱâÈ­ ÇÒ ¼ö ÀÖ½À´Ï´Ù.
x1 = init.uniform_(torch.FloatTensor(3,4),a=0,b=9) 

# https://pytorch.org/docs/stable/nn.html?highlight=init%20uniform#torch.nn.init.normal_
# Á¤±Ô ºÐÆ÷¸¦ µû¶ó ÅÙ¼­¸¦ ÃʱâÈ­ ÇÒ ¼ö ÀÖ½À´Ï´Ù.
x2 = init.normal_(torch.FloatTensor(3,4),std=0.2)

# https://pytorch.org/docs/stable/nn.html?highlight=init%20uniform#torch.nn.init.constant_
# ÁöÁ¤ÇÑ °ªÀ¸·Î ÅÙ¼­¸¦ ÃʱâÈ­ ÇÒ ¼ö ÀÖ½À´Ï´Ù.
x3 = init.constant_(torch.FloatTensor(3,4),3.1415)

x1,x2,x3
Out[27]:
(tensor([[2.7964, 0.5791, 7.9576, 3.1357],
         [5.0798, 3.0238, 4.4316, 2.2166],
         [0.1232, 0.1668, 5.4055, 7.5189]]),
 tensor([[-0.0223, -0.1276, -0.1218, -0.3168],
         [ 0.2625,  0.1122,  0.2551,  0.2158],
         [-0.2849,  0.1506, -0.0209,  0.1337]]),
 tensor([[3.1415, 3.1415, 3.1415, 3.1415],
         [3.1415, 3.1415, 3.1415, 3.1415],
         [3.1415, 3.1415, 3.1415, 3.1415]]))

4. Math Operations

1) Arithmetic operations

In [28]:
# https://pytorch.org/docs/stable/torch.html?highlight=add#torch.add
# torch.add()
# ´õÇϱ⠿¬»ê

x1 = torch.FloatTensor([[1,2,3],[4,5,6]])
x2 = torch.FloatTensor([[1,2,3],[4,5,6]])
add = torch.add(x1,x2)

print(x1,x2,add,x1+x2,x1-x2,sep="\n")
tensor([[1., 2., 3.],
        [4., 5., 6.]])
tensor([[1., 2., 3.],
        [4., 5., 6.]])
tensor([[ 2.,  4.,  6.],
        [ 8., 10., 12.]])
tensor([[ 2.,  4.,  6.],
        [ 8., 10., 12.]])
tensor([[0., 0., 0.],
        [0., 0., 0.]])
In [29]:
# torch.add() broadcasting
# ´õÇϱ⠿¬»êÀÇ ºê·Îµåij½ºÆÃ

x1 = torch.FloatTensor([[1,2,3],[4,5,6]])
x2 = torch.add(x1,10)

print(x1,x2,x1+10,x2-10,sep="\n")
tensor([[1., 2., 3.],
        [4., 5., 6.]])
tensor([[11., 12., 13.],
        [14., 15., 16.]])
tensor([[11., 12., 13.],
        [14., 15., 16.]])
tensor([[1., 2., 3.],
        [4., 5., 6.]])
In [30]:
# https://pytorch.org/docs/stable/torch.html?highlight=add#torch.mul
# torch.mul() -> element-wise multiplication 
# size better match
# °öÇϱ⠿¬»êÀÔ´Ï´Ù. °¢°¢ÀÇ ¿ä¼Ò°£ °öÀ̱⠶§¹®¿¡ »çÀÌÁî°¡ ÀÏÄ¡ÇØ¾ß ¿¬»êÀÌ °¡´ÉÇÕ´Ï´Ù.

x1 = torch.FloatTensor([[1,2,3],[4,5,6]])
x2 = torch.FloatTensor([[1,2,3],[4,5,6]])
x3 = torch.mul(x1,x2)

x3
Out[30]:
tensor([[ 1.,  4.,  9.],
        [16., 25., 36.]])
In [31]:
# torch.mul() -> broadcasting
# °öÇϱ⠿¬»êÀÇ ºê·Îµåij½ºÆÃ

x1 = torch.FloatTensor([[1,2,3],[4,5,6]])
x2 = x1*10

x2
Out[31]:
tensor([[10., 20., 30.],
        [40., 50., 60.]])
In [32]:
# https://pytorch.org/docs/stable/torch.html?highlight=add#torch.div
# torch.div() -> size better match
# ³ª´©±â ¿¬»ê. °¢°¢ÀÇ ¿ä¼Ò°£ ³ª´©±âÀ̱⠶§¹®¿¡ »çÀÌÁî°¡ ÀÏÄ¡ÇØ¾ß ¿¬»êÀÌ °¡´ÉÇÕ´Ï´Ù.

x1 = torch.FloatTensor([[1,2,3],[4,5,6]])
x2 = torch.FloatTensor([[1,2,3],[4,5,6]])
x3 = torch.div(x1,x2)

x3
Out[32]:
tensor([[1., 1., 1.],
        [1., 1., 1.]])
In [33]:
# torch.div() -> broadcasting
# ³ª´©±â ¿¬»êÀÇ ºê·Îµåij½ºÆÃ

x1 = torch.FloatTensor([[1,2,3],[4,5,6]])
x1/5
Out[33]:
tensor([[0.2000, 0.4000, 0.6000],
        [0.8000, 1.0000, 1.2000]])

2) Other Operations

In [34]:
# https://pytorch.org/docs/stable/torch.html?highlight=add#torch.pow
# torch.pow(input,exponent)
# ÆÄ¿ö ¿¬»ê(xÀÇ n½Â)

x1 = torch.FloatTensor(3,4)
torch.pow(x1,2),x1**2
Out[34]:
(tensor([[0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],
         [       nan, 0.0000e+00, 1.6868e-15, 1.0853e-17],
         [2.8730e-15,        inf, 4.5243e-14,        inf]]),
 tensor([[0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],
         [       nan, 0.0000e+00, 1.6868e-15, 1.0853e-17],
         [2.8730e-15,        inf, 4.5243e-14,        inf]]))
In [35]:
# https://pytorch.org/docs/stable/torch.html?highlight=add#torch.exp
# torch.exp(tensor,out=None) 
# exponential ¿¬»ê

x1 = torch.FloatTensor(3,4)
torch.exp(x1)
Out[35]:
tensor([[1.0000, 1.0000, 1.0000, 1.0000],
        [   nan, 1.0000, 1.0000, 1.0000],
        [1.0000,    inf, 1.0000,    inf]])
In [36]:
# https://pytorch.org/docs/stable/torch.html?highlight=add#torch.log
# torch.log(input, out=None) -> natural logarithm
# ·Î±× ¿¬»ê

x1 = torch.FloatTensor(3,4)
torch.log(x1)
Out[36]:
tensor([[     nan,     -inf, -99.8132,     -inf],
        [     nan,     -inf, -17.0080, -19.5310],
        [-16.7417,  48.1868, -15.3634,  48.4053]])

3) Matrix Operations

In [37]:
# https://pytorch.org/docs/stable/torch.html?highlight=add#torch.mm
# torch.mm(mat1, mat2) -> matrix multiplication
# Çà·Ä°ö ¿¬»ê

x1 = torch.FloatTensor(3,4)
x2 = torch.FloatTensor(4,5)

torch.mm(x1,x2)
Out[37]:
tensor([[ 7.1466e-44,  0.0000e+00,         nan,  0.0000e+00,  0.0000e+00],
        [        nan,         nan,         nan,         nan,         nan],
        [ 3.8561e-20,  0.0000e+00,         nan,  0.0000e+00, -3.5468e-34]])
In [38]:
# https://pytorch.org/docs/stable/torch.html?highlight=add#torch.bmm
# torch.bmm(batch1, batch2) -> batch matrix multiplication
# ¹èÄ¡ Çà·Ä°ö ¿¬»ê. ¸Ç ¾Õ¿¡ batch Â÷¿øÀº ¹«½ÃÇÏ°í µÚ¿¡ ¿ä¼Òµé·Î Çà·Ä°öÀ» ÇÕ´Ï´Ù.

x1 = torch.FloatTensor(10,3,4)
x2 = torch.FloatTensor(10,4,5)

torch.bmm(x1,x2).size()
Out[38]:
torch.Size([10, 3, 5])
In [39]:
# https://pytorch.org/docs/stable/torch.html?highlight=add#torch.dot
# torch.dot(tensor1,tensor2) -> dot product of two tensor
# µÎ ÅÙ¼­°£ÀÇ ÇÁ·Î´öÆ® ¿¬»ê

x1 = torch.tensor([2, 3])
x2 = torch.tensor([2, 1])

torch.dot(x1,x2)
Out[39]:
tensor(7)
In [40]:
# https://pytorch.org/docs/stable/torch.html?highlight=add#torch.t
# torch.t(matrix) -> transposed matrix
# Çà·ÄÀÇ ÀüÄ¡

x1 = torch.tensor([[1,2],[3,4]])
print(x1,x1.t(),sep="\n")
tensor([[1, 2],
        [3, 4]])
tensor([[1, 3],
        [2, 4]])
In [41]:
# https://pytorch.org/docs/stable/torch.html?highlight=add#torch.transpose
# torch.transpose(input,dim0,dim1) -> transposed matrix
# Â÷¿øÀ» ÁöÁ¤ÇÒ ¼ö ÀÖ´Â Çà·ÄÀÇ ÀüÄ¡ ¿¬»ê

x1 = torch.FloatTensor(10,3,4)
print(x1.size(), torch.transpose(x1,1,2).size(), x1.transpose(1,2).size(),sep="\n")
torch.Size([10, 3, 4])
torch.Size([10, 4, 3])
torch.Size([10, 4, 3])
In [0]: