查看: 1981|回复: 1

自动微分,优化机器学习模型的关键技术

[复制链接]
  • TA的每日心情
    开心
    2019-11-4 13:48
  • 签到天数: 14 天

    连续签到: 1 天

    [LV.3]偶尔看看II

    发表于 2019-1-10 13:26:37 | 显示全部楼层 |阅读模式
    分享到:
    今天的内容,我们将介绍自动微分,这是优化机器学习模型的关键技术。

    设置
    1. import tensorflow as tf

    2. tf.enable_eager_execution()
    复制代码



    梯度带
    TensorFlow 提供用于自动微分的 tf.GradientTape API - 计算与其输入变量相关的计算梯度。TensorFlow 通过 tf.GradientTape “记录” 在上下文中执行的所有操作到 “磁带”(tape)上。然后,TensorFlow 使用该磁带和与每个记录操作相关联的梯度来计算使用反向模式微分的 “记录” 计算的梯度。

    例如:

    1. x = tf.ones((2, 2))
    2.   
    3. with tf.GradientTape() as t:
    4.   t.watch(x)
    5.   y = tf.reduce_sum(x)
    6.   z = tf.multiply(y, y)

    7. # Derivative of z with respect to the original input tensor x
    8. dz_dx = t.gradient(z, x)
    9. for i in [0, 1]:
    10.   for j in [0, 1]:
    11.     assert dz_dx[i][j].numpy() == 8.0
    复制代码


    您还可以根据在 “记录” tf.GradientTape 上下文时计算的中间值请求输出的梯度。
    1. x = tf.ones((2, 2))
    2.   
    3. with tf.GradientTape() as t:
    4.   t.watch(x)
    5.   y = tf.reduce_sum(x)
    6.   z = tf.multiply(y, y)

    7. # Use the tape to compute the derivative of z with respect to the
    8. # intermediate value y.
    9. dz_dy = t.gradient(z, y)
    10. assert dz_dy.numpy() == 8.0
    复制代码


    默认情况下,GradientTape 持有的资源会在调用 GradientTape.gradient() 方法后立即释放。要在同一计算中计算多个梯度,创建一个持久的梯度带。这允许多次调用 gradient() 方法。当磁带对象 tape 被垃圾收集时释放资源。例如:
    1. x = tf.constant(3.0)
    2. with tf.GradientTape(persistent=True) as t:
    3.   t.watch(x)
    4.   y = x * x
    5.   z = y * y
    6. dz_dx = t.gradient(z, x)  # 108.0 (4*x^3 at x = 3)
    7. dy_dx = t.gradient(y, x)  # 6.0
    8. del t  # Drop the reference to the tape
    复制代码


    记录控制流
    因为磁带(tape)在执行时记录操作,所以自然会处理 Python 控制流(例如使用 ifs 和 whiles):

    1. def f(x, y):
    2.   output = 1.0
    3.   for i in range(y):
    4.     if i > 1 and i < 5:
    5.       output = tf.multiply(output, x)
    6.   return output

    7. def grad(x, y):
    8.   with tf.GradientTape() as t:
    9.     t.watch(x)
    10.     out = f(x, y)
    11.   return t.gradient(out, x)

    12. x = tf.convert_to_tensor(2.0)

    13. assert grad(x, 6).numpy() == 12.0
    14. assert grad(x, 5).numpy() == 12.0
    15. assert grad(x, 4).numpy() == 4.0
    复制代码


    高阶梯度
    GradientTape 记录上下文管理器内部的操作以实现自动区分。如果梯度是在这个上下文中计算的,那么梯度计算也会被记录下来。因此,同样的 API 也适用于高阶梯度。例如:

    1. x = tf.Variable(1.0)  # Create a Tensorflow variable initialized to 1.0

    2. with tf.GradientTape() as t:
    3.   with tf.GradientTape() as t2:
    4.     y = x * x * x
    5.   # Compute the gradient inside the 't' context manager
    6.   # which means the gradient computation is differentiable as well.
    7.   dy_dx = t2.gradient(y, x)
    8. d2y_dx2 = t.gradient(dy_dx, x)

    9. assert dy_dx.numpy() == 3.0
    10. assert d2y_dx2.numpy() == 6.0
    复制代码


    以上教程中,我们介绍了 TensorFlow 中的梯度计算。有了这些,我们就有了足够的基本要素来构建和训练神经网络。

    本文转载自 TensorFlow 公众号





    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条

    手机版|小黑屋|与非网

    GMT+8, 2024-4-19 21:36 , Processed in 0.117926 second(s), 17 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.