Class: Tensorflow::Graph::Session

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph, options) ⇒ Session

Returns a new instance of Session.



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

def initialize(graph, options)
  @graph = graph
  Status.check do |status|
    @pointer = FFI.TF_NewSession(graph, options, status)
  end
end

Instance Attribute Details

#graphObject

Returns the value of attribute graph.



22
23
24
# File 'lib/tensorflow/graph/session.rb', line 22

def graph
  @graph
end

#optionsObject

Returns the value of attribute options.



22
23
24
# File 'lib/tensorflow/graph/session.rb', line 22

def options
  @options
end

Class Method Details

.finalize(pointer) ⇒ Object



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

def self.finalize(pointer)
  proc do
    FFI.TF_DeleteSession(pointer)
  end
end

.run(graph) ⇒ Object



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

def self.run(graph)
  session = self.new(graph, SessionOptions.new)
  result = yield session
  session.close
  result
end

Instance Method Details

#closeObject



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

def close
  Status.check do |status|
    FFI.TF_CloseSession(self, status)
  end
end

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
# File 'lib/tensorflow/graph/session.rb', line 48

def run(operations, feed_dict={})
  operations = Array(operations).flatten.compact

  key_outputs = feed_dict.keys.map(&:outputs).flatten
  keys_ptr = FFI::Output.array_to_ptr(key_outputs.map(&:output))

  values = self.values_to_tensors(feed_dict)
  values_ptr = ::FFI::MemoryPointer.new(:pointer, values.length)
  values_ptr.write_array_of_pointer(values)

  # Gather up all the outputs for each operation
  outputs = operations.map do |operation|
    case operation
      when Operation, Variable
        operation.outputs
      when OperationOutput
        operation
      else
        raise(Error::UnimplementedError, "Unsupported operation type: #{operation}")
    end
  end.flatten

  outputs_ptr = FFI::Output.array_to_ptr(outputs.map(&:output))
  results_ptr = ::FFI::MemoryPointer.new(:pointer, outputs.length)

  # Gather up all the targets
  targets = operations.map do |operation|
    case operation
      when Operation, Variable
        operation
      when OperationOutput
        operation.operation
      else
        raise("Unsupported target: #{operation}")
    end
  end
  targets_ptr = ::FFI::MemoryPointer.new(:pointer, targets.length)
  targets_ptr.write_array_of_pointer(targets)

  run_options = nil
   = nil

  Status.check do |status|
    FFI.TF_SessionRun(self, run_options,
                      # Inputs
                      keys_ptr, values_ptr, feed_dict.keys.length,
                      # Outputs
                      outputs_ptr, results_ptr, outputs.length,
                      # Targets
                      targets_ptr, operations.length,
                      ,
                      status)
  end

  results = results_ptr.read_array_of_pointer(outputs.length).map.with_index do |pointer, i|
    output = outputs[i]
    Tensor.from_pointer(pointer).value
  end

  # For each operation we want to return a single result
  start = 0
  result = operations.reduce(Array.new) do |array, operation|
    length = case operation
               when Operation, Variable
                 operation.outputs.length
               when OperationOutput
                 1
               else
                 raise(Error::UnimplementedError, "Unsupported operation type: #{operation}")
             end

    if length == 0
      array << nil
    else
      array.concat(results[start, length])
      start += length
    end
    array
  end

  if operations.length == 1 && results.length == 1
    result.first
  else
    result
  end
end

#to_ptrObject



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

def to_ptr
  @pointer
end

#values_to_tensors(values) ⇒ Object



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

def values_to_tensors(values)
  values.map do |key, value|
    case value
      when Tensor
        value
      else
        # The value dtype needs to match the key dtype
        raise(Error::UnknownError, "Cannot determine dtype: #{key}") if key.num_outputs != 1
        dtype = key.output_types.first
        Tensor.new(value, dtype: dtype)
      end
  end
end