pytorch basic¶
Numpy to tensor¶
In [1]:
import numpy as np
n_array = np.arange(10).reshape(2,5)
n_array
Out[1]:
In [2]:
print(n_array.ndim)
print(n_array.shape)
ndim
: 차원확인shape
: 모양확인
In [3]:
import torch
t_array = torch.FloatTensor(n_array)
t_array
Out[3]:
In [4]:
type(t_array)
Out[4]:
- tensor타입 확인할 수 있다.
- 기본적인 array와 비슷하고 메서드도 비슷하다.
In [5]:
print(t_array.shape)
print(t_array.ndim)
print(t_array.size())
In [6]:
t_array[1:],t_array[:2, :3]
Out[6]:
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))
matmul
: 행렬 연산
In [9]:
n1.dot(n2)
Out[9]:
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]:
In [11]:
t1 * 5
Out[11]:
In [12]:
n1 = np.arange(10)
t1 = torch.FloatTensor(n1)
t1.mean()
Out[12]:
In [13]:
n1 = np.arange(10).reshape(5,2)
t1 = torch.FloatTensor(n1)
t1.mean(dim=0)
Out[13]:
In [14]:
t1.mean(dim=1)
Out[14]:
In [15]:
n1 = np.arange(10)
t1 = torch.FloatTensor(n1)
t1.view(-1, 2)
Out[15]:
In [16]:
n1.reshape(-1, 2), t1.reshape(-1,2)
Out[16]:
- tensor에서는
reshape
과view
둘다로 모양을 바꿀 수 있다. - ndarray에서는
reshape
으로 모양을 바꾼다.
In [17]:
t1.view(-1, 10).squeeze()
Out[17]:
In [18]:
t1.view(-1, 10).squeeze().unsqueeze(dim=0)
Out[18]:
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]:
softmax
함수를 사용하여 해당 차원에 대한 softmax값을 반환
In [21]:
y = torch.randint(5, (10,5))
y_label = y.argmax(dim=1)
y,y_label
Out[21]:
argmax
는 해당 차원의 가장 큰값의 index를 반환해 준다.
In [22]:
torch.nn.functional.one_hot(y_label)
Out[22]:
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]:
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]:
In [30]:
b.grad
Out[30]:
AutoGrad for Linear Regression¶
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
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)
'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 |