Tensor gradients

I am trying to adapt tf DeepDream code to work with another model. Right now when I call tf.gradients ():

t_grad = tf.gradients(t_score, t_input)[0]
g      = sess.run(t_grad, {t_input:img0})

I get an error like:

TypeError: Fetch argument None of None has invalid type <type 'NoneType'>,     
must be a string or Tensor. (Can not convert a NoneType into a Tensor or 
Operation.)

Where should I start looking for a fix for this error?

Can tf.gradients () be used with a model that has an Optimizer?

+4
source share
2 answers

I assume yours t_gradhas several Nones. Nonemathematically equivalent to a gradient of 0, but returns for the special case where the cost is independent of the argument that it differentiates. There are various reasons why we do not just return 0 instead None, which you can see in the discussion here.

None , , ,

def replace_none_with_zero(l):
  return [0 if i==None else i for i in l] 
+5

tf.gradients()

:

grads = tf.gradients(<a tensor>, <another tensor that doesn't depend on the first>)

, tf.gradients , , , print

print grads

[None] None .

:

results = sess.run(grads) 

None, , .

:

grads = tf.gradients(<a tensor>, <a related tensor>)
print grads 

- :

Tensor("gradients_1/sub_grad/Reshape:0", dtype=float32)

:

results = sess.run(grads, {<appropriate feeds>})
print results

-

[array([[ 4.97156498e-06, 7.87349381e-06, 9.25197037e-06, ..., 8.72526925e-06, 6.78442757e-06, 3.85240173e-06], [ 7.72772819e-06, 9.26370740e-06, 1.19129227e-05, ..., 1.27088233e-05, 8.76379818e-06, 6.00637532e-06], [ 9.46506498e-06, 1.10620931e-05, 1.43903117e-05, ..., 1.40718612e-05, 1.08670165e-05, 7.12365863e-06], ..., [ 1.03536004e-05, 1.03090524e-05, 1.32107480e-05, ..., 1.40605653e-05, 1.25974075e-05, 8.90011415e-06], [ 9.69486427e-06, 8.18045282e-06, 1.12702282e-05, ..., 1.32554378e-05, 1.13317501e-05, 7.74569162e-06], [ 5.61043908e-06, 4.93397192e-06, 6.33513537e-06, ..., 6.26539259e-06, 4.52598442e-06, 4.10689108e-06]], dtype=float32)]

+2

All Articles