Class: Tensorflow::Graph::Graph

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGraph

Returns a new instance of Graph.



23
24
25
26
27
28
29
# File 'lib/tensorflow/graph/graph.rb', line 23

def initialize
  @collections = Hash.new
  @name_scope = NameScope.new
  @pointer = FFI.TF_NewGraph()
  @control_inputs = Array.new
  ObjectSpace.define_finalizer(self, self.class.finalize(@pointer))
end

Instance Attribute Details

#control_inputsObject (readonly)

Returns the value of attribute control_inputs.



4
5
6
# File 'lib/tensorflow/graph/graph.rb', line 4

def control_inputs
  @control_inputs
end

Class Method Details

.defaultObject



9
10
11
# File 'lib/tensorflow/graph/graph.rb', line 9

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

.finalize(pointer) ⇒ Object



17
18
19
20
21
# File 'lib/tensorflow/graph/graph.rb', line 17

def self.finalize(pointer)
  proc do
    FFI::TF_DeleteGraph(pointer)
  end
end

.reset_defaultObject



13
14
15
# File 'lib/tensorflow/graph/graph.rb', line 13

def self.reset_default
  @default = Graph.new
end

Instance Method Details

#add_function(function, gradient = nil) ⇒ Object



170
171
172
173
174
# File 'lib/tensorflow/graph/graph.rb', line 170

def add_function(function, gradient=nil)
  Status.check do |status|
    FFI.TF_GraphCopyFunction(self, function, gradient, status)
  end
end

#add_to_collection(name, value) ⇒ Object



39
40
41
42
# File 'lib/tensorflow/graph/graph.rb', line 39

def add_to_collection(name, value)
  values = @collections[name] ||= Array.new
  values << value
end

#add_to_collections(names, value) ⇒ Object



44
45
46
47
48
# File 'lib/tensorflow/graph/graph.rb', line 44

def add_to_collections(names, value)
  names.each do |name|
    self.add_to_collection(name, value)
  end
end

#as_defaultObject



58
59
60
61
62
63
64
65
66
# File 'lib/tensorflow/graph/graph.rb', line 58

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

#as_graph_defObject



217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/tensorflow/graph/graph.rb', line 217

def as_graph_def
  buffer_ptr = FFI.TF_NewBuffer
  Status.check do |status|
    FFI.TF_GraphToGraphDef(self, buffer_ptr, status)
  end

  buffer = FFI::Buffer.new(buffer_ptr)
  string = buffer[:data].read_string(buffer[:length])
  GraphDef.decode(string)
ensure
  FFI.TF_DeleteBuffer(buffer)
end

#backward(operation) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/tensorflow/graph/graph.rb', line 101

def backward(operation)
  def backward_internal(set, operation)
    operation.inputs.each do |input|
      set << input.operation
      backward_internal(set, input.operation)
    end
    set
  end
  result = Set.new([operation])
  backward_internal(result, operation)
end

#backward_internal(set, operation) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/tensorflow/graph/graph.rb', line 102

def backward_internal(set, operation)
  operation.inputs.each do |input|
    set << input.operation
    backward_internal(set, input.operation)
  end
  set
end

#clear_collection(name) ⇒ Object



54
55
56
# File 'lib/tensorflow/graph/graph.rb', line 54

def clear_collection(name)
  @collections[name] = Array.new
end

#collectionsObject



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

def collections
  @collections.keys
end

#control_dependencies(control_inputs) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/tensorflow/graph/graph.rb', line 68

def control_dependencies(control_inputs)
  @control_inputs = Array(control_inputs)
  begin
    yield self
  ensure
    @control_inputs = []
  end
end

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



129
130
131
132
# File 'lib/tensorflow/graph/graph.rb', line 129

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

#execute(operations, feed_dict = {}) ⇒ Object



134
135
136
137
138
139
# File 'lib/tensorflow/graph/graph.rb', line 134

def execute(operations, feed_dict={})
  session = Session.new(self, SessionOptions.new)
  result = session.run(operations, feed_dict)
  session.close
  result
end

#forward(operation) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/tensorflow/graph/graph.rb', line 89

def forward(operation)
  def forward_internal(set, operation)
    operation.consumers.each do |consumer|
      set << consumer.operation
      forward_internal(set, consumer.operation)
    end
    set
  end
  result = Set.new([operation])
  forward_internal(result, operation)
end

#forward_internal(set, operation) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/tensorflow/graph/graph.rb', line 90

def forward_internal(set, operation)
  operation.consumers.each do |consumer|
    set << consumer.operation
    forward_internal(set, consumer.operation)
  end
  set
end

#get_collection_ref(name, scope = nil) ⇒ Object



50
51
52
# File 'lib/tensorflow/graph/graph.rb', line 50

def get_collection_ref(name, scope=nil)
  @collections[name]
end

#import(graph_def, options = nil) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/tensorflow/graph/graph.rb', line 230

def import(graph_def, options=nil)
  options ||= GraphDefOptions.new

  data = if graph_def.is_a?(GraphDef)
           GraphDef.encode(graph_def)
         else
           graph_def
         end

  ptr = ::FFI::MemoryPointer.new(:char, data.bytesize)
  ptr.put_bytes(0, data)

  buffer = FFI::Buffer.new
  buffer[:data] = ptr
  buffer[:length] = data.bytesize

  Status.check do |status|
    FFI.TF_GraphImportGraphDef(self, buffer, options, status)
  end
end

#op_def(op_type) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tensorflow/graph/graph.rb', line 77

def op_def(op_type)
  buffer_ptr = FFI.TF_NewBuffer
  Status.check do |status|
    FFI.TF_GraphGetOpDef(self, op_type, buffer_ptr, status)
  end
  buffer = FFI::Buffer.new(buffer_ptr)
  string = buffer[:data].read_string(buffer[:length])
  OpDef.decode(string)
ensure
  FFI.TF_DeleteBuffer(buffer)
end

#operation(name) ⇒ Object



124
125
126
127
# File 'lib/tensorflow/graph/graph.rb', line 124

def operation(name)
  ptr = FFI.TF_GraphOperationByName(self, name)
  ptr.null? ? nil : Operation.new(self, ptr)
end

#operationsObject



113
114
115
116
117
118
119
120
121
122
# File 'lib/tensorflow/graph/graph.rb', line 113

def operations
  return enum_for(:operations) unless block_given?

  # Get a pointer to a size_t set to 0

  position_ptr = ::FFI::MemoryPointer.new(:size_t, 1, true)
  while (ptr = FFI.TF_GraphNextOperation(self, position_ptr))
    break if ptr.null?
    yield Operation.new(self, ptr)
  end
end

#output_shapes(operation) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/tensorflow/graph/graph.rb', line 141

def output_shapes(operation)
  operation.outputs.map do |output|
    num_dims = Status.check do |status|
      FFI.TF_GraphGetTensorNumDims(self, output, status)
    end

    if num_dims == -1
      []
    else
      dims_ptr = ::FFI::MemoryPointer.new(:int64, num_dims)
      Status.check do |status|
        FFI.TF_GraphGetTensorShape(self, output, dims_ptr, num_dims, status)
      end
      dims_ptr.read_array_of_int64(num_dims)
    end
  end
end

#tensor_set_shape(operation, shape) ⇒ Object



159
160
161
162
163
164
165
166
167
168
# File 'lib/tensorflow/graph/graph.rb', line 159

def tensor_set_shape(operation, shape)
  ptr = ::FFI::MemoryPointer.new(:int64, shape.length)
  ptr.write_array_of_int64(shape)
  output = FFI::Output.new
  output[:oper] = operation
  output[:index] = 0
  Status.check do |status|
    FFI.TF_GraphSetTensorShape(self, output, ptr, shape.length, status)
  end
end

#to_function(name, operators, input_operations, output_operations, output_names = nil) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/tensorflow/graph/graph.rb', line 176

def to_function(name, operators, input_operations, output_operations, output_names=nil)
  inputs = input_operations ? input_operations.map(&:outputs).flatten : []
  inputs_ptr = FFI::Output.array_to_ptr(inputs.map(&:output))

  outputs = output_operations ? output_operations.map(&:outputs).flatten : []
  outputs_ptr = FFI::Output.array_to_ptr(outputs.map(&:output))

  # Check output names size

  if output_names && output_names.length != outputs.length
    raise(ArgumentError, "output_names length must equal outputs length or be nil")
  end

  # Convert to pointers - keep reference to pointers so they are not GC'ed until the end of the method

  output_names_ptr = if output_names
                       output_names_ptrs = output_names.map do |output_name|
                         ::FFI::MemoryPointer.from_string(output_name)
                       end
                       output_names_ptr = ::FFI::MemoryPointer.new(:pointer, output_names_ptrs.length, true)
                       output_names_ptr.write_array_of_pointer(output_names_ptrs)
                       output_names_ptr
                     else
                       nil
                     end

  append_hash_to_fn_name = 0
  options = nil
  description = nil

  func = Status.check do |status|
    FFI.TF_GraphToFunction(self, name, append_hash_to_fn_name,
                           operators ? operators.length : -1, operators,
                           inputs ? inputs.length : 0, inputs_ptr,
                           outputs ? outputs.length: 0, outputs_ptr,
                           output_names_ptr,
                           options, description, status)
  end
  output_types = output_operations.map(&:output_types).flatten(1)
  output_shapes = output_operations.map(&:output_shapes).flatten(1)
  Function.new(func, output_types, output_shapes)
end

#to_ptrObject



31
32
33
# File 'lib/tensorflow/graph/graph.rb', line 31

def to_ptr
  @pointer
end