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>
相关推荐
我狸才不是赔钱货15 小时前
Python的“环境之殇”:从Venv到Conda的终极抉择
开发语言·python·conda
程序员爱钓鱼16 小时前
Python编程实战 - 函数与模块化编程 - 参数与返回值
后端·python·ipython
程序员爱钓鱼16 小时前
Python编程实战 - 函数与模块化编程 - 局部变量与全局变量
后端·python·ipython
jiuri_12151 天前
Docker使用详解:在ARM64嵌入式环境部署Python应用
python·docker·容器
chenchihwen1 天前
AI代码开发宝库系列:Function Call
人工智能·python·1024程序员节·dashscope
FreeBuf_1 天前
微软Copilot被用于窃取OAuth令牌,AI Agent成为攻击者帮凶
人工智能·microsoft·copilot
学slam的小范1 天前
ROS跑ORB-SLAM3遇见的问题总结
人工智能·机器人·自动驾驶
coding消烦员1 天前
新版 vscode 去除快捷键 Ctrl+I 显示 Copilot 的 AI 对话框
人工智能·vscode·copilot
周杰伦_Jay1 天前
【自动驾驶开源仿真平台】Carla、AirSim、Udacity self-driving-car-sim、Apollo、Autoware。
人工智能·机器学习·自动驾驶