pytorch basic

pytorch basic


Numpy to tensor

In [1]:
import numpy as np
n_array = np.arange(10).reshape(2,5)
n_array
Out[1]:
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
In [2]:
print(n_array.ndim)
print(n_array.shape)
2
(2, 5)
  • ndim: 차원확인
  • shape: 모양확인
In [3]:
import torch
t_array = torch.FloatTensor(n_array)
t_array
Out[3]:
tensor([[0., 1., 2., 3., 4.],
        [5., 6., 7., 8., 9.]])
In [4]:
type(t_array)
Out[4]:
torch.Tensor
  • tensor타입 확인할 수 있다.
  • 기본적인 array와 비슷하고 메서드도 비슷하다.
In [5]:
print(t_array.shape)
print(t_array.ndim)
print(t_array.size())
torch.Size([2, 5])
2
torch.Size([2, 5])
In [6]:
t_array[1:],t_array[:2, :3]
Out[6]:
(tensor([[5., 6., 7., 8., 9.]]),
 tensor([[0., 1., 2.],
         [5., 6., 7.]]))



tensor operations

In [7]:
n1 = np.arange(10).reshape(2,5)
n2 = np.arange(10).reshape(5,2)
In [8]:
t1 = torch.FloatTensor(n1)
t2 = torch.FloatTensor(n2)
print('Shape of t1: ', t1.shape)
print('Shape of t2: ', t2.shape)
print(t1.matmul(t2))
Shape of t1:  torch.Size([2, 5])
Shape of t2:  torch.Size([5, 2])
tensor([[ 60.,  70.],
        [160., 195.]])
  • matmul: 행렬 연산
In [9]:
n1.dot(n2)
Out[9]:
array([[ 60,  70],
       [160, 195]])
  • dot: 같은 행렬 연산
In [10]:
n1 = np.arange(4).reshape(2,2)
n2 = np.arange(4).reshape(2,2)
t1 = torch.FloatTensor(n1)
t2 = torch.FloatTensor(n2)

t1 * t2, t1.mul(t2)
Out[10]:
(tensor([[0., 1.],
         [4., 9.]]),
 tensor([[0., 1.],
         [4., 9.]]))
In [11]:
t1 * 5
Out[11]:
tensor([[ 0.,  5.],
        [10., 15.]])
In [12]:
n1 = np.arange(10)
t1 = torch.FloatTensor(n1)
t1.mean()
Out[12]:
tensor(4.5000)
In [13]:
n1 = np.arange(10).reshape(5,2)
t1 = torch.FloatTensor(n1)
t1.mean(dim=0)
Out[13]:
tensor([4., 5.])
In [14]:
t1.mean(dim=1)
Out[14]:
tensor([0.5000, 2.5000, 4.5000, 6.5000, 8.5000])
In [15]:
n1 = np.arange(10)
t1 = torch.FloatTensor(n1)
t1.view(-1, 2)
Out[15]:
tensor([[0., 1.],
        [2., 3.],
        [4., 5.],
        [6., 7.],
        [8., 9.]])
In [16]:
n1.reshape(-1, 2), t1.reshape(-1,2)
Out[16]:
(array([[0, 1],
        [2, 3],
        [4, 5],
        [6, 7],
        [8, 9]]),
 tensor([[0., 1.],
         [2., 3.],
         [4., 5.],
         [6., 7.],
         [8., 9.]]))
  • tensor에서는 reshapeview 둘다로 모양을 바꿀 수 있다.
  • ndarray에서는 reshape으로 모양을 바꾼다.
In [17]:
t1.view(-1, 10).squeeze()
Out[17]:
tensor([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
In [18]:
t1.view(-1, 10).squeeze().unsqueeze(dim=0)
Out[18]:
tensor([[0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]])



tensor operations for ML/DL formula

In [19]:
import torch
import torch.nn.functional as F
In [20]:
tensor = torch.FloatTensor([0.5, 0.7, 0.1])
h_tensor = F.softmax(tensor, dim=0)
h_tensor
Out[20]:
tensor([0.3458, 0.4224, 0.2318])
  • softmax함수를 사용하여 해당 차원에 대한 softmax값을 반환
In [21]:
y = torch.randint(5, (10,5))
y_label = y.argmax(dim=1)
y,y_label
Out[21]:
(tensor([[2, 0, 1, 4, 3],
         [4, 0, 4, 3, 0],
         [3, 1, 3, 2, 2],
         [3, 2, 4, 4, 4],
         [3, 4, 0, 4, 3],
         [0, 0, 2, 1, 4],
         [4, 4, 0, 3, 4],
         [1, 4, 0, 1, 4],
         [1, 0, 3, 0, 0],
         [2, 1, 3, 3, 0]]),
 tensor([3, 0, 0, 2, 1, 4, 0, 1, 2, 2]))
  • argmax는 해당 차원의 가장 큰값의 index를 반환해 준다.
In [22]:
torch.nn.functional.one_hot(y_label)
Out[22]:
tensor([[0, 0, 0, 1, 0],
        [1, 0, 0, 0, 0],
        [1, 0, 0, 0, 0],
        [0, 0, 1, 0, 0],
        [0, 1, 0, 0, 0],
        [0, 0, 0, 0, 1],
        [1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0],
        [0, 0, 1, 0, 0],
        [0, 0, 1, 0, 0]])
  • torch.nn.functional.one_hot은 해당 인자를 one hot encoding해준다.



torch autograd

$$ y = w^2 \\ z = 2*y + 5 \\ z = 2*w^2 + 5 $$
In [23]:
w = torch.tensor(2.0, requires_grad=True)
y = w**2
z = 2*y + 5
  • requires_grad인자는 gradient를 할것인지 안할것인지 설정해 줄 수 있다.
  • 만약 False라면 값이 안변한다.
In [24]:
z.backward()
In [25]:
w.grad
Out[25]:
tensor(8.)
  • grad로 미분 값을 확인할 수 있다.
$$ Q = 3a^3 - b^2 $$
In [26]:
a = torch.tensor([2., 3.], requires_grad=True)
b = torch.tensor([6., 4.], requires_grad=True)
In [27]:
Q = 3*a**3 - b**2
In [28]:
external_grad = torch.tensor([1., 1.])
Q.backward(gradient=external_grad)
In [29]:
a.grad
Out[29]:
tensor([36., 81.])
In [30]:
b.grad
Out[30]:
tensor([-12.,  -8.])
In [31]:
import numpy as np
# create dummy data for training
x_values = [i for i in range(11)]
x_train = np.array(x_values, dtype=np.float32)
x_train = x_train.reshape(-1, 1)

y_values = [2*i + 1 for i in x_values]
y_train = np.array(y_values, dtype=np.float32)
y_train = y_train.reshape(-1, 1)
In [32]:
import torch
from torch.autograd import Variable
class linearRegression(torch.nn.Module):
    def __init__(self, inputSize, outputSize):
        super(linearRegression, self).__init__()
        self.linear = torch.nn.Linear(inputSize, outputSize)

    def forward(self, x):
        out = self.linear(x)
        return out
In [33]:
inputDim = 1        # takes variable 'x' 
outputDim = 1       # takes variable 'y'
learningRate = 0.01 
epochs = 100

model = linearRegression(inputDim, outputDim)
##### For GPU #######
if torch.cuda.is_available():
    model.cuda()
In [34]:
criterion = torch.nn.MSELoss() 
optimizer = torch.optim.SGD(model.parameters(), lr=learningRate)
In [37]:
flag=True
for epoch in range(epochs):
    # Converting inputs and labels to Variable
    if torch.cuda.is_available():
        inputs = Variable(torch.from_numpy(x_train).cuda())
        labels = Variable(torch.from_numpy(y_train).cuda())
    else:
        inputs = Variable(torch.from_numpy(x_train))
        labels = Variable(torch.from_numpy(y_train))

    # Clear gradient buffers because we don't want any gradient from previous epoch to carry forward, dont want to cummulate gradients
    optimizer.zero_grad()

    # get output from the model, given the inputs
    outputs = model(inputs)

    # get loss for the predicted output
    loss = criterion(outputs, labels)
    #print(loss)
    
    # get gradients w.r.t to parameters
    loss.backward()

    # update parameters
    optimizer.step()
    
    if epoch < 5 or epoch > 95 :
        print('epoch {}, loss {}'.format(epoch, loss.item()))
    elif flag:
        print('...')
        flag=False
epoch 0, loss 0.027474697679281235
epoch 1, loss 0.027167877182364464
epoch 2, loss 0.02686447463929653
epoch 3, loss 0.02656450867652893
epoch 4, loss 0.026267867535352707
...
epoch 96, loss 0.009348520077764988
epoch 97, loss 0.00924412626773119
epoch 98, loss 0.009140902198851109
epoch 99, loss 0.009038788266479969
In [36]:
with torch.no_grad(): # we don't need gradients in the testing phase
    if torch.cuda.is_available():
        predicted = model(Variable(torch.from_numpy(x_train).cuda())).cpu().data.numpy()
    else:
        predicted = model(Variable(torch.from_numpy(x_train))).data.numpy()
    print(predicted)
[[ 0.6899262]
 [ 2.7345796]
 [ 4.779233 ]
 [ 6.8238864]
 [ 8.86854  ]
 [10.913194 ]
 [12.957847 ]
 [15.002501 ]
 [17.047153 ]
 [19.091806 ]
 [21.136461 ]]

'AI > 이론' 카테고리의 다른 글

CNN - Convolution  (0) 2021.02.03
CNN Preview  (0) 2021.02.02
Optimization  (0) 2021.02.02
뉴럴 네트워크 - MLP(이론)  (0) 2021.02.01
베이즈 통계학  (0) 2021.02.01
통계학  (0) 2021.01.29
seaborn  (0) 2021.01.29
matplotlib II  (0) 2021.01.29

+ Recent posts