Class: TensorStream::Operation

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

Direct Known Subclasses

ControlFlow

Instance Attribute Summary collapse

Attributes inherited from Tensor

#breakpoint, #data_type, #given_name, #graph, #internal, #is_const, #native_buffer, #shape, #source, #value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Tensor

#!=, #*, #**, #+, #-, #-@, #/, #<, #<=, #==, #>, #>=, #[], #auto_math, #breakpoint!, #build_buffer, cast_dtype, #collect, const_name, detect_type, #dtype, #eval, #first, #internal?, #open_cl_buffer, placeholder_name, #sync_cl_buffer, #to_a, #to_f, #to_i, var_name

Methods included from OpHelper

#cons, #dtype_eval, #i_cons, #i_op, #op, #shape_eval, #val_to_dtype

Constructor Details

#initialize(operation, a, b, options = {}) ⇒ Operation



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/tensor_stream/operation.rb', line 5

def initialize(operation, a, b, options = {})
  @operation = operation
  @rank = options[:rank] || 0
  @name = options[:name] || set_name
  @internal = options[:internal]
  @given_name = @name
  @source = set_source(caller_locations)

  @graph = options[:graph] || TensorStream.get_default_graph
  @options = options


  @items = [a, b].map { |i| options[:preserve_params_type] ? i : auto_wrap(i) }
  @data_type = set_data_type(options[:data_type])

  if options[:shape]
    @shape = TensorShape.new(options[:shape], options[:shape].size || 0)
  end
  @graph.add_node(self)
end

Instance Attribute Details

#itemsObject

Returns the value of attribute items.



3
4
5
# File 'lib/tensor_stream/operation.rb', line 3

def items
  @items
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/tensor_stream/operation.rb', line 3

def name
  @name
end

#operationObject

Returns the value of attribute operation.



3
4
5
# File 'lib/tensor_stream/operation.rb', line 3

def operation
  @operation
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/tensor_stream/operation.rb', line 3

def options
  @options
end

#rankObject

Returns the value of attribute rank.



3
4
5
# File 'lib/tensor_stream/operation.rb', line 3

def rank
  @rank
end

Class Method Details

.empty_matrix?(m) ⇒ Boolean



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/tensor_stream/operation.rb', line 41

def self.empty_matrix?(m)
  if m.kind_of?(Array)
    m.each do |item|
      if item.kind_of?(Array)
        return false if !empty_matrix?(item)
      else
        return false if item!=0 || item!=0.0
      end
    end
  end

  return true
end

.operation_counterObject



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/tensor_stream/operation.rb', line 177

def self.operation_counter
  @@op_counter ||= 0

  name = if @@op_counter == 0
    ""
  else
    "_#{@@op_counter}"
  end

  @@op_counter += 1
  
  name
end

.reset_countersObject



29
30
31
# File 'lib/tensor_stream/operation.rb', line 29

def self.reset_counters
  @@op_counter = 0
end

Instance Method Details

#runObject



171
172
173
# File 'lib/tensor_stream/operation.rb', line 171

def run
  self.eval
end

#set_data_type(passed_data_type) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/tensor_stream/operation.rb', line 55

def set_data_type(passed_data_type)
  case operation
  when :greater, :less, :equal
    :boolean
  when :shape, :rank
    :int32
  else
    passed_data_type || (@items[0] ? @items[0].data_type : :unknown)
  end
end

#to_hObject



33
34
35
36
37
38
39
# File 'lib/tensor_stream/operation.rb', line 33

def to_h
  {
    op: operation,
    name: name,
    operands: hashify_tensor(items)
  }
end

#to_math(name_only = false, max_depth = 99) ⇒ Object



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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/tensor_stream/operation.rb', line 66

def to_math(name_only = false, max_depth = 99)
  return @name if max_depth == 0

  sub_item = auto_math(items[0], name_only, max_depth - 1)

  case operation
  when :argmax
    "argmax(#{auto_math(items[0])},#{options[:axis]})"
  when :negate
    "-#{sub_item}"
  when :index
    "#{sub_item}[#{auto_math(items[1], name_only, max_depth - 1)}]"
  when :slice
    "#{sub_item}[#{auto_math(items[1], name_only, max_depth - 1)}]"
  when :assign_sub
    "(#{items[0] ? items[0].name : "self"} -= #{auto_math(items[1], name_only)})"
  when :assign_add
    "(#{items[0] ? items[0].name : "self"} += #{auto_math(items[1], name_only)})"
  when :assign
    "(#{items[0] ? items[0].name : "self"} = #{auto_math(items[1], name_only)})"
  when :sin, :cos, :tanh
    "#{operation}(#{sub_item})"
  when :add
   "(#{sub_item} + #{auto_math(items[1], name_only, max_depth - 1)})"
  when :sub
    "(#{sub_item} - #{auto_math(items[1], name_only, max_depth - 1)})"
  when :pow
    "(#{sub_item}^#{auto_math(items[1], name_only, max_depth - 1)})"
  when :div
    "(#{sub_item} / #{auto_math(items[1], name_only, max_depth - 1)})"
  when :mul
    if auto_math(items[0]) == 1
      auto_math(items[1], name_only, max_depth - 1)
    elsif auto_math(items[1]) == 1
      sub_item
    else
      "(#{sub_item} * #{auto_math(items[1], name_only, max_depth - 1)})"
    end
  when :reduce_sum
    "reduce_sum(|#{sub_item}|)"
  when :reduce_mean
    "reduce_mean(|#{sub_item}|)"
  when :reduce_prod
    "reduce_prod(|#{sub_item}|)"
  when :gradients
    "gradient(#{sub_item})"
  when :stop_gradient
    sub_item
  when :matmul
    "#{sub_item}.matmul(#{auto_math(items[1], name_only, max_depth - 1)})"
  when :eye
    "eye(#{sub_item})"
  when :transpose
    "transpose(#{sub_item})"
  when :shape
    "#{sub_item}.shape"
  when :exp
    "e^#{sub_item})"
  when :ones
    "ones(#{sub_item})"
  when :ones_like
    "ones_like(#{sub_item})"
  when :flow_group
    "flow_group(#{items.collect { |i| auto_math(i)}.join(',')})"
  when :zeros
    "zeros(#{sub_item})"
  when :reshape
    "reshape(#{sub_item},#{auto_math(items[1], name_only, max_depth - 1)})"
  when :rank
    "#{sub_item}.rank"
  when :cond
    "(#{auto_math(options[:pred])} ? #{sub_item} : #{auto_math(items[1], name_only, max_depth - 1)})"
  when :less
    "#{sub_item} < #{auto_math(items[1], name_only, max_depth - 1)}"
  when :greater
    "#{sub_item} > #{auto_math(items[1], name_only, max_depth - 1)}"
  when :square
    "#{sub_item}\u00B2"
  when :log
    "log(#{sub_item})"
  when :identity
    "identity(#{sub_item})"
  when :print
    "print(#{sub_item})"
  when :pad
    "pad(#{sub_item},#{auto_math(options[:paddings])})"
  when :equal
    "#{sub_item} == #{auto_math(items[1], name_only, max_depth - 1)}"
  when :not_equal
    "#{sub_item} != #{auto_math(items[1], name_only, max_depth - 1)}"
  when :sqrt
    "sqrt(#{sub_item})"
  when :zeros_like
    "zeros_like(#{sub_item})"
  when :where
    "where(#{auto_math(options[:pred] , name_only, max_depth - 1)},#{auto_math(items[0])},#{auto_math(items[1])})"
  when :max
    "max(#{auto_math(sub_item)},#{auto_math(items[1])})"
  when :cast
    "cast(#{auto_math(sub_item)}, #{data_type})"
  else
    fail "math form for #{operation}"
  end
end

#to_sObject



25
26
27
# File 'lib/tensor_stream/operation.rb', line 25

def to_s
  @name
end