如何使用`tf.gradients`? `TypeError: Fetch argument None has invalid type <type 'NoneType'>`
Posted
技术标签:
【中文标题】如何使用`tf.gradients`? `TypeError: Fetch argument None has invalid type <type \'NoneType\'>`【英文标题】:How to use `tf.gradients`? `TypeError: Fetch argument None has invalid type <type 'NoneType'>`如何使用`tf.gradients`? `TypeError: Fetch argument None has invalid type <type 'NoneType'>` 【发布时间】:2018-04-30 09:52:28 【问题描述】:我收到此错误:TypeError: Fetch argument None has invalid type <type 'NoneType'>
我想计算loss
w.r.t 的梯度。 m_leftops2
:
t_im0 = tf.placeholder(tf.float32, [None, None, None, None], name='left_img')
t_im1 = tf.placeholder(tf.float32, [None, None, None, None], name='right_img')
strides=[1,1,1,1]
m_leftOps2 = tf.tanh(tf.nn.conv2d(t_im0, w1, strides=strides, padding=padding, data_format="NCHW")+b)
m_rightOps2 = tf.tanh(tf.nn.conv2d(t_im1, w1, strides=strides, padding=padding, data_format="NCHW")+b)
loss = tf.reduce_sum(m_leftOps2 * m_rightOps2)
t_gradients = tf.gradients(xs=loss, ys=[m_leftOps2])
with tf.Session(config=config) as sess:
sess.run(tf.global_variables_initializer())
feed_dict = t_im0: normalized_i1, t_im1: normalized_i2
print("gradients: ", sess.run([loss, t_gradients], feed_dict=feed_dict))
如果我计算m_leftOps2
的梯度,我应该得到m_rightOps2
的结果。
【问题讨论】:
【参考方案1】:tf.gradients()
计算 ys 关于 xs 的导数。所以你的论点倒退了。试试这个:
t_gradients = tf.gradients( ys = loss, xs = m_leftOps2 )
【讨论】:
以上是关于如何使用`tf.gradients`? `TypeError: Fetch argument None has invalid type <type 'NoneType'>`的主要内容,如果未能解决你的问题,请参考以下文章
tensorflow中gradients的使用以及TypeError: Fetch argument None has invalid type <class 'NoneType'