TensorFlow Automatic Differentiation Guide

In TensorFlow, automatic differentiation is achieved through the context manager tf.GradientTape. Using tf.GradientTape easily computes gradients of tensors with respect to certain variables.

Here is a simple example showing how to use tf.GradientTape for automatic differentiation in TensorFlow.

import tensorflow as tf

# 定义输入变量
x = tf.constant(3.0)

# 定义需要求导的函数
def f(x):
    return x**2

# 在tf.GradientTape()上下文中记录操作
with tf.GradientTape() as tape:
    # 计算函数值
    y = f(x)

# 计算关于x的导数
dy_dx = tape.gradient(y, x)
print(dy_dx)

In the example above, we defined a simple function f(x) = x^2, then used tf.GradientTape() to record the relevant operations and calculated the derivative of the function with respect to the variable x using the tape.gradient() method.

TensorFlow automatically calculates gradients based on the recorded operation graph and returns the corresponding derivative values. This feature of automatic differentiation allows users to easily compute gradients and accelerate the training process of machine learning models.

bannerAds