public final class Session extends Object implements AutoCloseable
Graph
execution.
A Session
instance encapsulates the environment in which Operation
s in a
Graph
are executed to compute Tensors
. For example:
// Let's say graph is an instance of the Graph class
// for the computation y = 3 * x
try (Session s = new Session(graph)) {
try (Tensor x = Tensor.create(2.0f);
Tensor y = s.runner().feed("x", x).fetch("y").run().get(0)) {
System.out.println(y.floatValue()); // Will print 6.0f
}
try (Tensor x = Tensor.create(1.1f);
Tensor y = s.runner().feed("x", x).fetch("y").run().get(0)) {
System.out.println(y.floatValue()); // Will print 3.3f
}
}
WARNING:A Session
owns resources that must be explicitly freed by
invoking close()
.
Instances of a Session are thread-safe.
Modifier and Type | Class and Description |
---|---|
static class |
Session.Run
Output tensors and metadata obtained when executing a session.
|
class |
Session.Runner
|
Constructor and Description |
---|
Session(Graph g)
Construct a new session with the associated
Graph . |
Session(Graph g,
byte[] config)
Construct a new session with the associated
Graph and configuration options. |
Modifier and Type | Method and Description |
---|---|
void |
close()
Release resources associated with the Session.
|
Session.Runner |
runner()
Create a Runner to execute graph operations and evaluate Tensors.
|
public Session(Graph g, byte[] config)
Graph
and configuration options.g
- The Graph
the created Session will operate on.config
- Configuration parameters for the session specified as a serialized ConfigProto
protocol buffer.IllegalArgumentException
- if the config is not a valid serialization of the ConfigProto
protocol buffer.public void close()
Blocks until there are no active executions (Session.Runner.run()
calls). A Session
is not usable after close returns.
close
in interface AutoCloseable
public Session.Runner runner()
Copyright © 2022. All rights reserved.