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, #device, #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, *args) ⇒ 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
25
26
27
28
29
30
31
# File 'lib/tensor_stream/operation.rb', line 7

def initialize(operation, *args)
  options = if args.last.is_a?(Hash)
    args.pop
  else
    {}
  end

  inputs = args

  setup_initial_state(options)

  @operation = operation
  @rank = options[:rank] || 0
  @name = [@graph.get_name_scope, options[:name] || set_name].compact.reject(&:empty?).join('/')
  @internal = options[:internal]
  @given_name = @name

  @options = options

  @inputs = inputs.map { |i| options[:preserve_params_type] ? i : TensorStream.convert_to_tensor(i) }
  @data_type = set_data_type(options[:data_type])
  @is_const = infer_const
  @shape = TensorShape.new(infer_shape)
  @graph.add_node(self)
end

Instance Attribute Details

#inputsObject

Returns the value of attribute inputs.



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

def inputs
  @inputs
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)


45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tensor_stream/operation.rb', line 45

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

  true
end

Instance Method Details

#infer_constObject



59
60
61
62
63
64
65
66
67
68
# File 'lib/tensor_stream/operation.rb', line 59

def infer_const
  return false if breakpoint
  case operation
  when :random_normal, :random_uniform, :glorot_uniform, :print
    false
  else
    non_const = @inputs.compact.find { |input| !input.is_const }
    non_const ? false : true
  end
end

#opObject



226
227
228
# File 'lib/tensor_stream/operation.rb', line 226

def op
  self
end

#runObject



222
223
224
# File 'lib/tensor_stream/operation.rb', line 222

def run
  eval
end

#set_data_type(passed_data_type) ⇒ 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
# File 'lib/tensor_stream/operation.rb', line 70

def set_data_type(passed_data_type)
  case operation
  when :greater, :less, :equal, :not_equal, :greater_equal, :less_equal, :logical_and
    :boolean
  when :shape, :rank
    :int32
  when :random_normal, :random_uniform, :glorot_uniform
    passed_data_type || :float32
  when :index
    if @inputs[0].is_a?(ControlFlow)

      if @inputs[1].is_const
        @inputs[0].inputs[@inputs[1].value].data_type
      else
        :unknown
      end
    else
      @inputs[0].data_type
    end
  else
    return passed_data_type if passed_data_type

    if @inputs[0]
      @inputs[0].data_type
    elsif @inputs[1]
      @inputs[1].data_type
    else
      :unknown
    end
  end
end

#to_hObject



37
38
39
40
41
42
43
# File 'lib/tensor_stream/operation.rb', line 37

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

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



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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/tensor_stream/operation.rb', line 102

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

  sub_input = auto_math(inputs[0], name_only, max_depth - 1, _cur_depth + 1)
  sub_input2 = auto_math(inputs[1], name_only, max_depth - 1, _cur_depth + 1) if inputs[1]

  out = case operation
  when :argmax
    "argmax(#{sub_input},#{options[:axis]})"
  when :negate
    "-#{sub_input}"
  when :index
    "#{sub_input}[#{sub_input2}]"
  when :slice
    "#{sub_input}[#{sub_input2}]"
  when :assign_sub
    "(#{inputs[0] ? inputs[0].name : 'self'} -= #{auto_math(inputs[1], name_only, 1)})"
  when :assign_add
    "(#{inputs[0] ? inputs[0].name : 'self'} += #{auto_math(inputs[1], name_only, 1)})"
  when :assign
    "(#{inputs[0] ? inputs[0].name : 'self'} = #{auto_math(inputs[1], name_only, 1)})"
  when :sin, :cos, :tanh
    "#{operation}(#{sub_input})"
  when :add
    "(#{sub_input} + #{sub_input2})"
  when :sub
    "(#{sub_input} - #{sub_input2})"
  when :pow
    "(#{sub_input}^#{sub_input2})"
  when :div
    "(#{sub_input} / #{sub_input2})"
  when :mul
    if auto_math(inputs[0]) == 1
      sub_input2
    elsif auto_math(inputs[1]) == 1
      sub_input
    else
      "(#{sub_input} * #{sub_input2})"
    end
  when :sum
    "sum(|#{sub_input}|,  axis=#{sub_input2})"
  when :mean
    "mean(|#{sub_input}|, axis=#{sub_input2})"
  when :prod
    "prod(|#{sub_input}|,  axis=#{sub_input2})"
  when :gradients
    "gradient(#{sub_input})"
  when :stop_gradient
    sub_input
  when :matmul
    "#{sub_input}.matmul(#{sub_input2})"
  when :eye
    "eye(#{sub_input})"
  when :transpose
    "transpose(#{sub_input})"
  when :shape
    "#{sub_input}.shape"
  when :exp
    "e^#{sub_input})"
  when :ones
    "ones(#{sub_input})"
  when :ones_like
    "ones_like(#{sub_input})"
  when :flow_group
    "flow_group(#{inputs.collect { |i| auto_math(i, name_only, max_depth - 1, _cur_depth) }.join(',')})"
  when :zeros
    "zeros(#{sub_input})"
  when :reshape
    "reshape(#{sub_input},#{sub_input2})"
  when :rank
    "#{sub_input}.rank"
  when :cond
    "(#{auto_math(options[:pred], name_only, max_depth - 1, _cur_depth)} ? #{sub_input} : #{sub_input2})"
  when :less
    "#{sub_input} < #{sub_input2}"
  when :less_equal
    "#{sub_input} <= #{sub_input2}"
  when :greater
    "#{sub_input} > #{sub_input2}"
  when :greater_equal
    "#{sub_input} >= #{sub_input2}"
  when :square
    "#{sub_input}\u00B2"
  when :log
    "log(#{sub_input})"
  when :identity
    "identity(#{sub_input})"
  when :print
    "print(#{sub_input})"
  when :pad
    "pad(#{sub_input},#{auto_math(options[:paddings])})"
  when :equal
    "#{sub_input} == #{sub_input2}"
  when :not_equal
    "#{sub_input} != #{sub_input2}"
  when :logical_and
    "#{sub_input} && #{sub_input2}"
  when :sqrt
    "sqrt(#{sub_input})"
  when :log1p
    "log1p(#{sub_input})"
  when :zeros_like
    "zeros_like(#{sub_input})"
  when :where
    "where(#{auto_math(options[:pred], name_only, max_depth - 1, _cur_depth)}, #{sub_input}, #{sub_input2})"
  when :max
    "max(#{sub_input},#{sub_input2})"
  when :cast
    "cast(#{sub_input}, #{data_type})"
  when :broadcast_transform
    "broadcast_transform(#{sub_input},#{sub_input2})"
  when :broadcast_gradient_args
    "broadcast_transform(#{sub_input},#{sub_input2})"
  else
    "#{operation}(#{sub_input})" if sub_input
    "#{operation}(#{sub_input}, #{sub_input2})" if sub_input && sub_input2
  end
  ["\n",(_cur_depth + 1).times.collect { ' ' }, out].flatten.join
end

#to_sObject



33
34
35
# File 'lib/tensor_stream/operation.rb', line 33

def to_s
  @name
end