Class: Session

Inherits:
Object
  • Object
show all
Defined in:
lib/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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSession

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.


18
19
20
21
22
# File 'lib/session.rb', line 18

def initialize()
	self.status = Tensorflow::TF_NewStatus()
	self.ops = Tensorflow::TF_NewSessionOptions()
	self.session = Tensorflow::TF_NewSession(self.ops, self.status)
end

Instance Attribute Details

#dimensionsObject

Create a success status.


18
19
20
21
22
# File 'lib/session.rb', line 18

def initialize()
	self.status = Tensorflow::TF_NewStatus()
	self.ops = Tensorflow::TF_NewSessionOptions()
	self.session = Tensorflow::TF_NewSession(self.ops, self.status)
end

#graphObject

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.


18
19
20
# File 'lib/session.rb', line 18

def graph
  @graph
end

#opsObject

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.


18
19
20
# File 'lib/session.rb', line 18

def ops
  @ops
end

#sessionObject

Returns the value of attribute session


10
11
12
# File 'lib/session.rb', line 10

def session
  @session
end

#statusObject

Returns the value of attribute status


10
11
12
# File 'lib/session.rb', line 10

def status
  @status
end

Instance Method Details

#extend_graph(graph) ⇒ Object


138
139
140
141
142
# File 'lib/session.rb', line 138

def extend_graph(graph)
	self.status = Tensorflow::TF_NewStatus()
	Tensorflow::TF_ExtendGraph(self.session, ruby_array_to_c(graph.graph_def_raw, "char"), graph.graph_def_raw.length, self.status)
	self.graph = graph
end

#ruby_array_to_c(array, type) ⇒ Object

Converts a give ruby array to C array (using SWIG) by detecting the data type automatically. Design decision needs to be made regarding this so the all the data types are supported. Currently Integer(Ruby) is converted to long long(C) and Float(Ruby) is converted double(C).

  • Returns :

    • A c array.


32
33
34
35
36
37
38
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
# File 'lib/session.rb', line 32

def ruby_array_to_c(array, type)
 c_array = 23
 if type == "long_long"
    c_array = Tensorflow::Long_long.new(array.length)
    (0..array.length-1).each do |i|
      c_array[i] = array[i]
    end
 elsif type == "long"
    c_array = Tensorflow::Long.new(array.length)
    (0..array.length-1).each do |i|
      c_array[i] = array[i]
    end
 elsif type == "int"
    c_array = Tensorflow::Int.new(array.length)
    (0..array.length-1).each do |i|
      c_array[i] = array[i]
    end

 elsif type == "float"
    c_array = Tensorflow::Float.new(array.length)
    (0..array.length-1).each do |i|
      c_array[i] = array[i]
    end
 elsif type == "char"
    c_array = Tensorflow::Character.new(array.length)
    (0..array.length-1).each do |i|
      c_array[i] = array[i]
    end
 else
    c_array = Tensorflow::Double.new(array.length)
    (0..array.length-1).each do |i|
      c_array[i] = array[i]
    end
 end
 c_array
end

#run(inputs, outputs, targets) ⇒ Object

Runs a session on a given input.

  • Returns :

    • nil


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/session.rb', line 74

def run(inputs, outputs, targets)
	inputNames = Tensorflow::String_Vector.new()
	inputValues = Tensorflow::Tensor_Vector.new()
  if inputs != nil
	  inputs.each do |key, value|
		  inputValues.push(value)
		  inputNames.push(key)
	  end
  end

	outputNames = Tensorflow::String_Vector.new()
	outputs.each do |name|
		outputNames.push(name)
	end

	targetNames = Tensorflow::String_Vector.new()
  if targets != nil
	  targets.each do |name|
		  targetNames.push(name)
	  end
  end

	outputValues = Tensorflow::Tensor_Vector.new()
 	status = Tensorflow::TF_NewStatus()
 Tensorflow::TF_Run_wrapper(self.session , inputNames, inputValues, outputNames, outputValues, targetNames, self.status)
  raise ("Incorrect specifications passed.")  if Tensorflow::TF_GetCode(status) != Tensorflow::TF_OK
  output_array = []
  (0..outputValues.size - 1).each do |i|
    size = Tensorflow::tensor_size(outputValues[i])
    type = Tensorflow::TF_TensorType(outputValues[i])

    c_array = Tensorflow::Double.new(size)    if type == Tensorflow::TF_DOUBLE
    Tensorflow::double_reader(outputValues[i], c_array, size) if type == Tensorflow::TF_DOUBLE
    c_array = Tensorflow::Long_long.new(size) if type == Tensorflow::TF_INT64
    Tensorflow::long_long_reader(outputValues[i], c_array, size) if type == Tensorflow::TF_INT64
    c_array = Tensorflow::Int.new(size) if type == Tensorflow::TF_INT32
    Tensorflow::int_reader(outputValues[i], c_array, size) if type == Tensorflow::TF_INT32

    num_dimensions = Tensorflow::TF_NumDims(outputValues[i])
    out_dimension = []
    (0..num_dimensions - 1).each do |j|
       out_dimension.push(Tensorflow::TF_Dim(outputValues[i], j))
    end
    output = []
    (0..size - 1).each do |j|
       output.push(c_array[j])
     end
     out_dimension.reverse!
     (0..out_dimension.length - 2).each do |k|
        total_dim = output.length
        dim_array = []
        temp_array = []
        (0..total_dim - 1).each do |ind|
          temp_array.push(output[ind])
          dim_array.push(temp_array) if (temp_array.length == out_dimension[k])
          temp_array = []          if (temp_array.length == out_dimension[k])
        end
        output = dim_array
     end
     output_array.push(output)
  end
  output_array
end