tf.gradient
tensorflow中有一个计算梯度的函数tf.gradients(ys, xs),要注意的是,xs中的x必须要与ys相关,不相关的话,会报错。
代码中定义了两个变量w1, w2, 但res只与w1相关
import tensorflow as tf
w1 = tf.Variable([[1,2]])
w2 = tf.Variable([[3,4]])
res = tf.matmul(w1, [[2],[1]])
grads = tf.gradients(res,[w1,w2])
with tf.Session() as sess:
tf.global_variables_initializer().run()
re = sess.run(grads)
print(re)
错误信息
TypeError: Fetch argument None has invalid type
tf.select(condition, t, e, name=None)
https://www.tensorflow.org/versions/r0.10/api_docs/python/control_flow_ops/comparison_operators#select
Selects elements from t or e, depending on condition.
a=2
b=3
sess.run(tf.select(True,a,b))
#outuput 2
sess.run(tf.select(False,a,b))
#output 3