user3180
user3180

Reputation: 1487

Gradient erroneously returns none

print("\ncomputed 1")
x=tfe.Variable(np.random.uniform(size=[166,]), name='x', dtype=tf.float64)
print(compute_cost(tf.constant(normed_data[:10], dtype=tf.float64), x))

print("\ncomputed 2")
x=tfe.Variable(np.random.uniform(size=[166,]), name='x', dtype=tf.float64)
print(compute_cost(tf.constant(normed_data[:10], dtype=tf.float64), x))

print("\ncomputed 0")
x=tfe.Variable(np.zeros((166,)), name='x', dtype=tf.float64)
print(compute_cost(tf.constant(normed_data[:10], dtype=tf.float64), x))

print("\ncomputed 0")
x=tfe.Variable(np.zeros((166,)), name='x', dtype=tf.float64)
print(compute_cost(tf.constant(normed_data[:10], dtype=tf.float64), x))

Output:

computed 1
tf.Tensor(0.00627350861776064, shape=(), dtype=float64)

computed 2
tf.Tensor(0.0032756996843633264, shape=(), dtype=float64)

computed 0
tf.Tensor(19.318829784931626, shape=(), dtype=float64)

computed 0
tf.Tensor(19.318829784931626, shape=(), dtype=float64)

Clearly, the tfe.Variable x is affecting the loss function, so the gradient should not be None/0. Yet it is...

x=tfe.Variable(np.random.uniform(size=[166,]), name='x', dtype=tf.float64)
f_grad = tfe.gradients_function(lambda s: compute_cost(tf.constant(normed_data[:10], dtype=tf.float64), s), params=['s'])
print("\nGradient " + str(f_grad(x)))

Output:

Gradient [None]

Can someone explain what's wrong? Using Tensorflow 1.10.1 with eager execution.

Upvotes: 0

Views: 113

Answers (1)

ash
ash

Reputation: 6751

A None return value signifies that there is no dependency between the output of the function and the variable.

Looking at the compute_cost function you linked to, I see a numpy operation (np.log) in there, which would break the "trace" of differentiable operations. Note that TensorFlow can only compute gradients through TensorFlow operations.

Similar to your other question, replacing the numpy operation with the corresponding TensorFlow one (tf.log) should do the trick.

Hope that helps.

Upvotes: 1

Related Questions