tensorflow检查op是否可导(反向传播)

1.安装最新版tf--tensorflow1.5,gpu版本需要CUDA8和cudnn6,命令如下

 GPU版:sudo pip3 install tf-nightly-gpu

 CPU版:sudo pip3 install tf-nightly

对应pip网站:tf-nightly-gpu · PyPI

2.编写代码进行测试,主要包括可导函数square和不可导函数floor

代码参考网站https://research.googleblog.com/2017/10/eager-execution-imperative-define-by.html

代码示例:

import numpy as np

import tensorflow as tf

import tensorflow.contrib.eager as tfe

tfe.enable_eager_execution()

def floor(x):

  return tf.floor(x)

def square(x):

  return tf.multiply(x, x)

grad_f = tfe.gradients_function(floor)

print(floor(3.))    

print('gradient of floor:',grad_f(3.))

grad_s = tfe.gradients_function(square)

print(square(3.))

print('gradient of square:',grad_s([3.]))

代码输出:

tf.Tensor(3.0, shape=(), dtype=float32)

gradient of floor: [None]

tf.Tensor(9.0, shape=(), dtype=float32)

gradient of square: [<tf.Tensor: id=21, shape=(1,), dtype=float32, numpy=array([ 6.], dtype=float32)>]

3.小结

     可以看出,floor函数对应的梯度为None,而square函数对应的梯度为 derivative(x^2)=2*x|x=3=6

福利区:独学而无友,则孤陋而寡闻.加入机器学习与深度学习讨论QQ群(581789266),一块交流与探讨,共同成长和进步!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

upDiff

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值