beginner/examples_tensor/polynomial_numpy
Run in Google Colab
Colab
Download Notebook
Notebook
View on GitHub
GitHub
참고
Go to the end to download the full example code.
준비 운동: NumPy#
\(y=\sin(x)\) 을 예측할 수 있도록, \(-\pi\) 부터 \(pi\) 까지 유클리드 거리(Euclidean distance)를 최소화하도록 3차 다항식을 학습합니다.
이 구현은 NumPy를 사용하여 순전파 단계와 손실(loss), 역전파 단계를 직접 계산합니다.
NumPy 배열은 일반적인 n-차원 배열로, 딥러닝이나 변화도(gradient), 연산 그래프(computational graph)는 알지 못하며 일반적인 수치 연산을 수행합니다.
99 775.3612839295903
199 529.5153202041848
299 362.90943470582715
399 249.88196904277515
499 173.1191458739682
599 120.9278540261685
699 85.40309026892623
799 61.19529422720715
899 44.680468577894274
999 33.40096190546195
1099 25.688284931622483
1199 20.408460097595864
1299 16.78992753444885
1399 14.30712500984892
1499 12.60164908000841
1599 11.428809433943101
1699 10.62135847338875
1799 10.064849747259494
1899 9.680878256887098
1999 9.415668723564835
Result: y = 0.021626061950050325 + 0.869821636959982 x + -0.0037308555456078397 x^2 + -0.09519101806235512 x^3
import numpy as np
import math
# 무작위로 입력과 출력 데이터를 생성합니다
x = np.linspace(-math.pi, math.pi, 2000)
y = np.sin(x)
# 무작위로 가중치를 초기화합니다
a = np.random.randn()
b = np.random.randn()
c = np.random.randn()
d = np.random.randn()
learning_rate = 1e-6
for t in range(2000):
# 순전파 단계: 예측값 y를 계산합니다
# y = a + b x + c x^2 + d x^3
y_pred = a + b * x + c * x ** 2 + d * x ** 3
# 손실(loss)을 계산하고 출력합니다
loss = np.square(y_pred - y).sum()
if t % 100 == 99:
print(t, loss)
# 손실에 따른 a, b, c, d의 변화도(gradient)를 계산하고 역전파합니다.
grad_y_pred = 2.0 * (y_pred - y)
grad_a = grad_y_pred.sum()
grad_b = (grad_y_pred * x).sum()
grad_c = (grad_y_pred * x ** 2).sum()
grad_d = (grad_y_pred * x ** 3).sum()
# 가중치를 갱신합니다.
a -= learning_rate * grad_a
b -= learning_rate * grad_b
c -= learning_rate * grad_c
d -= learning_rate * grad_d
print(f'Result: y = {a} + {b} x + {c} x^2 + {d} x^3')
Total running time of the script: (0 minutes 0.567 seconds)