Class: TensorStream::Operation
- Defined in:
- lib/tensor_stream/operation.rb
Overview
TensorStream class that defines an operation
Direct Known Subclasses
Instance Attribute Summary collapse
-
#items ⇒ Object
Returns the value of attribute items.
-
#name ⇒ Object
Returns the value of attribute name.
-
#operation ⇒ Object
Returns the value of attribute operation.
-
#options ⇒ Object
Returns the value of attribute options.
-
#rank ⇒ Object
Returns the value of attribute rank.
Attributes inherited from Tensor
#breakpoint, #data_type, #given_name, #graph, #internal, #is_const, #native_buffer, #shape, #source, #value
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(operation, input_a, input_b, options = {}) ⇒ Operation
constructor
A new instance of Operation.
- #run ⇒ Object
- #set_data_type(passed_data_type) ⇒ Object
- #to_h ⇒ Object
- #to_math(name_only = false, max_depth = 99) ⇒ Object
- #to_s ⇒ Object
Methods inherited from Tensor
#!=, #*, #**, #+, #-, #-@, #/, #<, #<=, #==, #>, #>=, #[], #auto_math, #breakpoint!, cast_dtype, #collect, detect_type, #dtype, #eval, #first, #internal?, reset_counters, #to_a, #to_f, #to_i
Methods included from OpHelper
#cons, #dtype_eval, #fp_type?, #i_cons, #i_op, #op, #shape_eval, #val_to_dtype
Constructor Details
#initialize(operation, input_a, input_b, options = {}) ⇒ Operation
6 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 6 def initialize(operation, input_a, input_b, = {}) @graph = [:graph] || TensorStream.get_default_graph @operation = operation @rank = [:rank] || 0 @name = [:name] || set_name @internal = [:internal] @given_name = @name @source = format_source(caller_locations) @options = @items = [input_a, input_b].map { |i| [:preserve_params_type] ? i : auto_wrap(i) } @data_type = set_data_type([:data_type]) @shape = TensorShape.new([:shape], [:shape].size || 0) if [:shape] @graph.add_node(self) end |
Instance Attribute Details
#items ⇒ Object
Returns the value of attribute items.
4 5 6 |
# File 'lib/tensor_stream/operation.rb', line 4 def items @items end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/tensor_stream/operation.rb', line 4 def name @name end |
#operation ⇒ Object
Returns the value of attribute operation.
4 5 6 |
# File 'lib/tensor_stream/operation.rb', line 4 def operation @operation end |
#options ⇒ Object
Returns the value of attribute options.
4 5 6 |
# File 'lib/tensor_stream/operation.rb', line 4 def @options end |
#rank ⇒ Object
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
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
#run ⇒ Object
168 169 170 |
# File 'lib/tensor_stream/operation.rb', line 168 def run eval end |
#set_data_type(passed_data_type) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/tensor_stream/operation.rb', line 52 def set_data_type(passed_data_type) case operation when :greater, :less, :equal :boolean when :shape, :rank :int32 else passed_data_type || (@items[0] ? @items[0].data_type : :unknown) end end |
#to_h ⇒ Object
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) ⇒ Object
63 64 65 66 67 68 69 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 |
# File 'lib/tensor_stream/operation.rb', line 63 def to_math(name_only = false, max_depth = 99) return @name if max_depth.zero? sub_item = auto_math(items[0], name_only, max_depth - 1) case operation when :argmax "argmax(#{auto_math(items[0])},#{[:axis]})" when :negate "-#{sub_item}" when :index "#{sub_item}[#{auto_math(items[1], name_only, max_depth - 1)}]" when :slice "#{sub_item}[#{auto_math(items[1], name_only, max_depth - 1)}]" when :assign_sub "(#{items[0] ? items[0].name : 'self'} -= #{auto_math(items[1], name_only)})" when :assign_add "(#{items[0] ? items[0].name : 'self'} += #{auto_math(items[1], name_only)})" when :assign "(#{items[0] ? items[0].name : 'self'} = #{auto_math(items[1], name_only)})" when :sin, :cos, :tanh "#{operation}(#{sub_item})" when :add "(#{sub_item} + #{auto_math(items[1], name_only, max_depth - 1)})" when :sub "(#{sub_item} - #{auto_math(items[1], name_only, max_depth - 1)})" when :pow "(#{sub_item}^#{auto_math(items[1], name_only, max_depth - 1)})" when :div "(#{sub_item} / #{auto_math(items[1], name_only, max_depth - 1)})" when :mul if auto_math(items[0]) == 1 auto_math(items[1], name_only, max_depth - 1) elsif auto_math(items[1]) == 1 sub_item else "(#{sub_item} * #{auto_math(items[1], name_only, max_depth - 1)})" 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(#{auto_math(items[1], name_only, max_depth - 1)})" 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},#{auto_math(items[1], name_only, max_depth - 1)})" when :rank "#{sub_item}.rank" when :cond "(#{auto_math([:pred])} ? #{sub_item} : #{auto_math(items[1], name_only, max_depth - 1)})" when :less "#{sub_item} < #{auto_math(items[1], name_only, max_depth - 1)}" when :greater "#{sub_item} > #{auto_math(items[1], name_only, max_depth - 1)}" 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([:paddings])})" when :equal "#{sub_item} == #{auto_math(items[1], name_only, max_depth - 1)}" when :not_equal "#{sub_item} != #{auto_math(items[1], name_only, max_depth - 1)}" when :sqrt "sqrt(#{sub_item})" when :zeros_like "zeros_like(#{sub_item})" when :where "where(#{auto_math([:pred], name_only, max_depth - 1)},#{auto_math(items[0])},#{auto_math(items[1])})" when :max "max(#{auto_math(sub_item)},#{auto_math(items[1])})" when :cast "cast(#{auto_math(sub_item)}, #{data_type})" else raise "math form for #{operation}" end end |
#to_s ⇒ Object
26 27 28 |
# File 'lib/tensor_stream/operation.rb', line 26 def to_s @name end |