j35t3r
j35t3r

Reputation: 1533

How to use `tf.gradients`? `TypeError: Fetch argument None has invalid type <type 'NoneType'>`

I am getting this error: TypeError: Fetch argument None has invalid type <type 'NoneType'>

I want to calculate the gradient of the 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))

If I calculate the gradient of m_leftOps2, I should get as result m_rightOps2.

Upvotes: 0

Views: 1025

Answers (1)

Peter Szoldan
Peter Szoldan

Reputation: 4868

tf.gradients() calculates the derivative of ys with respect to xs. So you have your arguments backwards. Try this:

t_gradients = tf.gradients( ys = loss, xs = m_leftOps2 )

Upvotes: 1

Related Questions