TensorFlow之微分求导

目录

前言

在TensorFlow中,微分是个非常重要的概念。它们分别用于自动求导(计算梯度)和高效地处理数据。下面我将分别介绍这两个主题。

微分(Automatic Differentiation)

TensorFlow提供了强大的自动求导功能,这对于训练机器学习模型尤其重要,因为需要通过反向传播算法来更新模型参数。自动求导允许 TensorFlow 自动计算损失函数相对于模型参数的梯度,从而简化了模型训练过程中的优化步骤。

使用 tf.GradientTape

tf.GradientTape是TensorFlow 2.x 中实现自动求导的核心工具。它会记录在其上下文内的所有操作,并可以在需要时计算这些操作相对于输入张量的梯度。

示例

手动微分实现

python 复制代码
from tensorflow import keras
import numpy as np
import pandas
import matplotlib.pyplot as plt


# 3 * x^2 + 2 * x - 1
# 手动微分
def f(x):
    return 3. * x ** 2 + 2. * x - 1.

# 近似求导
def approximate_derivative(f, x, eps=1e-3):
    return (f(x + eps) - f(x - eps)) / (2. * eps)

print(approximate_derivative(f, 1.))

结果如下:

powershell 复制代码
7.999999999999119

两个未知数, 求偏导

python 复制代码
from tensorflow import keras
import numpy as np
import pandas
import matplotlib.pyplot as plt


# 两个未知数, 求偏导
def g(x1, x2):
    return (x1 + 5) * (x2 ** 2)

# 近似求导
def approximate_derivative(f, x, eps=1e-3):
    return (f(x + eps) - f(x - eps)) / (2. * eps)

# 分别求g对x1,和x2的偏导.
def approximate_gradient(g, x1, x2, eps=1e-3):
    dg_x1 = approximate_derivative(lambda x: g(x, x2), x1, eps)
    dg_x2 = approximate_derivative(lambda x: g(x1, x), x2, eps)
    return dg_x1, dg_x2



print(approximate_gradient(g, 2, 3))

结果如下:

powershell 复制代码
(8.999999999993236, 41.999999999994486)

tf.GradientTape常量求导

python 复制代码
from tensorflow import keras
import numpy as np
import pandas
import matplotlib.pyplot as plt
import tensorflow as tf 


x1 = tf.Variable(2.0)
x2 = tf.Variable(3.0)

def g(x1, x2):
    return (x1 + 5) * (x2 ** 2)

with tf.GradientTape() as tape:
    z = g(x1, x2)
    
dz_x1x2 = tape.gradient(z, [x1, x2])
print(dz_x1x2)

结果如下:

powershell 复制代码
[<tf.Tensor: shape=(), dtype=float32, numpy=9.0>, <tf.Tensor: shape=(), dtype=float32, numpy=42.0>]

tf.GradientTape二阶导数

python 复制代码
from tensorflow import keras
import numpy as np
import pandas
import matplotlib.pyplot as plt
import tensorflow as tf 


x1 = tf.Variable(2.0)
x2 = tf.Variable(3.0)

def g(x1, x2):
    return (x1 + 5) * (x2 ** 2)



# 二阶导数
# 嵌套tf.GradientTape
x1 = tf.Variable(2.0)
x2 = tf.Variable(3.0)

with tf.GradientTape(persistent=True) as outer_tape:
    with tf.GradientTape(persistent=True) as inner_tape:
        z = g(x1, x2)
    # 一阶导    
    inner_grads = inner_tape.gradient(z, [x1, x2])
# 对一阶导的结果再求导
outer_grads = [outer_tape.gradient(inner_grad, [x1, x2]) for inner_grad in inner_grads]
print(outer_grads)
del inner_tape
del outer_tape

结果如下:

powershell 复制代码
[[None, <tf.Tensor: shape=(), dtype=float32, numpy=6.0>], [<tf.Tensor: shape=(), dtype=float32, numpy=6.0>, <tf.Tensor: shape=(), dtype=float32, numpy=14.0>]]

tf.GradientTape实现梯度下降

python 复制代码
from tensorflow import keras
import numpy as np
import pandas
import matplotlib.pyplot as plt
import tensorflow as tf 


def f(x):
    return 3. * x ** 2 + 2. * x - 1.

# 使用tf.GradientTape实现梯度下降
learing_rate = 0.1
x = tf.Variable(0.0)

for _ in range(100):
    with tf.GradientTape() as tape:
        z = f(x)
    dz_dx = tape.gradient(z, x)
    x.assign_sub(learing_rate * dz_dx) # x -= learning_rate * dz_dx
print(x)

结果如下:

powershell 复制代码
<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=-0.3333333>

结合optimizer实现梯度下降

python 复制代码
from tensorflow import keras
import numpy as np
import pandas
import matplotlib.pyplot as plt
import tensorflow as tf 


def f(x):
    return 3. * x ** 2 + 2. * x - 1.

from tensorflow import keras

# 结合optimizer去实现梯度下降
learing_rate = 0.1
x = tf.Variable(0.0)
optimizer = keras.optimizers.SGD(lr=learing_rate)

for _ in range(100):
    with tf.GradientTape() as tape:
        z = f(x)
    dz_dx = tape.gradient(z, x)
#     x.assign_sub(learing_rate * dz_dx) # x -= learning_rate * dz_dx
    optimizer.apply_gradients([(dz_dx, x)])
print(x)

结果如下

powershell 复制代码
<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=-0.3333333>
相关推荐
CV实验室26 分钟前
TIP 2025 | 哈工大&哈佛等提出 TripleMixer:攻克雨雪雾干扰的3D点云去噪网络!
人工智能·计算机视觉·3d·论文
apocelipes28 分钟前
golang unique包和字符串内部化
java·python·性能优化·golang
Geoking.1 小时前
NumPy zeros() 函数详解
python·numpy
Full Stack Developme1 小时前
java.text 包详解
java·开发语言·python
余俊晖1 小时前
一套针对金融领域多模态问答的自适应多层级RAG框架-VeritasFi
人工智能·金融·rag
码农阿树2 小时前
视频解析转换耗时—OpenCV优化摸索路
人工智能·opencv·音视频
丁浩6662 小时前
Python机器学习---2.算法:逻辑回归
python·算法·机器学习
B站_计算机毕业设计之家2 小时前
计算机毕业设计:Python农业数据可视化分析系统 气象数据 农业生产 粮食数据 播种数据 爬虫 Django框架 天气数据 降水量(源码+文档)✅
大数据·爬虫·python·机器学习·信息可视化·课程设计·农业
Q_Q5110082853 小时前
python+uniapp基于微信小程序的旅游信息系统
spring boot·python·微信小程序·django·flask·uni-app·node.js
伏小白白白3 小时前
【论文精度-2】求解车辆路径问题的神经组合优化算法:综合展望(Yubin Xiao,2025)
人工智能·算法·机器学习