height2 = 170
foot2 = 260
# 신발 = a * 170 + b
a = tf.Variable(0.1)
b = tf.Variable(0.2)
def 손실함수():
foot_predict = height2 * a + b
return tf.square(foot2 - foot_predict)
opt = tf.keras.optimizers.Adam(learning_rate=0.1) # 경사하강법 도와주는 친구
# 경사하강법 반복
for i in range(300):
with tf.GradientTape() as tape:
loss = 손실함수()
# 매번 새로운 기울기 계산
gradients = tape.gradient(loss, [a, b])
# 경사하강법 적용
opt.apply_gradients(zip(gradients, [a, b]))
# a, b 값 출력
print(f"Step {i+1}: a = {a.numpy():.5f}, b = {b.numpy():.5f}")