文章标题

来源:互联网 发布:淘宝客服专用语言 编辑:程序博客网 时间:2024/06/02 03:58

tf.gradient

tensorflow中有一个计算梯度的函数tf.gradients(ys, xs),要注意的是,xs中的x必须要与ys相关,不相关的话,会报错。
代码中定义了两个变量w1, w2, 但res只与w1相关

import tensorflow as tfw1 = 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=2b=3sess.run(tf.select(True,a,b))#outuput 2sess.run(tf.select(False,a,b))#output 3
0 0
原创粉丝点击