Class: Tensorflow::Graph

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

Overview

A TensorFlow computation is represented as a dataflow graph. A Graph contains a set of Operation objects, which represent units of computation; and Tensor objects, which represent the units of data that flow between operations. Official documentation of graph. Graph represents a computation graph. Graphs may be shared between sessions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGraph

Returns a new instance of Graph.



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

def initialize
    self.c = Tensorflow::TF_NewGraph()
    @number_of_defaults_created = Hash.new(0)
end

Instance Attribute Details

#cObject

contains the graph representation.



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

def c
  @c
end

Instance Method Details

#AddOperation(opspec) ⇒ Object

Add a method for variables so that they are not alone everything uptil set attributes is okay but then we need reflect equivalent for ruby



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
# File 'lib/tensorflow/graph.rb', line 98

def AddOperation(opspec)
    opspec.name = opspec.type if opspec.name.nil?
    opspec.name = opspec.type if opspec.name == ''
    cname = CString(opspec.name)
    ctype = CString(opspec.type)
    cdesc = Tensorflow::TF_NewOperation(c, ctype, cname)

    unless opspec.input.empty?
        opspec.input.each do |name|
            Tensorflow::TF_AddInput(cdesc, name.c)
        end
    end

    unless opspec.inputlist.empty?
        c_array = Tensorflow::TF_Output_vector.new
        length = opspec.inputlist.length
        opspec.inputlist.each_with_index { |value, i| c_array[i] = value.c }
        c_array = Tensorflow::TF_Output_array_from_vector(c_array)
        cdesc = Tensorflow.input_list_helper(cdesc, c_array, length)
     end

    status = Tensorflow::Status.new
    opspec.attr.each do |name, value|
        cdesc, status = set_attributes(cdesc, status, name, value)
        # Memory leak here as the TF_OperationDescription
        # object will not be cleaned up. At the time of this
        # writing, this was next to impossible since it
        # required value to be a string tensor with
        # incorrectly encoded strings. Given this rarity, live
        # with the memory leak.  If it becomes a real problem,
        # consider adding a TF_DeleteOperationDescription
        # function to the C API.
    end
    Tensorflow::Operation.new(Tensorflow::TF_FinishOperation(cdesc, status.c), self)
end

#constant(value, name: nil, dtype: nil) ⇒ Object

Creates a constant Tensor that is added to the graph with a specified name. Official documentation of tf.constant.



85
86
87
88
89
90
91
92
93
94
# File 'lib/tensorflow/graph.rb', line 85

def constant(value, name: nil, dtype: nil)
    # Value is the tensor but for now we can ignore that shit
    # Raise error if name and data type are incorrect in any way
    # we have both datatype and tensor for this.
    tensor = Tensorflow::Tensor.new(value, dtype)
    name ||= default_name('Constant')
    opspec = Tensorflow::OpSpec.new(name, 'Const', 'dtype' => {tensor.type_num => 'DataType' }, 'value' => {tensor => 'tensor'})
    operation = AddOperation(opspec)
    operation.output(0)
end

#delete_graphObject



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

def delete_graph
    Tensorflow::TF_DeleteGraph(c)
end

#import(byte, prefix) ⇒ Object

import function imports the nodes and edges from a serialized representation of another Graph into g. Names of imported nodes will be prefixed with prefix.



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tensorflow/graph.rb', line 36

def import(byte, prefix)
    cprefix = CString(prefix)
    opts = Tensorflow::TF_NewImportGraphDefOptions()
    Tensorflow::TF_ImportGraphDefOptionsSetPrefix(opts, cprefix)

    buffer = Tensorflow::TF_NewBuffer()
    Tensorflow.buffer_read(buffer, CString(byte))
    status = Tensorflow::Status.new
    Tensorflow::TF_GraphImportGraphDef(self.c, buffer, opts, status.c)
    Tensorflow::TF_DeleteImportGraphDefOptions(opts)
    Tensorflow::TF_DeleteBuffer(buffer)
end

#operation(name) ⇒ Object

Operation returns the Operation named name in the Graph, or nil if no such operation is present.



63
64
65
66
67
68
69
70
71
# File 'lib/tensorflow/graph.rb', line 63

def operation(name)
    c_operation = Tensorflow::TF_GraphOperationByName(c, CString(name))
    if c_operation.nil?
        warn("No Operation with the name #{name} exists.")
        nil
    else
        Tensorflow::Operation.new(c_operation, self)
    end
end

#placeholder(name, type_enum) ⇒ Object

Adds a placeholder to the Graph, a placeholder is an operation that must be fed with data on execution. Notice that this does not have the shape parameter. Official documentation of tf.placeholder.



77
78
79
80
81
# File 'lib/tensorflow/graph.rb', line 77

def placeholder(name, type_enum)
    opspec = Tensorflow::OpSpec.new(name, 'Placeholder', 'dtype' => {type_enum => 'DataType'})
    operation = AddOperation(opspec)
    operation.output(0)
end

#read_file(filename) ⇒ Object

Loads a graph stored in pb file into a graph def. This way you can define the graph in python / ruby, save it in pb file and load it in ruby. The limitation of is that it can only read binary wire format for protocol buffer messages In order to debug convoluted messages in ruby its always a good idea to convert the format to a readable form using converter.py file in the gem and specifying the file name of the .pb file to be converted. This makes use of import function.

Raises:

  • (ArgumentError)


55
56
57
58
59
# File 'lib/tensorflow/graph.rb', line 55

def read_file(filename)
    raise ArgumentError, 'File does not exist' unless File.file?(filename)
    reader = File.read(filename)
    import(reader, '')
end

#write_file(filename) ⇒ Object

write_file writes out a serialized representation of graph to a file.



29
30
31
# File 'lib/tensorflow/graph.rb', line 29

def write_file(filename)
    File.open(filename, 'w') { |file| file.write(write_to) }
end

#write_toObject

write_to writes out a serialized representation of graph in binary wire format. This graph defination can be written to file using write_file function and then converted to a readable form using converter.py file in the gem.



21
22
23
24
25
26
# File 'lib/tensorflow/graph.rb', line 21

def write_to
    buffer = Tensorflow::TF_NewBuffer()
    status = Tensorflow::Status.new
    Tensorflow::TF_GraphToGraphDef(c, buffer, status.c)
    Tensorflow.buffer_write(buffer)
end