Driver for 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.
Nested Classes
class | Session.Run | Output tensors and metadata obtained when executing a session. | |
class | Session.Runner | Run Operation s and evaluate Tensors . |
Public Constructors
Session(Graph g, ConfigProto config)
Construct a new session with the associated
Graph and configuration options. |
Public Methods
void |
close()
Release resources associated with the Session.
|
void |
restore(String prefix)
Restore the actual state of the variables of this session's graph.
|
void | |
void |
run(String opName)
Executes an operation in the graph with the given name.
|
void |
runInit()
Execute the graph's initializers.
|
Session.Runner |
runner()
Create a Runner to execute graph operations and evaluate Tensors.
|
void |
save(String prefix)
Saves the actual state of the variables of this session's graph.
|
Inherited Methods
Public Constructors
public Session (Graph g)
public Session (Graph g, ConfigProto config)
Construct a new session with the associated Graph
and configuration options.
Parameters
g | The Graph the created Session will operate on. |
---|---|
config | Configuration parameters for the session specified as a ConfigProto protocol buffer. |
Throws
IllegalArgumentException | if the config is not a valid serialization of the ConfigProto protocol buffer. |
---|
Public Methods
public void close ()
Release resources associated with the Session.
Blocks until there are no active executions (run()
calls). A Session
is not usable after close returns.
public void restore (String prefix)
Restore the actual state of the variables of this session's graph.
prefix
is the path where the files containing the variables state live,
followed by the filename prefix. For example, if prefix
is set to
mymodel/myvariables/variables, then the files are loaded from
mymodel/myvariables and named variables.data-*-of-*
Note that this method might alter the underlying graph if it is the first time that one
of its sessions is saved, see ERROR(/Graph#saverDef())
for more details.
Parameters
prefix | prefix to restore from |
---|
public void run (Op op)
Executes an operation in the graph.
This method is equivalent to session.runner().addTarget(op).run()
.
Parameters
op | the operation to run. |
---|
public void run (String opName)
Executes an operation in the graph with the given name.
This method is equivalent to session.runner().addTarget(opName).run()
.
Parameters
opName | name of the operation to run. |
---|
Throws
IllegalArgumentException | if no operation of that name can be found in the graph |
---|
public void runInit ()
Execute the graph's initializers.
This method is equivalent to session.run(Ops.create(session.graph).init())
.
public void save (String prefix)
Saves the actual state of the variables of this session's graph.
prefix
is a path where the files containing the variables state will be saved,
followed by a prefix for naming these files. For example, if prefix
is set to
mymodel/myvariables/variables, then the generated files will be located under
mymodel/myvariables and named variables.data-*-of-*
Note that this method might alter the underlying graph if it is the first time that one
of its sessions is saved, see ERROR(/Graph#saverDef())
for more details.
Parameters
prefix | prefix to the variable files to save |
---|