
Data Science
118
의 그래디언트는 각 데이터 포인트에서 계산된 그래디언트의 평균이다.
다음과 같이 경사 하강법을 적용해 보자.
1
. 임의의
theta
로 시작
2
. 모든 그래디언트의 평균을 계산
3
.
theta
를
2
번에서 계산된 값으로 변경
4
. 반복
전체 데이터셋을 한 번 훑어본다는 의미의 에폭(
epoch
)을 여러 번 수행하면 올
바른 경사와 절편이 학습되었을 것이다.
from scratch.linear
_
algebra import vector
_
mean
#
임의의
경사와
절편으로
시작
theta = [random.uniform(-1, 1), random.uniform(-1, 1)]
learning
_
rate = 0.001
for epoch in range(5000):
#
모든
그래디언트의
평균을
계산
grad = vector
_
mean([linear
_
gradient(x, y, theta) for x, y in inputs])
#
그래디언트만큼
이동
theta = gradient
_
step(theta, grad, -learning
_
rate)
print(epoch, theta)
slope, intercept = theta
assert 19.9 < slope < 20.1, "slope should be about 20"
assert 4.9 < intercept ...