Missed TensorFlow World? Check out the recap. Learn more

EagerSession

public final class EagerSession

An environment for executing TensorFlow operations eagerly.

Eager execution is an imperative programming environment that evaluates operations immediately, without building graphs. Operations return concrete values instead of constructing a computational graph to run later, as with Graphs and Sessions.

This makes it easy to develop with TensorFlow and debug models, as it behaves more like a standard programming library.

Instances of a EagerSession are thread-safe.

Nested Classes

enum EagerSession.DevicePlacementPolicy Controls how to act when we try to run an operation on a given device but some input tensors are not on that device. 
class EagerSession.Options  
enum EagerSession.ResourceCleanupStrategy Controls how TensorFlow resources are cleaned up when they are no longer needed. 

Public Methods

synchronized void
close()
static EagerSession
create()
Returns an EagerSession configured with default options.
static EagerSession
getDefault()
Returns the default eager session

Once initialized, the default eager session remains active for the whole life of the application, as opposed to sessions obtained from create() or build() which should be closed after their usage.

static EagerSession
initDefault(EagerSession.Options options)
Initializes the default eager session, which remains active for the lifetime of the application.
OperationBuilder
opBuilder(String type, String name)
Returns a builder to create a new Operation.
static EagerSession.Options
options()
Returns an object that configures and builds a EagerSession with custom options.

Inherited Methods

Public Methods

public synchronized void close ()

public static EagerSession create ()

Returns an EagerSession configured with default options.

WARNING:Instances of EagerSession returned by this method must be explicitly freed by invoking close() when they are no longer needed. This could be achieve using the `try-with-resources` technique.

Example usage:

try (EagerSession session = EagerSession.create()) {
   Ops tf = Ops.create(session);
   // build execute operations eagerly...
 }
 

public static EagerSession getDefault ()

Returns the default eager session

Once initialized, the default eager session remains active for the whole life of the application, as opposed to sessions obtained from create() or build() which should be closed after their usage.

The default set of EagerSession.Options is used to initialize the session on the first call. To override this behavior, it is possible to invoke initDefault(Options) with a different set of options prior to this first call.

Example usage:

// Starting to build eager operations using default session, by calling
 // EagerSession.getDefault() implicitly
 Ops tf = Ops.create();

 // Starting to build eager operations using default session, by calling
 // EagerSession.getDefault() explictly
 Ops tf = Ops.create(EagerSession.getDefault());
 

Returns
  • default eager session

public static EagerSession initDefault (EagerSession.Options options)

Initializes the default eager session, which remains active for the lifetime of the application.

This method is implicitly invoked on the first call to getDefault(), but can also be invoked explicitly to override default options.

Note that calling this method more than once will throw an IllegalArgumentException as the default session cannot be modified once it has been created. Therefore, it is important to explicitly initialize it before getDefault() is invoked for the first time from any thread.

Example usage:

// Initializing default session to override default options is valid but
 // is optional
 EagerSession.initDefault(EagerSession.options().async(true));

 // Starting to build eager operations using default session, by calling
 // EagerSession.getDefault() implicitly
 Ops tf = Ops.create();

 // Initializing default session more than once or after using it is not
 // permitted and throws an exception
 EagerSession.initDefault(EagerSession.options().async(true));  // throws
 

Parameters
options options to use to build default session
Returns
  • default eager session
Throws
IllegalStateException if the default session is already initialized

public OperationBuilder opBuilder (String type, String name)

Returns a builder to create a new Operation.

Parameters
type of the Operation (i.e., identifies the computation to be performed)
name to refer to the created Operation in this environment scope.
Returns

public static EagerSession.Options options ()

Returns an object that configures and builds a EagerSession with custom options.

WARNING:Instances of EagerSession returned by this method must be explicitly freed by invoking close() when they are no longer needed. This could be achieve using the `try-with-resources` technique.

Example usage:

try (EagerSession session = EagerSession.options().async(true).build()) {
   Ops tf = Ops.create(session);
   // build execute operations eagerly and asynchronously...
 }