가중치 여러 번 업데이트하기
이제 여러 번 업데이트를 수행해 모델 가중치를 크게 개선하고, 각 업데이트마다 예측이 어떻게 좋아지는지 확인해 보겠습니다.
코드를 깔끔하게 유지하기 위해, input_data, target, weights를 인수로 받는 사전 로드된 get_slope() 함수가 제공됩니다. 동일한 인수를 받는 get_mse() 함수도 준비되어 있습니다. input_data, target, weights는 이미 로드되어 있습니다.
이 네트워크에는 은닉층이 없으며, 입력(노드 3개)에서 바로 출력 노드로 연결됩니다. weights는 단일 배열이라는 점에 유의하세요.
또한 matplotlib.pyplot이 미리 로드되어 있으며, 경사 하강 단계를 수행한 뒤 오류 히스토리가 그려집니다.
이 연습은 강의의 일부입니다
Python으로 시작하는 Deep Learning
연습 안내
for루프를 사용해 가중치를 반복적으로 업데이트하세요:get_slope()함수로 기울기를 계산합니다.- 학습률
0.01을 사용해 가중치를 업데이트합니다. - 업데이트된 가중치로
get_mse()함수를 사용하여 평균제곱오차(mse)를 계산합니다. mse를mse_hist에 추가합니다.
- '답변 제출'을 눌러
mse_hist를 시각화하세요. 어떤 추세가 보이나요?
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
n_updates = 20
mse_hist = []
# Iterate over the number of updates
for i in range(n_updates):
# Calculate the slope: slope
slope = ____(____, ____, ____)
# Update the weights: weights
weights = ____ - ____ * ____
# Calculate mse with new weights: mse
mse = ____(____, ____, ____)
# Append the mse to mse_hist
____
# Plot the mse history
plt.plot(mse_hist)
plt.xlabel('Iterations')
plt.ylabel('Mean Squared Error')
plt.show()