Automatic Differentiation for ABSOLUTE beginners: "with tf.GradientTape() as tape"

Опубликовано: 17 Июнь 2026
на канале: Dr. Data Science
2,658
86

#deeplearning #machinelearning #datascience

Automatic differentiation is a key concept in machine learning, particularly in the context of training neural networks

TensorFlow, along with its high-level API Keras, provides a powerful tool for automatic differentiation called `GradientTape`

When you're training a neural network, you need to compute gradients of the loss with respect to the model parameters to update them using optimization algorithms like stochastic gradient descent

GradientTape is like a recording mechanism that watches the operations executed inside its context

During the forward pass, it records the operations for later gradient computation. Once the forward pass is done, you can use the recorded information to compute gradients effortlessly

Training loop using tf.GradientTape
learning_rate = 0.01
epochs = 100

for epoch in range(epochs):
with tf.GradientTape() as tape:
Forward pass
y_pred = linear_regression(x_train)

Calculate mean squared error
loss = mean_squared_error(y_true, y_pred)

Compute gradient with respect to weight (w)
gradient = tape.gradient(loss, w)

Update weight using gradient descent
w.assign_sub(learning_rate * gradient)