Skip to content

Issue with _create_slots: var_list[0].device empty #333

Closed

Description

@malleus17

I have problem running

loss = crossentropy(activation, y)
train_loss = tf.train.AdamOptimizer(learning_rate=0.01).minimize(loss)

with crossentropy defined in another file as a simple function that return

def crossentropy(activation, y)
    return tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(activation, y))

I got this error:

Traceback (most recent call last):
  File "test/test.py", line 66, in <module>
    train_loss = tf.train.AdamOptimizer(learning_rate=0.01).minimize(loss) # Adam Optimizer
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/training/optimizer.py", line 167, in minimize
    name=name)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/training/optimizer.py", line 245, in apply_gradients
    self._create_slots([v for g, v in grads_and_vars if g is not None])
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/training/adam.py", line 87, in _create_slots
    with ops.device(var_list[0].device):
IndexError: list index out of range

But this is working: (only call function from another file is not)

loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(activation, y))
train_loss  = self.optimizer(learning_rate=0.01).minimize(loss)

It seemed I can't define that expression outside the main thread, I also tried using same graph as default graph, but it doesn't seem to work either. I think this is an issue with graph building.
If you ever ran into this problem, thanks for helping!

Activity

  1. touts commented on Nov 25, 2015

    @touts

    The error indicates that the optimizer could not find any gradients for the variables to optimize.

    This can be caused by several problems:
    1- The optimizer could not find any variable to optimize.
    2- The loss does not depend on any variable.

    For 1-: Optimizers look for trainable variables in the default graph. They use tf.trainable_variables() as the list of variables to optimize. So try calling that function and print its output before calling .minimize(). If you get an empty list then the optimizer won't find variables either.

    Why can't it find variables?

    For 2-: The optimizer computes gradients of the loss with respect to variables. If there are no variables there are no gradients and nothing to optimize. For example, if you pass a constant to minimize() nothing can be done. Now, the fact that you say it works when called from the same file seems to rule out that possibility, but check anyway.

    Finally: The error message clearly needs to be improved.

  2. vrv commented on Dec 23, 2015

    @vrv

    Closing due to staleness, I think we committed a change a few weeks ago that fixed the error message to be better. Let us know if that's not true!

  3. added a commit that references this issue on Jun 20, 2017
  4. added a commit that references this issue on May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions