Class: Tensorflow::Session
- Inherits:
-
Object
- Object
- Tensorflow::Session
- Defined in:
- lib/tensorflow/session.rb
Overview
A class for running TensorFlow operations. A Session object encapsulates the environment in which Operation objects are executed, and Tensor objects are evaluated. A Session instance lets a caller drive a TensorFlow graph computation. When a Session is created with a given target, a new Session object is bound to the universe of resources specified by that target. Those resources are available to this session to perform computation described in the GraphDef. After extending the session with a graph, the caller uses the Run() API to perform the computation and potentially fetch outputs as Tensors. Protocol buffer exposes various configuration options for a session. The Op definations are stored in ops.pb file. Official documentation of session and Operation.
Instance Attribute Summary collapse
-
#c ⇒ Object
Returns the value of attribute c.
-
#dimensions ⇒ Object
Create a success status.
-
#graph ⇒ Object
A TensorFlow graph is a description of computations.
-
#ops ⇒ Object
Nodes in the graph are called ops (short for operations).
-
#session ⇒ Object
Returns the value of attribute session.
-
#status ⇒ Object
Returns the value of attribute status.
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(graph, c_options = nil) ⇒ Session
constructor
A TensorFlow graph is a description of computations.
-
#run(inputs, outputs, targets) ⇒ Object
Run the graph with the associated session starting with the supplied feeds to compute the value of the requested fetches.
Constructor Details
#initialize(graph, c_options = nil) ⇒ Session
A TensorFlow graph is a description of computations. To compute anything, a graph must be launched in a Session. A Session places the graph ops and provides methods to execute them.
19 20 21 22 23 24 25 26 27 |
# File 'lib/tensorflow/session.rb', line 19 def initialize(graph, = nil) = Tensorflow::Session_options.new if .nil? self.status = Tensorflow::Status.new cOpt = .c c_session = Tensorflow::TF_NewSession(graph.c, cOpt, status.c) Tensorflow::TF_DeleteSessionOptions(cOpt) raise 'Error in session initialization.' if status.code != 0 self.c = c_session end |
Instance Attribute Details
#c ⇒ Object
Returns the value of attribute c.
11 12 13 |
# File 'lib/tensorflow/session.rb', line 11 def c @c end |
#dimensions ⇒ Object
Create a success status.
19 20 21 22 23 24 25 26 27 |
# File 'lib/tensorflow/session.rb', line 19 def initialize(graph, = nil) = Tensorflow::Session_options.new if .nil? self.status = Tensorflow::Status.new cOpt = .c c_session = Tensorflow::TF_NewSession(graph.c, cOpt, status.c) Tensorflow::TF_DeleteSessionOptions(cOpt) raise 'Error in session initialization.' if status.code != 0 self.c = c_session end |
#graph ⇒ Object
A TensorFlow graph is a description of computations. To compute anything, a graph must be launched in a Session. A Session places the graph ops and provides methods to execute them.
19 20 21 |
# File 'lib/tensorflow/session.rb', line 19 def graph @graph end |
#ops ⇒ Object
Nodes in the graph are called ops (short for operations). An op takes zero or more Tensors, performs some computation, and produces zero or more Tensors.
19 20 21 |
# File 'lib/tensorflow/session.rb', line 19 def ops @ops end |
#session ⇒ Object
Returns the value of attribute session.
11 12 13 |
# File 'lib/tensorflow/session.rb', line 11 def session @session end |
#status ⇒ Object
Returns the value of attribute status.
11 12 13 |
# File 'lib/tensorflow/session.rb', line 11 def status @status end |
Instance Method Details
#close ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/tensorflow/session.rb', line 70 def close status = Tensorflow::Status.new Tensorflow::TF_CloseSession(c, status.c) raise 'Error in closing session.' if status.code != 0 Tensorflow::TF_DeleteSession(c, status.c) raise 'Error in deleting session.' if status.code != 0 self.c = nil end |
#run(inputs, outputs, targets) ⇒ Object
Run the graph with the associated session starting with the supplied feeds to compute the value of the requested fetches. Runs, but does not return Tensors for operations specified in targets.
On success, returns the fetched Tensors in the same order as supplied in the fetches argument. If fetches is set to nil, the returned Tensor fetches is empty.
Note that the caller maintains responsibility for the input tensors, so the caller should still call tensor.delete() on each input tensor
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/tensorflow/session.rb', line 39 def run(inputs, outputs, targets) inputPorts = Tensorflow::TF_Output_vector.new inputValues = Tensorflow::Tensor_Vector.new inputs.each do |port, tensor| inputPorts.push(port.c) inputValues.push(tensor.tensor) end outputPorts = Tensorflow::TF_Output_vector.new outputs.each do |output| outputPorts.push(output.c) end cTargets = Tensorflow::TF_Operation_vector.new targets.each do |targe| cTargets.push(targe.c) end outputValues = Tensorflow::Session_run(c, inputPorts, inputValues, outputPorts, cTargets) output_array = [] outputValues.each do |value| converted_value = convert_value_for_output_array(value) output_array.push(converted_value) # Since we're returning the results as an array, there's no point keeping # the output tensor, so we delete it. Tensorflow::TF_DeleteTensor(value) end output_array end |