Warning: This API is deprecated and will be removed in a future
version of TensorFlow after
the replacement is stable.
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 Constructors
|
|
|
Session( Graph g, byte[] config)
Construct a new session with the associated Graph and configuration options.
|
Public Methods
void
|
close()
Release resources associated with the Session.
|
Session.Runner
|
runner()
Create a Runner to execute graph operations and evaluate Tensors.
|
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
.
public
Session
(Graph g, byte[] 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 serialized 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 (Session.Runner.run()
calls). A Session
is not usable after close returns.
Create a Runner to execute graph operations and evaluate Tensors.
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 2022-02-12 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 2022-02-12 UTC."],[],[],null,["# Session\n\npublic final class **Session** \nDriver for [Graph](/api_docs/java/org/tensorflow/Graph) execution.\n\nA `Session` instance encapsulates the environment in which [Operation](/api_docs/java/org/tensorflow/Operation)s in a\n[Graph](/api_docs/java/org/tensorflow/Graph) are executed to compute [Tensors](/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**WARNING:** A `Session` owns resources that **must** be explicitly freed by\ninvoking [close()](/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](/api_docs/java/org/tensorflow/Session.Run) || Output tensors and metadata obtained when executing a session. |\n| class | [Session.Runner](/api_docs/java/org/tensorflow/Session.Runner) || Run [Operation](/api_docs/java/org/tensorflow/Operation)s and evaluate [Tensors](/api_docs/java/org/tensorflow/Tensor). |\n\n### Public Constructors\n\n|---|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| | [Session](/api_docs/java/org/tensorflow/Session#Session(org.tensorflow.Graph))([Graph](/api_docs/java/org/tensorflow/Graph) g) Construct a new session with the associated [Graph](/api_docs/java/org/tensorflow/Graph). |\n| | [Session](/api_docs/java/org/tensorflow/Session#Session(org.tensorflow.Graph,%20byte[]))([Graph](/api_docs/java/org/tensorflow/Graph) g, byte\\[\\] config) Construct a new session with the associated [Graph](/api_docs/java/org/tensorflow/Graph) and configuration options. |\n\n### Public Methods\n\n|----------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------|\n| void | [close](/api_docs/java/org/tensorflow/Session#close())() Release resources associated with the Session. |\n| [Session.Runner](/api_docs/java/org/tensorflow/Session.Runner) | [runner](/api_docs/java/org/tensorflow/Session#runner())() Create a Runner to execute graph operations and evaluate Tensors. |\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](/api_docs/java/org/tensorflow/Graph) g)\n\nConstruct a new session with the associated [Graph](/api_docs/java/org/tensorflow/Graph). \n\n##### Parameters\n\n| g | |\n|---|---|\n\n#### public\n**Session**\n([Graph](/api_docs/java/org/tensorflow/Graph) g, byte\\[\\] config)\n\nConstruct a new session with the associated [Graph](/api_docs/java/org/tensorflow/Graph) and configuration options. \n\n##### Parameters\n\n| g | The [Graph](/api_docs/java/org/tensorflow/Graph) the created Session will operate on. |\n| config | Configuration parameters for the session specified as a serialized [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 ([Session.Runner.run()](/api_docs/java/org/tensorflow/Session.Runner#run()) calls). A Session\nis not usable after close returns.\n\n\u003cbr /\u003e\n\n#### public [Session.Runner](/api_docs/java/org/tensorflow/Session.Runner)\n**runner**\n()\n\nCreate a Runner to execute graph operations and evaluate Tensors."]]