Keras: Bug: ValueError: An operation has `None` for gradient.

Created on 13 Jun 2019  路  5Comments  路  Source: keras-team/keras

Please make sure that this is a Bug or a Feature Request and provide all applicable information asked by the template.
If your issue is an implementation question, please ask your question on StackOverflow or on the Keras Slack channel instead of opening a GitHub issue.

System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Linux Ubuntu 18.04
  • TensorFlow backend (yes / no): yes
  • TensorFlow version: 1.12.0
  • Keras version: 2.2.4
  • Python version: 3.6
  • CUDA/cuDNN version: 9.2
  • GPU model and memory: nVidia GTX360

Describe the current behavior
When I call train_on_batch on my model, i receive the error:

sup_loss = self.supervised_phase.train_on_batch(X, label)
  File "/home/xxx/anaconda3/envs/mygan/lib/python3.6/site-packages/keras/engine/training.py", line 1216, in train_on_batch
    self._make_train_function()
  File "/home/xxx/anaconda3/envs/mygan/lib/python3.6/site-packages/keras/engine/training.py", line 509, in _make_train_function
    loss=self.total_loss)
  File "/home/xxx/anaconda3/envs/mygan/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/home/xxx/anaconda3/envs/mygan/lib/python3.6/site-packages/keras/optimizers.py", line 475, in get_updates
    grads = self.get_gradients(loss, params)
  File "/home/xxx/anaconda3/envs/mygan/lib/python3.6/site-packages/keras/optimizers.py", line 91, in get_gradients
    raise ValueError('An operation has `None` for gradient. '
ValueError: An operation has `None` for gradient. Please make sure that all of your ops have a gradient defined (i.e. are differentiable). Common ops without gradient: K.argmax, K.round, K.eval.

Describe the expected behavior
I expect the training to proceed with no errors.

Code to reproduce the issue

X = Input(shape=(self.phrase_size, self.n_cropped_notes, self.n_tracks), name="X")

s_pred, _  = self.encoder(X)

self.supervised_phase = Model(
            inputs=X,
            outputs=s_pred,
            name="supervised_phase"
        )
        self.supervised_phase.compile(
            loss="binary_crossentropy",
            loss_weights=[self.supervised_weight],
            optimizer=self.aae_optim,
            metrics=["binary_accuracy"]
        )

sup_loss = self.supervised_phase.train_on_batch(X, label)

Other info
I'm sure that self.encoder only contains differentiable operations, because I use it as part of other subgraphs that I can train without problems, I only get the error while trying to train self.supervised_phase.

tensorflow support

Most helpful comment

I'm facing the same issue. But it also happens when I add TensorBoard callback and ask to record histograms, grads and graphs. If I remove histograms and grads, it works fine.

My environment:

  • Ubuntu 18.04
  • TensorFlow version: 1.13.1
  • Keras version: 2.2.4
  • Python version: 3.6.8
  • CUDA/cuDNN version: 10.0

Stack trace:

/usr/local/lib/python3.6/dist-packages/keras/callbacks.py in set_model(self, model)
    797                     if self.write_grads:
    798                         grads = model.optimizer.get_gradients(model.total_loss,
--> 799                                                               weight)
    800 
    801                         def is_indexed_slices(grad):

/usr/local/lib/python3.6/dist-packages/keras/optimizers.py in get_gradients(self, loss, params)
     89         grads = K.gradients(loss, params)
     90         if None in grads:
---> 91             raise ValueError('An operation has `None` for gradient. '
     92                              'Please make sure that all of your ops have a '
     93                              'gradient defined (i.e. are differentiable). '

I'm inspecting my model and will come back with more details soon.

All 5 comments

See same thing on Keras 2.2.4 with TF 1.13.1. Downgrading to Keras 2.2.0 and TF 1.8.0 my code works.

I'm facing the same issue. But it also happens when I add TensorBoard callback and ask to record histograms, grads and graphs. If I remove histograms and grads, it works fine.

My environment:

  • Ubuntu 18.04
  • TensorFlow version: 1.13.1
  • Keras version: 2.2.4
  • Python version: 3.6.8
  • CUDA/cuDNN version: 10.0

Stack trace:

/usr/local/lib/python3.6/dist-packages/keras/callbacks.py in set_model(self, model)
    797                     if self.write_grads:
    798                         grads = model.optimizer.get_gradients(model.total_loss,
--> 799                                                               weight)
    800 
    801                         def is_indexed_slices(grad):

/usr/local/lib/python3.6/dist-packages/keras/optimizers.py in get_gradients(self, loss, params)
     89         grads = K.gradients(loss, params)
     90         if None in grads:
---> 91             raise ValueError('An operation has `None` for gradient. '
     92                              'Please make sure that all of your ops have a '
     93                              'gradient defined (i.e. are differentiable). '

I'm inspecting my model and will come back with more details soon.

@wilderrodrigues Hi there, did you manage to find a solution in the end?
I'm having the same issue when trying to get gradient values for interpretablity of the model.
my environment:

  • CentOS Linux 7 (Core)
  • Nvidia Driver Version: 418.87.00 CUDA Version: 10.1
  • Python 3.7.4
  • Tensorflow 2.0.0
  • Keras 2.3.1

Any ideas?

@wilderrodrigues Hi there, did you find any solution.
For some architecture my code is working but when I am using Dense layer I have encountered same problem.

@wilderrodrigues @sepidehsaran @alokssingh @Andrea-V
Did any of you find the reason for this issue ?

https://github.com/keras-team/keras/issues/12521 the suggestion is to look for unused weights. My model has 1.8 million parameters.

Do you know of any method to check for unused weights ?

Was this page helpful?
0 / 5 - 0 ratings