Module: TensorStream::OpHelper

Included in:
TensorStream, Debugging, Evaluator::OpenclEvaluator, Evaluator::RubyEvaluator, MathGradients, NN, Pbtext, Tensor
Defined in:
lib/tensor_stream/helpers/op_helper.rb

Overview

module that contains helper functions useful for ops

Instance Method Summary collapse

Instance Method Details

#_op(code, t_a, t_b = nil, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/tensor_stream/helpers/op_helper.rb', line 4

def _op(code, t_a, t_b = nil, options = {})
  op = Operation.new(code.to_sym, t_a, t_b, options)
  if !TensorStream.get_default_graph.get_dependency_scope.nil?
    i_op(:identity, op, TensorStream.get_default_graph.get_dependency_scope, name: [op.name, 'tuple', 'control_dependency'].join('/'))
  else
    op
  end
end

#cons(value, options = {}) ⇒ Object



18
19
20
# File 'lib/tensor_stream/helpers/op_helper.rb', line 18

def cons(value, options = {})
  TensorStream.constant(value, options)
end

#dtype_eval(rank, value, data_type = nil) ⇒ Object



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

def dtype_eval(rank, value, data_type = nil)
  dtype = if data_type.nil?
    Tensor.detect_type(value[0])
  else
   data_type
  end

  rank += 1 if dtype == :array

  [dtype, rank, value[0], value.size]
end

#format_source(trace) ⇒ Object



71
72
73
74
75
# File 'lib/tensor_stream/helpers/op_helper.rb', line 71

def format_source(trace)
  grad_source = trace.select { |c| c.to_s.include?(File.join('lib', 'tensor_stream', 'math_gradients')) }.first
  source = trace.reject { |c| c.to_s.include?(File.join('lib', 'tensor_stream')) }.first
  [grad_source, source].compact.join("\n")
end

#fp_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


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

def fp_type?(type)
  TensorStream::Ops::FLOATING_POINT_TYPES.include?(type)
end

#i_cons(value, options = {}) ⇒ Object



22
23
24
# File 'lib/tensor_stream/helpers/op_helper.rb', line 22

def i_cons(value, options = {})
  TensorStream.constant(value, options.merge(internal: true))
end

#i_op(code, t_a, t_b = nil, options = {}) ⇒ Object

same as op but with a marker that it was internal generated



14
15
16
# File 'lib/tensor_stream/helpers/op_helper.rb', line 14

def i_op(code, t_a, t_b = nil, options = {})
  Operation.new(code.to_sym, t_a, t_b, options.merge(internal: true))
end

#shape_eval(input, output_type = :int32) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tensor_stream/helpers/op_helper.rb', line 26

def shape_eval(input, output_type = :int32)
  return [] unless input.is_a?(Array)
  arr = []
  arr_ptr = input

  Kernel.loop do
    arr << (TensorStream::Ops::FLOATING_POINT_TYPES.include?(output_type) ? arr_ptr.size.to_f : arr_ptr.size)
    arr_ptr = arr_ptr[0]

    break unless arr_ptr.is_a?(Array)
  end

  arr
end

#val_to_dtype(value) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/tensor_stream/helpers/op_helper.rb', line 53

def val_to_dtype(value)
  if value.is_a?(String)
    :string
  elsif value.is_a?(Float)
    :float32
  elsif value.is_a?(Integer)
    :int32
  elsif value.is_a?(Array)
    :array
  else
    :float32
  end
end