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



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

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

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



127
128
129
# File 'lib/tensor_stream/ops.rb', line 127

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



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

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

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



107
108
109
# File 'lib/tensor_stream/ops.rb', line 107

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

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



119
120
121
# File 'lib/tensor_stream/ops.rb', line 119

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

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



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

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



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

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

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



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

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



51
52
53
# File 'lib/tensor_stream/ops.rb', line 51

def eye(num_rows, num_columns: nil, dtype: :float32, name: nil)
  op(:eye, num_rows, num_columns || num_rows, data_type: dtype, name: name, preserve_params_type: true)
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
# 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
                       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
    tensor_program
  end
  TensorStream.group(gs)
end

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



83
84
85
# File 'lib/tensor_stream/ops.rb', line 83

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



87
88
89
# File 'lib/tensor_stream/ops.rb', line 87

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

#identity(input, name: nil) ⇒ Object



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

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

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



79
80
81
# File 'lib/tensor_stream/ops.rb', line 79

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



91
92
93
# File 'lib/tensor_stream/ops.rb', line 91

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

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



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

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

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



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

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



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

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

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



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

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

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



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

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

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



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

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



75
76
77
# File 'lib/tensor_stream/ops.rb', line 75

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



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

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



245
246
247
# File 'lib/tensor_stream/ops.rb', line 245

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



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

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


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

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



42
43
44
45
# File 'lib/tensor_stream/ops.rb', line 42

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



37
38
39
40
# File 'lib/tensor_stream/ops.rb', line 37

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

#rank(input, name: nil) ⇒ Object



59
60
61
# File 'lib/tensor_stream/ops.rb', line 59

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

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



95
96
97
# File 'lib/tensor_stream/ops.rb', line 95

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



103
104
105
# File 'lib/tensor_stream/ops.rb', line 103

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



99
100
101
# File 'lib/tensor_stream/ops.rb', line 99

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



111
112
113
# File 'lib/tensor_stream/ops.rb', line 111

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

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



55
56
57
# File 'lib/tensor_stream/ops.rb', line 55

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

#sign(input, name: nil) ⇒ Object



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

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

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



190
191
192
193
194
# File 'lib/tensor_stream/ops.rb', line 190

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



67
68
69
# File 'lib/tensor_stream/ops.rb', line 67

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

#sqrt(input, name: nil) ⇒ Object



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

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



115
116
117
# File 'lib/tensor_stream/ops.rb', line 115

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

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



47
48
49
# File 'lib/tensor_stream/ops.rb', line 47

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

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



131
132
133
# File 'lib/tensor_stream/ops.rb', line 131

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

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



202
203
204
205
206
# File 'lib/tensor_stream/ops.rb', line 202

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



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

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



241
242
243
# File 'lib/tensor_stream/ops.rb', line 241

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



123
124
125
# File 'lib/tensor_stream/ops.rb', line 123

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



71
72
73
# File 'lib/tensor_stream/ops.rb', line 71

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

#zeros_initializer(options = {}) ⇒ Object



63
64
65
# File 'lib/tensor_stream/ops.rb', line 63

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

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



162
163
164
# File 'lib/tensor_stream/ops.rb', line 162

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