tensorflow中有一个计算梯度的函数tf.gradients(ys, xs)，要注意的是，xs中的x必须要与ys相关，不相关的话，会报错。

#wrong
import tensorflow as tf

w1 = tf.Variable([[1,2]], dtype=tf.float32)
w2 = tf.Variable([[3,4]], dtype=tf.float32)

res = tf.matmul(w1, tf.convert_to_tensor([[2],[1]], dtype=tf.float32))

with tf.Session() as sess:
tf.global_variables_initializer().run()
print(re)


TypeError: Fetch argument None has invalid type <class ‘NoneType’>

# right
import tensorflow as tf

w1 = tf.Variable([[1, 2]], dtype=tf.float32)
w2 = tf.Variable([[3, 4]], dtype=tf.float32)

res = tf.matmul(w1, tf.convert_to_tensor([[2], [1]],dtype=tf.float32))

with tf.Session() as sess:
tf.global_variables_initializer().run()
print(re)
#  [array([[2, 1]], dtype=int32)]


import tensorflow as tf

w1 = tf.get_variable('w1', shape=[3])
w2 = tf.get_variable('w2', shape=[3])

w3 = tf.get_variable('w3', shape=[3])
w4 = tf.get_variable('w4', shape=[3])

z1 = w1 + w2+ w3
z2 = w3 + w4

tf.convert_to_tensor([3.,2.,4.])])

with tf.Session() as sess:
tf.global_variables_initializer().run()

[array([ 2.,  2.,  3.],dtype=float32),
array([ 2.,  2.,  3.], dtype=float32),
array([ 5.,  4.,  7.], dtype=float32),
array([ 3.,  2.,  4.], dtype=float32)]

• 可以看出，grad_ys 代表的是 ys 的头梯度

import tensorflow as tf

w1 = tf.Variable(2.0)
w2 = tf.Variable(2.0)

a = tf.multiply(w1, 3.0)

# b=w1*3.0*w2
b = tf.multiply(a_stoped, w2)
#输出


a = tf.Variable(1.0)
b = tf.Variable(1.0)

with tf.Session() as sess:
tf.global_variables_initializer().run()
#输出 [1.0, 1.0]


import tensorflow as tf

w1 = tf.Variable(2.0)
w2 = tf.Variable(2.0)
a = tf.multiply(w1, 3.0)

# b=w1*3.0*w2
b = tf.multiply(a_stoped, w2)

#其它地方都会运行正常，无论是梯度的计算还是变量的更新。总觉着tensorflow这么设计有点不好，
#不如改成流过去的梯度为0

with tf.Session() as sess:
tf.global_variables_initializer().run()
print(sess.run(train_op))
print(sess.run([w1, w2]))


## 高阶导数

tensorflow 求 高阶导数可以使用 tf.gradients 来实现

import tensorflow as tf

with tf.device('/cpu:0'):
a = tf.constant(1.)
b = tf.pow(a, 2)

with tf.Session() as sess:


Note: 有些 op，tf 没有实现其高阶导的计算，例如 tf.add …, 如果计算了一个没有实现 高阶导的 op的高阶导， gradients 会返回 None。

©️2019 CSDN 皮肤主题: 程序猿惹谁了 设计师: 上身试试