Class: Tensorflow::ExecutionContext

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/tensorflow/execution_context.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeExecutionContext

Returns a new instance of ExecutionContext.



12
13
14
# File 'lib/tensorflow/execution_context.rb', line 12

def initialize
  @stack = Array.new
end

Class Method Details

.contextObject



8
9
10
# File 'lib/tensorflow/execution_context.rb', line 8

def self.context
  Thread.current[:execution_context] ||= self.new
end

Instance Method Details

#current(inputs = []) ⇒ Object



48
49
50
# File 'lib/tensorflow/execution_context.rb', line 48

def current(inputs=[])
  figure_from_context || figure_from_inputs(inputs) || figure_from_execution_mode
end

#eager?(inputs = []) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/tensorflow/execution_context.rb', line 52

def eager?(inputs=[])
  context = self.current(inputs)
  context.is_a?(Eager::Context)
end

#figure_from_contextObject



36
37
38
# File 'lib/tensorflow/execution_context.rb', line 36

def figure_from_context
  @stack.last
end

#figure_from_execution_modeObject



40
41
42
43
44
45
46
# File 'lib/tensorflow/execution_context.rb', line 40

def figure_from_execution_mode
  if ::Tensorflow.execution_mode == Tensorflow::GRAPH_MODE
    Graph::Graph.default
  else
    Eager::Context.default
  end
end

#figure_from_inputs(inputs = []) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/tensorflow/execution_context.rb', line 24

def figure_from_inputs(inputs=[])
  inputs.flatten.each do |input|
    case input
      when Graph::Operation
        return input.graph
      when Eager::TensorHandle
        return input.context
    end
  end
  nil
end

#graph?(inputs = []) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/tensorflow/execution_context.rb', line 57

def graph?(inputs=[])
  context = self.current(inputs)
  context.is_a?(Graph::Graph)
end

#popObject



20
21
22
# File 'lib/tensorflow/execution_context.rb', line 20

def pop
  @stack.pop
end

#push(value) ⇒ Object



16
17
18
# File 'lib/tensorflow/execution_context.rb', line 16

def push(value)
  @stack.push(value)
end