Tensorflow2.0教程-自动求导

一、前言:

    目前谷歌已经开源了tensorflow2.0版本,本着铁粉的信念,学习记录一下新的征程。 

二、GradientTape(自动求导)介绍:

    tensorflow2.0版本提供tf.GradientTape api来实现对数学公式的自动求导功能。默认情况下GradientTape的资源会在执行tf.GradientTape()后被释放,即每个tape只能求一次导数。

with tf.GradientTape() as t:

如果想多次计算梯度,需要创建一个持久的GradientTape。

with tf.GradientTape(persistent=True) as t:

三、GradientTape(自动求导)代码实现:

import tensorflow as tf

x = tf.constant(3.0)

with tf.GradientTape(persistent=True) as t:
    t.watch(x)       # Ensures that `tensor` is being traced by this tape.
    y = x * x
    z = y * y
dz_dx = t.gradient(z, x)  # 108.0 (4*x^3 at x = 3)
dy_dx = t.gradient(y, x)  # 6.0
print("dz/dx=", dz_dx.numpy())
print("dy/dx=", dy_dx.numpy())
del t  # Drop the reference to the tape

注:Calling GradientTape.gradient on a persistent tape inside its context is significantly less efficient than calling it outside the context (it causes the gradient ops to be recorded on the tape, leading to increased CPU and memory usage).

参考资料:

1.https://github.com/czy36mengfei/tensorflow2_tutorials_chinese/blob/master/020-Eager/003-automatic_differentiation.ipynb

2.https://github.com/YunYang1994/TensorFlow2.0-Examples/blob/master/1-Introduction/GradientTape.py

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值