May every moment inspire me.

study things

[NLP with Pytorch] PyTorchZeroToAll Lecture02-03

my_jennyee 2019. 7. 23. 11:32

PyTorchZeroToAll by Sung Kim

(https://www.youtube.com/playlist?list=PLlMkM4tgfjnJ3I-dbhO9JTw7gNty6o_2m)

 

PyTorchZeroToAll (in English) - YouTube

Basic ML/DL lectures using PyTorch in English.

www.youtube.com

 

Lecture02-Lecture03 주요 내용 정리

 


 

저는 텐서플로우를 먼저 배웠지만, NLP에 대해서는 파이토치를 쓰는게 좋다는 이야기를 자주 듣게 되었습니다.

이번 기회에 파이토치로 갈아타고자, NLP에 관련된 내용을 정리하려고 합니다.

 

감사하게도 Sung Kim 교수님께서 파이토치에 대한 개념 강의를 열어주셨고(Youtube) 관련 내용을 정리 포스터 입니다. 

 

머신 러닝 (Machine Learning: ML) 과 딥 러닝 (Deep Learning: DL) 의 경계는 아직 확실히 알지는 못합니다. 하지만 ML과 DL을 모든 분야에서 연구에 대한 도구로서 사용하고 있습니다. 

 

 

 

 

PyTorch Lecture02 : Linear Model

 

1. 머신 러닝의 세가지 학습 방식

 

- Supervised learning 지도학습: Training dataset (한 문제에 대한 input 값과 output 값) 을 학습시켜 Test dataset의 값을 추측하는 학습 방식

- Unsupervised learning 비지도학습

- Reinforcement learning강화학습

 

2. Linear Model

PyTorchZeroToAll_by SungKim 

x 의 input 값이 linear 식을 거쳐 y hat output 값이 나왔습니다. 

우리는 이 Linear한 식이 무엇인지, 왜 y hat의 값이 나왔는지 알아보려고 합니다.  

Linear 식을 y hat = x * w 라고 가정하고, 가장 최적화된 w 값을 구하는 과정을 공부하겠습니다.

 

 

PyTorchZeroToAll_by SungKim 

0. Training dataset = ( x , y )

1. w값을 가정한다

2. y hat = x*w, y hat은 가정한 w값으로 계산된 예상된 y값이다.

3. 예상된 y값, y hat과 진짜 y값의 차이를 계산해 절대값화 (제곱) 한다. Training Loss = (y hat - y)^2 이다.

 

PyTorchZeroToAll_by SungKim

 

Loss 가 최소가 되는 w 값을 찾는 것이다.

 

# a random guess : random value
w = 1.0

def forward(x):
	return x * w
    
def loss(x, y):
	y_pred = forward(x)
    return (y_pred - y) * (y_pred - y)
    
for w in np.arange(0.0, 4.1, 0.1):
	print("w=", w)
    l_sum = 0
    for x_val, y_val in zip(x_data, y_data):
    y_pred_val = forward(x_val)
    l = loss(x_val, y_val)
    l_sum += l
    print("\t", x_val, y_val. y_pred_val, l)
    
print("MSE=", l_sum / 3)

 

for문의 기본 구조

for 변수 in 리스트 (또는 튜플, 문자열):
	수행할 문장1
    수행할 문장2
    ...

numpy.arange ([start, ]stop, [step, ]dtype=None)

 

np.arange (https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.arange.html)

np.arange

1. 정수를 value로 하여 범위를 지정해주는 넘파이 함수.

2. np.arange([시작값:정수], 마지막값, [step 값] , dtype=None)

 

Jump to Python _ Wikidocs

Jump to Python (https://wikidocs.net/book/1)

 

위키독스

온라인 책을 제작 공유하는 플랫폼 서비스

wikidocs.net

PyTorch Lecture03 : Gradient Descent 

 

Find w that minimizes the loss!

PyTorchZeroToAll_by SungKim

https://www.derivative-calculator.net/

 

Derivative Calculator • With Steps!

Above, enter the function to derive. Differentiation variable and more can be changed in "Options". Click "Go!" to start the derivative calculation. The result will be shown further below. How the Derivative Calculator Works For those with a technical back

www.derivative-calculator.net

 

x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]

w = 1.0 #이건 임의 w값

def forward(x):
	return x*w
    
def loss(x, y):
	y_pred = forward(x)
    return (y_pred - y) * (y_pred - y)
    
def gradient(x, y):		#gradient는 미분한 식
	return 2 * x * (x * w - y)
    
#Before training
print("predict (before training)", 4, forward(4))

#Traing loop
for epoch in range(100):	#range([start,] stop [,step] ) epoch를 0부터 100R까지 돌리겠다!
	for x_val, y_val in zip(x_data, y_data):
    	grad = gradient(x_val, y_val)
        w = w - 0.01 * grad		#0.01은 알파값
        print("\tgrad: ", x_val, y_val, grad)
        l = loss(x_val, y_val)
        
    print("progress:", epoch, "w=", w, "loss=", l)
    
#After training
print("predict (after training)", "4hours", forward(4))
728x90

'study things' 카테고리의 다른 글

영어 이메일 작성 표현 정리  (0) 2020.06.29
XML Tutorial 02: XML Quiz  (0) 2019.08.09
XML Tutorial 01 (eXtensible Markup Language)  (0) 2019.08.09
IFC / Schema 관련 용어 정리  (0) 2019.08.09
Chap 12. Query Processing  (0) 2019.05.26