Module: TensorStream::Ops

Included in:
TensorStream
Defined in:
lib/tensor_stream/ops.rb

Overview

Class that defines all available ops supported by TensorStream

Constant Summary collapse

FLOATING_POINT_TYPES =
%w[float32 float64].map(&:to_sym)
NUMERIC_TYPES =
%w[int32 int64 float32 float64].map(&:to_sym)

Instance Method Summary collapse

Instance Method Details

#abs(input, name: nil) ⇒ Object



209
210
211
# File 'lib/tensor_stream/ops.rb', line 209

def abs(input, name: nil)
  _op(:abs, input, nil, name: name)
end

#add(input_a, input_b, name: nil) ⇒ Object



150
151
152
# File 'lib/tensor_stream/ops.rb', line 150

def add(input_a, input_b, name: nil)
  _op(:add, input_a, input_b, name: name)
end

#argmax(input, axis = nil, name: nil, dimension: nil, output_type: :int32) ⇒ Object



7
8
9
# File 'lib/tensor_stream/ops.rb', line 7

def argmax(input, axis = nil, name: nil, dimension: nil, output_type: :int32)
  _op(:argmax, input, nil, axis: axis, name: name, dimension: dimension, data_type: output_type)
end

#cast(input, dtype, name: nil) ⇒ Object



169
170
171
# File 'lib/tensor_stream/ops.rb', line 169

def cast(input, dtype, name: nil)
  _op(:cast, input, nil, data_type: dtype, name: name)
end

#concat(values, axis, name: 'concat') ⇒ Object



121
122
123
# File 'lib/tensor_stream/ops.rb', line 121

def concat(values, axis, name: 'concat')
  _op(:concat, values, nil, axis: axis, name: name)
end

#cond(pred, true_fn, false_fn, name: nil) ⇒ Object



142
143
144
# File 'lib/tensor_stream/ops.rb', line 142

def cond(pred, true_fn, false_fn, name: nil)
  _op(:cond, true_fn, false_fn, pred: pred, name: name)
end

#cos(input, options = {}) ⇒ Object



223
224
225
226
227
# File 'lib/tensor_stream/ops.rb', line 223

def cos(input, options = {})
  options[:data_type] ||= :float32
  check_allowed_types(input, FLOATING_POINT_TYPES)
  _op(:cos, input, nil, options)
end

#equal(input_a, input_b, name: nil) ⇒ Object



181
182
183
# File 'lib/tensor_stream/ops.rb', line 181

def equal(input_a, input_b, name: nil)
  _op(:equal, input_a, input_b, name: name)
end

#exp(input, options = {}) ⇒ Object



262
263
264
265
266
# File 'lib/tensor_stream/ops.rb', line 262

def exp(input, options = {})
  options[:data_type] ||= :float32
  check_allowed_types(input, FLOATING_POINT_TYPES)
  _op(:exp, input, nil, options)
end

#eye(num_rows, num_columns: nil, dtype: :float32, name: nil) ⇒ Object



53
54
55
# File 'lib/tensor_stream/ops.rb', line 53

def eye(num_rows, num_columns: nil, dtype: :float32, name: nil)
  _op(:eye, num_rows, num_columns || num_rows, data_type: dtype, name: name)
end

#glorot_uniform_initializer(seed: nil, dtype: :float32) ⇒ Object



69
70
71
# File 'lib/tensor_stream/ops.rb', line 69

def glorot_uniform_initializer(seed: nil, dtype: :float32)
  TensorStream::Initializer.new(-> { _op(:glorot_uniform, nil, nil, seed: seed, data_type: dtype) })
end

#gradients(input, wrt_xs, grad_ys: nil, name: 'gradients', colocate_gradients_with_ops: false, gate_gradients: false, aggregation_method: nil, stop_gradients: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/tensor_stream/ops.rb', line 11

def gradients(input, wrt_xs, grad_ys: nil,
              name: 'gradients',
              colocate_gradients_with_ops: false,
              gate_gradients: false,
              aggregation_method: nil,
              stop_gradients: nil)

  gs = wrt_xs.collect do |x|
    raise "#{x} passed is not a tensor object" unless x.is_a?(Tensor)

    stops = stop_gradients ? stop_gradients.map(&:name).join('_') : ''
    gradient_program_name = "grad_#{input.name}_#{x.name}_#{stops}".to_sym

    tensor_program = if input.graph.node_added?(gradient_program_name)
                       input.graph.get_node(gradient_program_name)
                     else
                      input.graph.name_scope("gradient_wrt_#{x.name}") do
                        derivative_ops = TensorStream::MathGradients.derivative(input, x, graph: input.graph,
                                                                                          stop_gradients: stop_gradients)
                        unit_matrix = _op(:ones_like, x)
                        input.graph.add_node!(gradient_program_name, unit_matrix * derivative_ops)
                       end
                     end
    tensor_program
  end
  TensorStream.group(gs)
end

#greater(input_a, input_b, name: nil) ⇒ Object



97
98
99
# File 'lib/tensor_stream/ops.rb', line 97

def greater(input_a, input_b, name: nil)
  _op(:greater, input_a, input_b, name: name)
end

#greater_equal(input_a, input_b, name: nil) ⇒ Object



101
102
103
# File 'lib/tensor_stream/ops.rb', line 101

def greater_equal(input_a, input_b, name: nil)
  _op(:greater_equal, input_a, input_b, name: name)
end

#identity(input, name: nil) ⇒ Object



197
198
199
# File 'lib/tensor_stream/ops.rb', line 197

def identity(input, name: nil)
  _op(:identity, input, nil, name: name)
end

#less(input_a, input_b, name: nil) ⇒ Object



89
90
91
# File 'lib/tensor_stream/ops.rb', line 89

def less(input_a, input_b, name: nil)
  _op(:less, input_a, input_b, name: name)
end

#less_equal(input_a, input_b, name: nil) ⇒ Object



105
106
107
# File 'lib/tensor_stream/ops.rb', line 105

def less_equal(input_a, input_b, name: nil)
  _op(:less_equal, input_a, input_b, name: name)
end

#log(input, options = {}) ⇒ Object



250
251
252
253
254
# File 'lib/tensor_stream/ops.rb', line 250

def log(input, options = {})
  options[:data_type] ||= :float32
  check_allowed_types(input, FLOATING_POINT_TYPES)
  _op(:log, input, nil, options)
end

#log1p(input, options = {}) ⇒ Object



256
257
258
259
260
# File 'lib/tensor_stream/ops.rb', line 256

def log1p(input, options = {})
  options[:data_type] ||= :float32
  check_allowed_types(input, FLOATING_POINT_TYPES)
  _op(:log1p, input, nil, options)
end

#logical_and(input_a, input_b, name: nil) ⇒ Object



93
94
95
# File 'lib/tensor_stream/ops.rb', line 93

def logical_and(input_a, input_b, name: nil)
  _op(:logical_and, input_a, input_b, name: name)
end

#matmul(input_a, input_b, transpose_a: false, transpose_b: false, name: nil) ⇒ Object



273
274
275
276
277
# File 'lib/tensor_stream/ops.rb', line 273

def matmul(input_a, input_b, transpose_a: false,
           transpose_b: false,
           name: nil)
  _op(:matmul, input_a, input_b, transpose_a: transpose_a, transpose_b: transpose_b, name: name)
end

#max(input_a, input_b, name: nil) ⇒ Object



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

def max(input_a, input_b, name: nil)
  check_allowed_types(input_a, NUMERIC_TYPES)
  check_allowed_types(input_b, NUMERIC_TYPES)

  _op(:max, input_a, input_b, name: name)
end

#maximum(input_a, input_b, name: nil) ⇒ Object



165
166
167
# File 'lib/tensor_stream/ops.rb', line 165

def maximum(input_a, input_b, name: nil)
  max(input_a, input_b, name: name)
end

#multiply(input_a, input_b, name: nil) ⇒ Object



201
202
203
# File 'lib/tensor_stream/ops.rb', line 201

def multiply(input_a, input_b, name: nil)
  _op(:mul, input_a, input_b, name: name)
end

#negate(input, options = {}) ⇒ Object



177
178
179
# File 'lib/tensor_stream/ops.rb', line 177

def negate(input, options = {})
  _op(:negate, input, nil, options)
end

#not_equal(input_a, input_b, name: nil) ⇒ Object



185
186
187
# File 'lib/tensor_stream/ops.rb', line 185

def not_equal(input_a, input_b, name: nil)
  _op(:not_equal, input_a, input_b, name: name)
end

#ones(shape, dtype: :float32, name: nil) ⇒ Object



85
86
87
# File 'lib/tensor_stream/ops.rb', line 85

def ones(shape, dtype: :float32, name: nil)
  _op(:ones, shape, nil, data_type: dtype, name: name)
end

#ones_like(tensor, dtype: nil, name: nil) ⇒ Object



193
194
195
# File 'lib/tensor_stream/ops.rb', line 193

def ones_like(tensor, dtype: nil, name: nil)
  _op(:ones_like, tensor, nil, data_type: dtype, name: name)
end

#pad(tensor, paddings, mode: 'CONSTANT', name: nil) ⇒ Object



283
284
285
# File 'lib/tensor_stream/ops.rb', line 283

def pad(tensor, paddings, mode: 'CONSTANT', name: nil)
  _op(:pad, tensor, nil, paddings: paddings, mode: mode, name: name)
end

#pow(input_a, input_e, name: nil) ⇒ Object



205
206
207
# File 'lib/tensor_stream/ops.rb', line 205

def pow(input_a, input_e, name: nil)
  _op(:pow, input_a, input_e, name: name)
end


173
174
175
# File 'lib/tensor_stream/ops.rb', line 173

def print(input, data, message: nil, name: nil)
  _op(:print, input, data, message: message, name: name)
end

#random_normal(shape, dtype: :float32, mean: 0.0, stddev: 1.0, seed: nil, name: nil) ⇒ Object



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

def random_normal(shape, dtype: :float32, mean: 0.0, stddev: 1.0, seed: nil, name: nil)
  options = { shape: shape, dtype: dtype, mean: mean, stddev: stddev, seed: seed, name: name }
  _op(:random_normal, nil, nil, options)
end

#random_uniform(shape, dtype: :float32, minval: 0, maxval: 1, seed: nil, name: nil) ⇒ Object



39
40
41
42
# File 'lib/tensor_stream/ops.rb', line 39

def random_uniform(shape, dtype: :float32, minval: 0, maxval: 1, seed: nil, name: nil)
  options = { shape: shape, dtype: dtype, minval: minval, maxval: maxval, seed: seed, name: name }
  _op(:random_uniform, nil, nil, options)
end

#random_uniform_initializer(minval: 0, maxval: 1, seed: nil, dtype: nil) ⇒ Object



73
74
75
# File 'lib/tensor_stream/ops.rb', line 73

def random_uniform_initializer(minval: 0, maxval: 1, seed: nil, dtype: nil)
  TensorStream::Initializer.new(-> { _op(:random_uniform, nil, nil, minval: 0, maxval: 1, seed: seed, data_type: dtype) })
end

#rank(input, name: nil) ⇒ Object



61
62
63
# File 'lib/tensor_stream/ops.rb', line 61

def rank(input, name: nil)
  _op(:rank, input, name: name)
end

#reciprocal(tensor, name: nil) ⇒ Object



138
139
140
# File 'lib/tensor_stream/ops.rb', line 138

def reciprocal(tensor, name: nil)
  _op(:reciprocal, tensor, nil, name: name)
end

#reduce_mean(input_tensor, axis = nil, keepdims: false, name: nil) ⇒ Object



109
110
111
# File 'lib/tensor_stream/ops.rb', line 109

def reduce_mean(input_tensor, axis = nil, keepdims: false, name: nil)
  _op(:reduce_mean, input_tensor, nil, axis: axis, keepdims: keepdims, name: name)
end

#reduce_prod(input, axis = nil, keepdims: false, name: nil) ⇒ Object



117
118
119
# File 'lib/tensor_stream/ops.rb', line 117

def reduce_prod(input, axis = nil, keepdims: false, name: nil)
  _op(:reduce_prod, input, nil, axis: axis, keepdims: keepdims, name: name)
end

#reduce_sum(input_tensor, axis = nil, keepdims: false, name: nil) ⇒ Object



113
114
115
# File 'lib/tensor_stream/ops.rb', line 113

def reduce_sum(input_tensor, axis = nil, keepdims: false, name: nil)
  _op(:reduce_sum, input_tensor, nil, axis: axis, keepdims: keepdims, name: name)
end

#reshape(tensor, shape, name: nil) ⇒ Object



125
126
127
# File 'lib/tensor_stream/ops.rb', line 125

def reshape(tensor, shape, name: nil)
  _op(:reshape, tensor, shape, name: name)
end

#round(tensor, name: nil) ⇒ Object



133
134
135
136
# File 'lib/tensor_stream/ops.rb', line 133

def round(tensor, name: nil)
  check_allowed_types(tensor, FLOATING_POINT_TYPES)
  _op(:round, tensor, nil, name: name)
end

#shape(input, name: nil, out_type: :int32) ⇒ Object



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

def shape(input, name: nil, out_type: :int32)
  _op(:shape, input, nil, name: name, out_type: out_type)
end

#sigmoid(input, name: nil) ⇒ Object



268
269
270
271
# File 'lib/tensor_stream/ops.rb', line 268

def sigmoid(input, name: nil)
  check_allowed_types(input, FLOATING_POINT_TYPES)
  _op(:sigmoid, input, nil, name: name)
end

#sign(input, name: nil) ⇒ Object



213
214
215
# File 'lib/tensor_stream/ops.rb', line 213

def sign(input, name: nil)
  _op(:sign, input, nil, name: name)
end

#sin(input, options = {}) ⇒ Object



217
218
219
220
221
# File 'lib/tensor_stream/ops.rb', line 217

def sin(input, options = {})
  options[:data_type] ||= :float32
  check_allowed_types(input, FLOATING_POINT_TYPES)
  _op(:sin, input, nil, options)
end

#slice(input, start, size, name: nil) ⇒ Object



77
78
79
# File 'lib/tensor_stream/ops.rb', line 77

def slice(input, start, size, name: nil)
  _op(:slice, input, start, size: size, name: name)
end

#sqrt(input, name: nil) ⇒ Object



241
242
243
244
245
246
247
248
# File 'lib/tensor_stream/ops.rb', line 241

def sqrt(input, name: nil)
  options = {
    data_type: input.data_type,
    name: name
  }
  check_allowed_types(input, FLOATING_POINT_TYPES)
  _op(:sqrt, input, nil, options)
end

#square(tensor, name: nil) ⇒ Object



129
130
131
# File 'lib/tensor_stream/ops.rb', line 129

def square(tensor, name: nil)
  _op(:square, tensor, nil, name: name)
end

#stop_gradient(tensor, options = {}) ⇒ Object



49
50
51
# File 'lib/tensor_stream/ops.rb', line 49

def stop_gradient(tensor, options = {})
  _op(:stop_gradient, tensor, nil, options)
end

#sub(input_a, input_b, name: nil) ⇒ Object



154
155
156
# File 'lib/tensor_stream/ops.rb', line 154

def sub(input_a, input_b, name: nil)
  _op(:sub, input_a, input_b, name: name)
end

#tan(input, options = {}) ⇒ Object



229
230
231
232
233
# File 'lib/tensor_stream/ops.rb', line 229

def tan(input, options = {})
  options[:data_type] ||= :float32
  check_allowed_types(input, FLOATING_POINT_TYPES)
  _op(:tan, input, nil, options)
end

#tanh(input, options = {}) ⇒ Object



235
236
237
238
239
# File 'lib/tensor_stream/ops.rb', line 235

def tanh(input, options = {})
  options[:data_type] ||= :float32
  check_allowed_types(input, FLOATING_POINT_TYPES)
  _op(:tanh, input, nil, options)
end

#transpose(tensor, perm: nil, name: 'transpose') ⇒ Object



279
280
281
# File 'lib/tensor_stream/ops.rb', line 279

def transpose(tensor, perm: nil, name: 'transpose')
  _op(:transpose, tensor, nil, perm: perm, name: name)
end

#where(condition, true_t = nil, false_t = nil, name: nil) ⇒ Object



146
147
148
# File 'lib/tensor_stream/ops.rb', line 146

def where(condition, true_t = nil, false_t = nil, name: nil)
  _op(:where, true_t, false_t, pred: condition, name: name)
end

#zeros(shape, dtype: :float32, name: nil) ⇒ Object



81
82
83
# File 'lib/tensor_stream/ops.rb', line 81

def zeros(shape, dtype: :float32, name: nil)
  _op(:zeros, shape, nil, data_type: dtype, name: name)
end

#zeros_initializer(options = {}) ⇒ Object



65
66
67
# File 'lib/tensor_stream/ops.rb', line 65

def zeros_initializer(options = {})
  _op(:zeros, nil, nil, options)
end

#zeros_like(tensor, dtype: nil, name: nil) ⇒ Object



189
190
191
# File 'lib/tensor_stream/ops.rb', line 189

def zeros_like(tensor, dtype: nil, name: nil)
  _op(:zeros_like, tensor, nil, data_type: dtype, name: name)
end