Class: Tensorflow::Scope

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

Overview

Scope encapsulates common operation properties when building a Graph.

A Scope object (and its derivates, e.g., obtained from Scope.SubScope) act as a builder for graphs. They allow common properties (such as a name prefix) to be specified for multiple operations being added to the graph.

A Scope object and all its derivates (e.g., obtained from Scope.SubScope) are not safe for concurrent use by multiple goroutines.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace = '') ⇒ Scope

creates a Scope initialized with an empty Graph.



13
14
15
16
17
# File 'lib/tensorflow/scope.rb', line 13

def initialize(namespace = '')
    self.graph = Tensorflow::Graph.new
    self.namemap = {}
    self.namespace = namespace
end

Instance Attribute Details

#graphObject

Returns the value of attribute graph.



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

def graph
  @graph
end

#namemapObject

Returns the value of attribute namemap.



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

def namemap
  @namemap
end

#namespaceObject

Returns the value of attribute namespace.



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

def namespace
  @namespace
end

Instance Method Details

#AddOperation(args) ⇒ Object

AddOperation adds the operation to the Graph managed by s. If there is a name prefix associated with s (such as if s was created by a call to SubScope), then this prefix will be applied to the name of the operation being added. See also Graph.AddOperation.



23
24
25
26
27
# File 'lib/tensorflow/scope.rb', line 23

def AddOperation(args)
    args.name = args.type if args.name == ''
    args.name = namespace + '/' + args.name if namespace != ''
    op = graph.AddOperation(args)
end

#op_name(typ) ⇒ Object



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

def op_name(typ)
    namespace + '/' + typ
end

#subscope(namespace) ⇒ Object

SubScope returns a new Scope which will cause all operations added to the graph to be namespaced with ‘namespace’. If namespace collides with an existing namespace within the scope, then a suffix will be added.



32
33
34
35
36
37
38
39
40
# File 'lib/tensorflow/scope.rb', line 32

def subscope(namespace)
    namespace = unique_name(namespace)
    namespace = self.namespace + '/' + namespace if self.namespace != ''
    sub_scope = Tensorflow::Scope.new
    sub_scope.graph = graph.clone
    sub_scope.namemap = namemap.clone
    sub_scope.namespace = namespace.clone
    sub_scope
end

#unique_name(name) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/tensorflow/scope.rb', line 42

def unique_name(name)
    namemap[name] = 0 if namemap[name].nil?
    counts = namemap[name]
    namemap[name] = counts + 1
    name = name + '_' + counts.to_s if counts != 0
    name
end