Class: TensorStream::Graph

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

Overview

A class that defines a TensorStream graph

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGraph

Returns a new instance of Graph.



6
7
8
9
10
11
12
# File 'lib/tensor_stream/graph.rb', line 6

def initialize
  @eager_execution = false
  @nodes = {}
  @collections = {
    :"#{GraphKeys::GLOBAL_VARIABLES}" => []
  }
end

Instance Attribute Details

#collectionsObject

Returns the value of attribute collections.



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

def collections
  @collections
end

#eager_executionObject

Returns the value of attribute eager_execution.



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

def eager_execution
  @eager_execution
end

#nodesObject

Returns the value of attribute nodes.



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

def nodes
  @nodes
end

#random_seedObject

Returns the value of attribute random_seed.



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

def random_seed
  @random_seed
end

Class Method Details

.create_defaultObject



48
49
50
# File 'lib/tensor_stream/graph.rb', line 48

def self.create_default
  Thread.current[:tensor_stream_current_graph] = TensorStream::Graph.new
end

.get_default_graphObject



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

def self.get_default_graph
  Thread.current[:tensor_stream_current_graph] || create_default
end

Instance Method Details

#add_node(node) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tensor_stream/graph.rb', line 61

def add_node(node)
  raise 'Placeholder cannot be used when eager_execution is enabled' if @eager_execution && node.is_a?(Placeholder)

  node.name = if @nodes[node.name]
    uniqunify(node.name)
  else
    node.name
  end

  @nodes[node.name] = node
  node.send(:propagate_consumer, node)
  node.value = node.eval if @eager_execution
end

#add_node!(name, node) ⇒ Object



83
84
85
86
# File 'lib/tensor_stream/graph.rb', line 83

def add_node!(name, node)
  @nodes[name] = node
  node
end

#add_to_collection(collection_name, val) ⇒ Object



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

def add_to_collection(collection_name, val)
  @collections[collection_name.to_sym] ||= []
  @collections[collection_name.to_sym] << val
end

#add_variable(node, options = {}) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/tensor_stream/graph.rb', line 88

def add_variable(node, options = {})
  scope = _variable_scope

  raise "duplicate variable detected #{node.name} and reuse=false in current scope" if @nodes[node.name] && !scope.reuse

  return @nodes[node.name] if @nodes[node.name]
  
  raise "shape is not declared for #{node.name}" if node.shape.nil?

  if !options[:collections].nil? && !options[:collections].empty?
    options[:collections] = [options[:collections]] unless options[:collections].is_a?(Array)
    options[:collections].each { |coll| add_to_collection(coll, node) } 
  end

  add_to_collection(GraphKeys::GLOBAL_VARIABLES, node)
  add_to_collection(GraphKeys::TRAINABLE_VARIABLES, node) if node.trainable?
  add_node(node)
end

#as_default {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



26
27
28
29
30
# File 'lib/tensor_stream/graph.rb', line 26

def as_default
  Thread.current[:tensor_stream_current_graph] = self
  yield(self) if block_given?
  self
end

#control_dependencies(_dependencies = [], &_block) ⇒ Object



107
108
109
# File 'lib/tensor_stream/graph.rb', line 107

def control_dependencies(_dependencies = [], &_block)
  raise 'not implemented'
end

#disable_eager_executionObject



115
116
117
# File 'lib/tensor_stream/graph.rb', line 115

def disable_eager_execution
  @eager_execution = false
end

#enable_eager_executionObject



111
112
113
# File 'lib/tensor_stream/graph.rb', line 111

def enable_eager_execution
  @eager_execution = true
end

#executing_eagerly?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/tensor_stream/graph.rb', line 119

def executing_eagerly?
  @eager_execution
end

#get_collection(name, _options = {}) ⇒ Object



52
53
54
# File 'lib/tensor_stream/graph.rb', line 52

def get_collection(name, _options = {})
  @collections[name.to_sym]
end

#get_const_counterObject



149
150
151
152
153
154
155
156
# File 'lib/tensor_stream/graph.rb', line 149

def get_const_counter
  @const_counter ||= 0

  name = @const_counter.zero? ? '' : "_#{@const_counter}"

  @const_counter += 1
  name
end

#get_name_scopeObject



158
159
160
161
162
163
# File 'lib/tensor_stream/graph.rb', line 158

def get_name_scope
  graph_thread_storage = Thread.current["ts_graph_#{object_id}"]
  return nil if graph_thread_storage.nil?

  graph_thread_storage[:current_scope].join('/')
end

#get_node(name) ⇒ Object



79
80
81
# File 'lib/tensor_stream/graph.rb', line 79

def get_node(name)
  @nodes[name]
end

#get_operation_counterObject



123
124
125
126
127
128
129
130
131
# File 'lib/tensor_stream/graph.rb', line 123

def get_operation_counter
  @op_counter ||= 0

  name = @op_counter.zero? ? '' : "_#{@op_counter}"

  @op_counter += 1

  name
end

#get_placeholder_counterObject



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

def get_placeholder_counter
  @placeholder_counter ||= 0
  @placeholder_counter += 1

  return '' if @placeholder_counter == 1
  "_#{@placeholder_counter}"
end

#get_var_counterObject



141
142
143
144
145
146
147
# File 'lib/tensor_stream/graph.rb', line 141

def get_var_counter
  @var_counter ||= 0
  @var_counter += 1

  return '' if @var_counter == 1
  "_#{@var_counter}"
end

#name_scope(name = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tensor_stream/graph.rb', line 32

def name_scope(name = nil)
  Thread.current["ts_graph_#{object_id}"] ||= {}
  Thread.current["ts_graph_#{object_id}"][:current_scope] ||= []
  Thread.current["ts_graph_#{object_id}"][:current_scope] << name

  begin
    yield get_name_scope if block_given?
  ensure
    Thread.current["ts_graph_#{object_id}"][:current_scope].pop
  end
end

#node_added?(name) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/tensor_stream/graph.rb', line 75

def node_added?(name)
  @nodes.key?(name)
end

#resetObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/tensor_stream/graph.rb', line 14

def reset
  @placeholder_counter = 0
  @const_counter = 0
  @var_counter = 0
  @op_counter = 0
  @random_seed = nil
  @nodes = {}
  @collections = {
    :"#{GraphKeys::GLOBAL_VARIABLES}" => []
  }
end