# Module: tf_agents.utils.eager_utils

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/agents/blob/v0.19.0/tf_agents/utils/eager_utils.py) |

Common utilities for TF-Agents.

#### Example of usage:

    from tf_agents.utils import eager_utils

    @eager_utils.run_in_graph_and_eager_modes
    def loss_fn(x, y):
      v = tf.get_variable('v', initializer=tf.ones_initializer(), shape=())
      return v + x - y

    with tfe.graph_mode():
      # loss and train_step are Tensors/Ops in the graph
      loss_op = loss_fn(inputs, labels)
      train_step_op = eager_utils.create_train_step(loss_op, optimizer)
      # Compute the loss and apply gradients to the variables using the optimizer.
      with tf.Session() as sess:
        sess.run(tf.compat.v1.global_variables_initializer())
        for _ in range(num_train_steps):
          loss_value = sess.run(train_step_op)

    with tfe.eager_mode():
      # loss and train_step are lambda functions that can be called.
      loss = loss_fn(inputs, labels)
      train_step = eager_utils.create_train_step(loss, optimizer)
      # Compute the loss and apply gradients to the variables using the optimizer.
      for _ in range(num_train_steps):
        loss_value = train_step()

## Classes

[`class Future`](https://www.tensorflow.org/agents/api_docs/python/tf_agents/utils/eager_utils/Future): Converts a function or class method call into a future callable.

## Functions

[`add_gradients_summaries(...)`](https://www.tensorflow.org/agents/api_docs/python/tf_agents/utils/eager_utils/add_gradients_summaries): Add summaries to gradients.

[`add_variables_summaries(...)`](https://www.tensorflow.org/agents/api_docs/python/tf_agents/utils/eager_utils/add_variables_summaries): Add summaries for variables.

[`clip_gradient_norms(...)`](https://www.tensorflow.org/agents/api_docs/python/tf_agents/utils/eager_utils/clip_gradient_norms): Clips the gradients by the given value.

[`clip_gradient_norms_fn(...)`](https://www.tensorflow.org/agents/api_docs/python/tf_agents/utils/eager_utils/clip_gradient_norms_fn): Returns a `transform_grads_fn` function for gradient clipping.

[`create_train_op(...)`](https://www.tensorflow.org/agents/api_docs/python/tf_agents/utils/eager_utils/create_train_op): Creates an `Operation` that evaluates the gradients and returns the loss.

[`create_train_step(...)`](https://www.tensorflow.org/agents/api_docs/python/tf_agents/utils/eager_utils/create_train_step): Creates a train_step that evaluates the gradients and returns the loss.

[`dataset_iterator(...)`](https://www.tensorflow.org/agents/api_docs/python/tf_agents/utils/eager_utils/dataset_iterator): Constructs a `Dataset` iterator.

[`future_in_eager_mode(...)`](https://www.tensorflow.org/agents/api_docs/python/tf_agents/utils/eager_utils/future_in_eager_mode): Decorator that allow a function/method to run in graph and in eager modes.

[`get_next(...)`](https://www.tensorflow.org/agents/api_docs/python/tf_agents/utils/eager_utils/get_next): Returns the next element in a `Dataset` iterator.

[`has_self_cls_arg(...)`](https://www.tensorflow.org/agents/api_docs/python/tf_agents/utils/eager_utils/has_self_cls_arg): Checks if it is method which takes self/cls as the first argument.

[`is_unbound(...)`](https://www.tensorflow.org/agents/api_docs/python/tf_agents/utils/eager_utils/is_unbound): Checks if it is an unbounded method.

[`np_function(...)`](https://www.tensorflow.org/agents/api_docs/python/tf_agents/utils/eager_utils/np_function): Decorator that allow a numpy function to be used in Eager and Graph modes.

<br />

<br />

<br />

| ## Other Members ||
|---|---|
| absolute_import | Instance of `__future__._Feature` |
| division | Instance of `__future__._Feature` |
| print_function | Instance of `__future__._Feature` |

<br />