1. 程式人生 > >tensorflow檢查op是否可導(反向傳播)

tensorflow檢查op是否可導(反向傳播)

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

 GPU版:sudo pip3 install tf-nightly-gpu

 CPU版:sudo pip3 install tf-nightly

對應pip網站:https://pypi.python.org/pypi/tf-nightly-gpu

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),一塊交流與探討,共同成長和進步!