Session
Stay organized with collections
Save and categorize content based on your preferences.
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.
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
|
run( Op op)
Executes an operation in the graph.
|
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
From class
java.lang.Object
boolean
|
equals(Object arg0)
|
final
Class<?>
|
getClass()
|
int
|
hashCode()
|
final
void
|
notify()
|
final
void
|
notifyAll()
|
String
|
toString()
|
final
void
|
wait(long arg0, int arg1)
|
final
void
|
wait(long arg0)
|
final
void
|
wait()
|
From interface
java.lang.AutoCloseable
Public Constructors
public
Session
(Graph g)
Construct a new session with the associated Graph
.
Parameters
g |
The Graph the created Session will operate on.
|
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()
.
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())
.
Create a Runner to execute graph operations and evaluate Tensors.
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
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2021-11-29 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2021-11-29 UTC."],[],[],null,["# Session\n\npublic final class **Session** \nDriver for [Graph](/jvm/api_docs/java/org/tensorflow/Graph) execution.\n\nA `Session` instance encapsulates the environment in which [Operation](/jvm/api_docs/java/org/tensorflow/Operation)s in a\n[Graph](/jvm/api_docs/java/org/tensorflow/Graph) are executed to compute [Tensors](/jvm/api_docs/java/org/tensorflow/Tensor). For example:\n\n // Let's say graph is an instance of the Graph class\n // for the computation y = 3 * x\n\n try (Session s = new Session(graph)) {\n try (Tensor x = Tensor.create(2.0f);\n Tensor y = s.runner().feed(\"x\", x).fetch(\"y\").run().get(0)) {\n System.out.println(y.floatValue()); // Will print 6.0f\n \n try (Tensor x = Tensor.create(1.1f);\n Tensor y = s.runner().feed(\"x\", x).fetch(\"y\").run().get(0)) {\n System.out.println(y.floatValue()); // Will print 3.3f\n }\n }\n }\n\n**WARNING:** A `Session` owns resources that **must** be explicitly freed by\ninvoking [close()](/jvm/api_docs/java/org/tensorflow/Session#close()).\n\nInstances of a Session are thread-safe.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n### Nested Classes\n\n|-------|---|---|---------------------------------------------------------------------------------------------------------------------------------|\n| class | [Session.Run](/jvm/api_docs/java/org/tensorflow/Session.Run) || Output tensors and metadata obtained when executing a session. |\n| class | [Session.Runner](/jvm/api_docs/java/org/tensorflow/Session.Runner) || Run [Operation](/jvm/api_docs/java/org/tensorflow/Operation)s and evaluate [Tensors](/jvm/api_docs/java/org/tensorflow/Tensor). |\n\n### Public Constructors\n\n|---|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| | [Session](/jvm/api_docs/java/org/tensorflow/Session#Session(org.tensorflow.Graph))([Graph](/jvm/api_docs/java/org/tensorflow/Graph) g) Construct a new session with the associated [Graph](/jvm/api_docs/java/org/tensorflow/Graph). |\n| | [Session](/jvm/api_docs/java/org/tensorflow/Session#Session(org.tensorflow.Graph, org.tensorflow.proto.framework.ConfigProto))([Graph](/jvm/api_docs/java/org/tensorflow/Graph) g, [ConfigProto](/jvm/api_docs/java/org/tensorflow/proto/framework/ConfigProto) config) Construct a new session with the associated [Graph](/jvm/api_docs/java/org/tensorflow/Graph) and configuration options. |\n\n### Public Methods\n\n|--------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| void | [close](/jvm/api_docs/java/org/tensorflow/Session#close())() Release resources associated with the Session. |\n| void | [restore](/jvm/api_docs/java/org/tensorflow/Session#restore(java.lang.String))(String prefix) Restore the actual state of the variables of this session's graph. |\n| void | [run](/jvm/api_docs/java/org/tensorflow/Session#run(org.tensorflow.op.Op))([Op](/jvm/api_docs/java/org/tensorflow/op/Op) op) Executes an operation in the graph. |\n| void | [run](/jvm/api_docs/java/org/tensorflow/Session#run(java.lang.String))(String opName) Executes an operation in the graph with the given name. |\n| void | [runInit](/jvm/api_docs/java/org/tensorflow/Session#runInit())() Execute the graph's initializers. |\n| [Session.Runner](/jvm/api_docs/java/org/tensorflow/Session.Runner) | [runner](/jvm/api_docs/java/org/tensorflow/Session#runner())() Create a Runner to execute graph operations and evaluate Tensors. |\n| void | [save](/jvm/api_docs/java/org/tensorflow/Session#save(java.lang.String))(String prefix) Saves the actual state of the variables of this session's graph. |\n\n### Inherited Methods\n\nFrom class java.lang.Object \n\n|------------------|---------------------------|\n| boolean | equals(Object arg0) |\n| final Class\\\u003c?\\\u003e | getClass() |\n| int | hashCode() |\n| final void | notify() |\n| final void | notifyAll() |\n| String | toString() |\n| final void | wait(long arg0, int arg1) |\n| final void | wait(long arg0) |\n| final void | wait() |\n\nFrom interface java.lang.AutoCloseable \n\n|---------------|---------|\n| abstract void | close() |\n\nPublic Constructors\n-------------------\n\n#### public\n**Session**\n([Graph](/jvm/api_docs/java/org/tensorflow/Graph) g)\n\nConstruct a new session with the associated [Graph](/jvm/api_docs/java/org/tensorflow/Graph). \n\n##### Parameters\n\n| g | The [Graph](/jvm/api_docs/java/org/tensorflow/Graph) the created Session will operate on. |\n|---|-------------------------------------------------------------------------------------------|\n\n#### public\n**Session**\n([Graph](/jvm/api_docs/java/org/tensorflow/Graph) g, [ConfigProto](/jvm/api_docs/java/org/tensorflow/proto/framework/ConfigProto) config)\n\nConstruct a new session with the associated [Graph](/jvm/api_docs/java/org/tensorflow/Graph) and configuration options. \n\n##### Parameters\n\n| g | The [Graph](/jvm/api_docs/java/org/tensorflow/Graph) the created Session will operate on. |\n| config | Configuration parameters for the session specified as a [ConfigProto](https://www.tensorflow.org/code/tensorflow/core/protobuf/config.proto) protocol buffer. |\n|--------|---------------------------------------------------------------------------------------------------------------------------------------------------------------|\n\n##### Throws\n\n| IllegalArgumentException | if the config is not a valid serialization of the ConfigProto protocol buffer. |\n|--------------------------|--------------------------------------------------------------------------------|\n\nPublic Methods\n--------------\n\n#### public void\n**close**\n()\n\nRelease resources associated with the Session.\n\nBlocks until there are no active executions ([run()](/jvm/api_docs/java/org/tensorflow/Session.Runner#run()) calls). A Session\nis not usable after close returns.\n\n\u003cbr /\u003e\n\n#### public void\n**restore**\n(String prefix)\n\nRestore the actual state of the variables of this session's graph.\n\n`prefix` is the path where the files containing the variables state live,\nfollowed by the filename prefix. For example, if `prefix` is set to\n*mymodel/myvariables/variables* , then the files are loaded from\n*mymodel/myvariables* and named *variables.data-\\*-of-\\**\n\nNote that this method might alter the underlying graph if it is the first time that one\nof its sessions is saved, see [ERROR(/Graph#saverDef())]() for more details.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n##### Parameters\n\n| prefix | prefix to restore from |\n|--------|------------------------|\n\n#### public void\n**run**\n([Op](/jvm/api_docs/java/org/tensorflow/op/Op) op)\n\nExecutes an operation in the graph.\n\nThis method is equivalent to `session.runner().addTarget(op).run()`.\n\n\u003cbr /\u003e\n\n##### Parameters\n\n| op | the operation to run. |\n|----|-----------------------|\n\n#### public void\n**run**\n(String opName)\n\nExecutes an operation in the graph with the given name.\n\nThis method is equivalent to `session.runner().addTarget(opName).run()`.\n\n\u003cbr /\u003e\n\n##### Parameters\n\n| opName | name of the operation to run. |\n|--------|-------------------------------|\n\n##### Throws\n\n| IllegalArgumentException | if no operation of that name can be found in the graph |\n|--------------------------|--------------------------------------------------------|\n\n#### public void\n**runInit**\n()\n\nExecute the graph's initializers.\n\nThis method is equivalent to `session.run(Ops.create(session.graph).init())`.\n\n\u003cbr /\u003e\n\n#### public [Session.Runner](/jvm/api_docs/java/org/tensorflow/Session.Runner)\n**runner**\n()\n\nCreate a Runner to execute graph operations and evaluate Tensors. \n\n#### public void\n**save**\n(String prefix)\n\nSaves the actual state of the variables of this session's graph.\n\n`prefix` is a path where the files containing the variables state will be saved,\nfollowed by a prefix for naming these files. For example, if `prefix` is set to\n*mymodel/myvariables/variables* , then the generated files will be located under\n*mymodel/myvariables* and named *variables.data-\\*-of-\\**\n\nNote that this method might alter the underlying graph if it is the first time that one\nof its sessions is saved, see [ERROR(/Graph#saverDef())]() for more details.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n##### Parameters\n\n| prefix | prefix to the variable files to save |\n|--------|--------------------------------------|"]]