public final class EagerSession extends Object implements ExecutionEnvironment, AutoCloseable
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 Graph
s and Session
s.
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.
Modifier and Type | Class and Description |
---|---|
static class |
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.
|
static class |
EagerSession.Options |
static class |
EagerSession.ResourceCleanupStrategy
Controls how TensorFlow resources are cleaned up when they are no longer needed.
|
Modifier and Type | Method and Description |
---|---|
void |
close() |
static EagerSession |
create()
Returns an
EagerSession configured with default options. |
static EagerSession |
getDefault()
Returns the default eager session
|
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. |
public static EagerSession initDefault(EagerSession.Options options)
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
options
- options to use to build default sessionIllegalStateException
- if the default session is already initializedgetDefault()
public static EagerSession getDefault()
Once initialized, the default eager session remains active for the whole life of the
application, as opposed to sessions obtained from create()
or EagerSession.Options.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() explicitly
Ops tf = Ops.create(EagerSession.getDefault());
initDefault(org.tensorflow.EagerSession.Options)
public static EagerSession create()
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.Options options()
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...
}
public void close()
close
in interface AutoCloseable
public OperationBuilder opBuilder(String type, String name)
ExecutionEnvironment
Operation
.opBuilder
in interface ExecutionEnvironment
type
- of the Operation (i.e., identifies the computation to be performed)name
- to refer to the created Operation in this environment scope.OperationBuilder
to create an Operation when OperationBuilder.build()
is invoked. If OperationBuilder.build()
is not invoked,
then some resources may leak.Copyright © 2022. All rights reserved.