Class: TensorStream::Operation

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

Overview

TensorStream class that defines an operation

Direct Known Subclasses

ControlFlow

Instance Attribute Summary collapse

Attributes inherited from Tensor

#breakpoint, #consumers, #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

#!=, #*, #**, #+, #-, #-@, #/, #<, #<=, #==, #>, #>=, #[], #and, #auto_math, #breakpoint!, cast_dtype, #collect, detect_type, #dot, #dtype, #eval, #first, #internal?, #matmul, #print!, reset_counters, #to_a, #to_f, #to_i

Methods included from OpHelper

#_op, #cons, #dtype_eval, #format_source, #fp_type?, #i_cons, #i_op, #shape_eval, #val_to_dtype

Constructor Details

#initialize(operation, input_a, input_b, options = {}) ⇒ Operation

Returns a new instance of Operation.



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 7

def initialize(operation, input_a, input_b, options = {})
  @graph = options[:graph] || TensorStream.get_default_graph

  @operation = operation
  @rank = options[:rank] || 0
  @name = [@graph.get_name_scope, options[:name] || set_name].compact.join('/')
  @internal = options[:internal]
  @given_name = @name
  @source = format_source(caller_locations)

  @options = options

  @items = [input_a, input_b].map { |i| options[:preserve_params_type] ? i : TensorStream.convert_to_tensor(i) }
  @data_type = set_data_type(options[:data_type])

  @shape = TensorShape.new(infer_shape)
  @graph.add_node(self)
end

Instance Attribute Details

#itemsObject

Returns the value of attribute items.



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

def items
  @items
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#operationObject

Returns the value of attribute operation.



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

def operation
  @operation
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#outputsObject (readonly)

Returns the value of attribute outputs.



5
6
7
# File 'lib/tensor_stream/operation.rb', line 5

def outputs
  @outputs
end

#rankObject

Returns the value of attribute rank.



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

def rank
  @rank
end

Class Method Details

.empty_matrix?(input) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tensor_stream/operation.rb', line 38

def self.empty_matrix?(input)
  if input.is_a?(Array)
    input.each do |item|
      if item.is_a?(Array)
        return false unless empty_matrix?(item)
      elsif item != 0 || item != 0.0
        return false
      end
    end
  end

  true
end

Instance Method Details

#opObject



187
188
189
# File 'lib/tensor_stream/operation.rb', line 187

def op
  self
end

#runObject



183
184
185
# File 'lib/tensor_stream/operation.rb', line 183

def run
  eval
end

#set_data_type(passed_data_type) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/tensor_stream/operation.rb', line 52

def set_data_type(passed_data_type)
  case operation
  when :greater, :less, :equal, :not_equal, :greater_equal, :less_equal
    :boolean
  when :shape, :rank
    :int32
  else
    return passed_data_type if passed_data_type
    if @items[0]
      @items[0].data_type
    elsif @items[1]
      @items[1].data_type
    else
      :unknown
    end
  end
end

#to_hObject



30
31
32
33
34
35
36
# File 'lib/tensor_stream/operation.rb', line 30

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

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



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
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/tensor_stream/operation.rb', line 70

def to_math(name_only = false, max_depth = 99, _cur_depth = 0)
  return @name if max_depth.zero?

  sub_item = auto_math(items[0], name_only, max_depth - 1, _cur_depth + 1)
  sub_item2 = auto_math(items[1], name_only, max_depth - 1, _cur_depth + 1) if items[1]

  out = case operation
  when :argmax
    "argmax(#{sub_item},#{options[:axis]})"
  when :negate
    "-#{sub_item}"
  when :index
    "#{sub_item}[#{sub_item2}]"
  when :slice
    "#{sub_item}[#{sub_item2}]"
  when :assign_sub
    "(#{items[0] ? items[0].name : 'self'} -= #{auto_math(items[1], name_only, 1)})"
  when :assign_add
    "(#{items[0] ? items[0].name : 'self'} += #{auto_math(items[1], name_only, 1)})"
  when :assign
    "(#{items[0] ? items[0].name : 'self'} = #{auto_math(items[1], name_only, 1)})"
  when :sin, :cos, :tanh
    "#{operation}(#{sub_item})"
  when :add
    "(#{sub_item} + #{sub_item2})"
  when :sub
    "(#{sub_item} - #{sub_item2})"
  when :pow
    "(#{sub_item}^#{sub_item2})"
  when :div
    "(#{sub_item} / #{sub_item2})"
  when :mul
    if auto_math(items[0]) == 1
      sub_item2
    elsif auto_math(items[1]) == 1
      sub_item
    else
      "(#{sub_item} * #{sub_item2})"
    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(#{sub_item2})"
  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},#{sub_item2})"
  when :rank
    "#{sub_item}.rank"
  when :cond
    "(#{auto_math(options[:pred], name_only, max_depth - 1, _cur_depth)} ? #{sub_item} : #{sub_item2})"
  when :less
    "#{sub_item} < #{sub_item2}"
  when :less_equal
    "#{sub_item} <= #{sub_item2}"
  when :greater
    "#{sub_item} > #{sub_item2}"
  when :greater_equal
    "#{sub_item} >= #{sub_item2}"
  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} == #{sub_item2}"
  when :not_equal
    "#{sub_item} != #{sub_item2}"
  when :logical_and
    "#{sub_item} && #{sub_item2}"
  when :sqrt
    "sqrt(#{sub_item})"
  when :zeros_like
    "zeros_like(#{sub_item})"
  when :where
    "where(#{auto_math(options[:pred], name_only, max_depth - 1, _cur_depth)}, #{sub_item}, #{sub_item2})"
  when :max
    "max(#{sub_item},#{sub_item2})"
  when :cast
    "cast(#{sub_item}, #{data_type})"
  else
    raise "no math form for #{operation} defined"
  end
  ["\n",(_cur_depth + 1).times.collect { ' ' }, out].flatten.join
end

#to_sObject



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

def to_s
  @name
end