더북(TheBook)

다음은 훈련 스텝으로 훈련 데이터를 받아 이 데이터에 대한 손실을 최소화하도록 가중치 W와 b를 업데이트합니다.

코드 3-20 훈련 스텝 함수

learning_rate = 0.1   

def training_step(inputs, targets):
    with tf.GradientTape() as tape: ➊
        predictions = model(inputs)                        
        loss = square_loss(targets, predictions) ➊         
    grad_loss_wrt_W, grad_loss_wrt_b = tape.gradient(loss, [W, b]) ➋
    W.assign_sub(grad_loss_wrt_W * learning_rate) ➌
    b.assign_sub(grad_loss_wrt_b * learning_rate) ➌
    return loss

➊ 그레이디언트 테이프 블록 안의 정방향 패스

➋ 가중치에 대한 손실의 그레이디언트를 구합니다.

➌ 가중치를 업데이트합니다.