Class: Tensorflow::Eager::Context

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContext

Returns a new instance of Context.



15
16
17
18
19
20
21
22
23
# File 'lib/tensorflow/eager/context.rb', line 15

def initialize
  @name_scope = NameScope.new
  options = FFI.TFE_NewContextOptions
  Status.check do |status|
    @pointer = FFI.TFE_NewContext(options, status)
  end
  ObjectSpace.define_finalizer(self, self.class.finalize(@pointer))
  FFI.TFE_DeleteContextOptions(options)
end

Class Method Details

.defaultObject



7
8
9
# File 'lib/tensorflow/eager/context.rb', line 7

def self.default
  @default ||= Context.new
end

.finalize(pointer) ⇒ Object



11
12
13
# File 'lib/tensorflow/eager/context.rb', line 11

def self.finalize(pointer)
  proc { FFI.TFE_DeleteContext(pointer) }
end

Instance Method Details

#add_function(function) ⇒ Object



100
101
102
103
104
# File 'lib/tensorflow/eager/context.rb', line 100

def add_function(function)
  Status.check do |status|
    FFI.TFE_ContextAddFunction(self, function, status)
  end
end

#add_to_collection(name, value) ⇒ Object

Mimic graph api



90
91
# File 'lib/tensorflow/eager/context.rb', line 90

def add_to_collection(name, value)
end

#add_to_collections(names, value) ⇒ Object

Mimic graph api



94
95
# File 'lib/tensorflow/eager/context.rb', line 94

def add_to_collections(names, value)
end

#as_defaultObject



25
26
27
28
29
30
31
32
33
# File 'lib/tensorflow/eager/context.rb', line 25

def as_default
  raise(Error::InvalidArgumentError, "Must provide block") unless block_given?
  ExecutionContext.push(self)
  begin
    yield self
  ensure
    ExecutionContext.pop
  end
end

#create_operation(op_type, inputs = [], attrs = {}) ⇒ Object



35
36
37
# File 'lib/tensorflow/eager/context.rb', line 35

def create_operation(op_type, inputs=[], attrs={})
  Operation.new(self, op_type, inputs, attrs)
end

#device_policyObject



60
61
62
# File 'lib/tensorflow/eager/context.rb', line 60

def device_policy
  FFI::ContextDevicePlacementPolicy[FFI.TFE_ContextGetDevicePlacementPolicy(@pointer)]
end

#disable_run_metadataObject



68
69
70
# File 'lib/tensorflow/eager/context.rb', line 68

def 
  FFI.TFE_ContextDisableRunMetadata(@pointer)
end

#enable_run_metadataObject



64
65
66
# File 'lib/tensorflow/eager/context.rb', line 64

def 
  FFI.TFE_ContextEnableRunMetadata(@pointer)
end

#end_stepObject



76
77
78
# File 'lib/tensorflow/eager/context.rb', line 76

def end_step
  FFI.TFE_ContextEndStep(@pointer)
end

#execute(operation) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tensorflow/eager/context.rb', line 39

def execute(operation)
  # TODO decide how many retvals to allocate
  retvals = ::FFI::MemoryPointer.new(:pointer, 10)
  num_retvals = ::FFI::MemoryPointer.new(:int)
  num_retvals.write_int(retvals.size)

  Status.check do |status|
    FFI.TFE_Execute(operation, retvals, num_retvals, status)
  end

  n = num_retvals.read_int
  if n > 0
    handles = retvals.read_array_of_pointer(n).map do |handle|
      TensorHandle.new(self, handle)
    end

    # TODO handle case where n = 1 and still want an array for retvals
    n == 1 ? handles.first : handles
  end
end

#function?(function) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
116
117
# File 'lib/tensorflow/eager/context.rb', line 113

def function?(function)
  name = function.is_a?(Graph::Function) ? function.name : function
  # result is uchar
  FFI.TFE_ContextHasFunction(self, name) != 0
end

#get_collection_ref(name) ⇒ Object



97
98
# File 'lib/tensorflow/eager/context.rb', line 97

def get_collection_ref(name)
end

#remove_function(function) ⇒ Object



106
107
108
109
110
111
# File 'lib/tensorflow/eager/context.rb', line 106

def remove_function(function)
  name = function.is_a?(Graph::Function) ? function.name : function
  Status.check do |status|
    FFI.TFE_ContextRemoveFunction(self, name, status)
  end
end

#shared_nameObject



84
85
86
87
# File 'lib/tensorflow/eager/context.rb', line 84

def shared_name
  # hard-coded in Python library
  "cd2c89b7-88b7-44c8-ad83-06c2a9158347"
end

#start_stepObject



72
73
74
# File 'lib/tensorflow/eager/context.rb', line 72

def start_step
  FFI.TFE_ContextStartStep(@pointer)
end

#to_ptrObject



80
81
82
# File 'lib/tensorflow/eager/context.rb', line 80

def to_ptr
  @pointer
end