Class: TensorStream::Tensor

Inherits:
Object
  • Object
show all
Includes:
OpHelper
Defined in:
lib/tensor_stream/tensor.rb

Overview

Base class that defines a tensor like interface

Direct Known Subclasses

Operation, Placeholder, Variable

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OpHelper

#_op, #cons, #dtype_eval, #format_source, #fp_type?, #i_cons, #i_op, #shape_eval, #val_to_dtype

Constructor Details

#initialize(data_type, rank, shape, options = {}) ⇒ Tensor

Returns a new instance of Tensor.



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
38
39
40
41
# File 'lib/tensor_stream/tensor.rb', line 12

def initialize(data_type, rank, shape, options = {})
  setup_initial_state(options)
  @data_type = data_type
  @rank = rank
  @breakpoint = false
  @shape = TensorShape.new(shape, rank)
  @value = nil

  @is_const = options[:const] || false
  @internal = options[:internal]
  @name = [@graph.get_name_scope, options[:name] || build_name].compact.reject(&:empty?).join('/')
  @given_name = @name

  if options[:value]
    if options[:value].is_a?(Array)
      # check if single dimenstion array is passed
      options[:value] = reshape(options[:value], shape.reverse.dup) if shape.size >= 2 && !options[:value].empty? && !options[:value][0].is_a?(Array)

      @value = options[:value].collect do |v|
        v.is_a?(Tensor) ? Tensor.cast_dtype(v, @data_type) : v
      end
    elsif !shape.empty?
      @value = reshape(Tensor.cast_dtype(options[:value], @data_type), shape.dup)
    else
      @value = Tensor.cast_dtype(options[:value], @data_type)
    end
  end

  @graph.add_node(self)
end

Instance Attribute Details

#breakpointObject

Returns the value of attribute breakpoint.



8
9
10
# File 'lib/tensor_stream/tensor.rb', line 8

def breakpoint
  @breakpoint
end

#consumersObject

Returns the value of attribute consumers.



8
9
10
# File 'lib/tensor_stream/tensor.rb', line 8

def consumers
  @consumers
end

#data_typeObject

Returns the value of attribute data_type.



8
9
10
# File 'lib/tensor_stream/tensor.rb', line 8

def data_type
  @data_type
end

#deviceObject

Returns the value of attribute device.



8
9
10
# File 'lib/tensor_stream/tensor.rb', line 8

def device
  @device
end

#given_nameObject

Returns the value of attribute given_name.



8
9
10
# File 'lib/tensor_stream/tensor.rb', line 8

def given_name
  @given_name
end

#graphObject

Returns the value of attribute graph.



8
9
10
# File 'lib/tensor_stream/tensor.rb', line 8

def graph
  @graph
end

#internalObject

Returns the value of attribute internal.



8
9
10
# File 'lib/tensor_stream/tensor.rb', line 8

def internal
  @internal
end

#is_constObject

Returns the value of attribute is_const.



8
9
10
# File 'lib/tensor_stream/tensor.rb', line 8

def is_const
  @is_const
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/tensor_stream/tensor.rb', line 8

def name
  @name
end

#native_bufferObject

Returns the value of attribute native_buffer.



8
9
10
# File 'lib/tensor_stream/tensor.rb', line 8

def native_buffer
  @native_buffer
end

#outputsObject

Returns the value of attribute outputs.



8
9
10
# File 'lib/tensor_stream/tensor.rb', line 8

def outputs
  @outputs
end

#rankObject

Returns the value of attribute rank.



8
9
10
# File 'lib/tensor_stream/tensor.rb', line 8

def rank
  @rank
end

#shapeObject

Returns the value of attribute shape.



8
9
10
# File 'lib/tensor_stream/tensor.rb', line 8

def shape
  @shape
end

#sourceObject

Returns the value of attribute source.



8
9
10
# File 'lib/tensor_stream/tensor.rb', line 8

def source
  @source
end

#valueObject

Returns the value of attribute value.



8
9
10
# File 'lib/tensor_stream/tensor.rb', line 8

def value
  @value
end

Class Method Details

.cast_dtype(val, dtype) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/tensor_stream/tensor.rb', line 207

def self.cast_dtype(val, dtype)
  return val if dtype.nil?
  return val if val.is_a?(Tensor)

  if val.is_a?(Array)
    return val.collect do |v|
      cast_dtype(v, dtype)
    end
  end

  if dtype.is_a?(Hash)
    dtype = dtype[:dtype]
  end

  case dtype.to_sym
  when :float64, :float32, :float
    if !!val == val
      val ? 1.0 : 0.0
    else
      val.to_f
    end
  when :string
    val.to_s
  when :int32, :int16, :int
    if !!val == val
      val ? 1 : 0
    else
      val.to_i
    end
  when :boolean
    !!val
  when :unknown
    val
  else
    raise "unknown data_type #{dtype} passed"
  end
end

.detect_type(value) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/tensor_stream/tensor.rb', line 191

def self.detect_type(value)
  if !!value==value
    :boolean
  elsif value.is_a?(String)
    :string
  elsif value.is_a?(Float)
    :float32
  elsif value.is_a?(Integer)
    :int32
  elsif value.is_a?(Array)
    return detect_type(value[0])
  else
    :float32
  end
end

.reset_countersObject



51
52
53
54
55
# File 'lib/tensor_stream/tensor.rb', line 51

def self.reset_counters
  @const_counter = 0
  @var_counter = 0
  @placeholder_counter = 0
end

Instance Method Details

#!=(other) ⇒ Object



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

def !=(other)
  _a, other = TensorStream.check_data_types(self, other)
  _op(:not_equal, self, other)
end

#*(other) ⇒ Object



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

def *(other)
  _a, other = TensorStream.check_data_types(self, other)
  _op(:mul, self, TensorStream.convert_to_tensor(other, dtype: data_type))
end

#**(other) ⇒ Object



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

def **(other)
  _a, other = TensorStream.check_data_types(self, other)
  _op(:pow, self, TensorStream.convert_to_tensor(other, dtype: data_type))
end

#+(other) ⇒ Object



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

def +(other)
  _a, other = TensorStream.check_data_types(self, other)
  _op(:add, self, other)
end

#-(other) ⇒ Object



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

def -(other)
  _a, other = TensorStream.check_data_types(self, other)
  _op(:sub, self, TensorStream.convert_to_tensor(other, dtype: data_type))
end

#-@Object



86
87
88
# File 'lib/tensor_stream/tensor.rb', line 86

def -@
  _op(:negate, self, nil)
end

#/(other) ⇒ Object



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

def /(other)
  _a, other = TensorStream.check_data_types(self, other)
  _op(:div, self, TensorStream.convert_to_tensor(other, dtype: data_type))
end

#<(other) ⇒ Object



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

def <(other)
  _a, other = TensorStream.check_data_types(self, other)
  _op(:less, self, other)
end

#<=(other) ⇒ Object



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

def <=(other)
  _a, other = TensorStream.check_data_types(self, other)
  _op(:less_equal, self, other)
end

#==(other) ⇒ Object



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

def ==(other)
  _a, other = TensorStream.check_data_types(self, other)
  _op(:equal, self, other)
end

#>(other) ⇒ Object



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

def >(other)
  _a, other = TensorStream.check_data_types(self, other)
  _op(:greater, self, other)
end

#>=(other) ⇒ Object



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

def >=(other)
  _a, other = TensorStream.check_data_types(self, other)
  _op(:greater_equal, self, other)
end

#[](index) ⇒ Object



62
63
64
# File 'lib/tensor_stream/tensor.rb', line 62

def [](index)
  _op(:index, self, index)
end

#and(other) ⇒ Object



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

def and(other)
  _a, other = TensorStream.check_data_types(self, other)
  _op(:logical_and, self, other)
end

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



187
188
189
# File 'lib/tensor_stream/tensor.rb', line 187

def auto_math(tensor, name_only = false, max_depth = 99, _cur_depth = 0)
  tensor.is_a?(Tensor) ? tensor.to_math(name_only, max_depth, _cur_depth) : tensor
end

#breakpoint!(&block) ⇒ Object



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

def breakpoint!(&block)
  self
end

#collect(&block) ⇒ Object



135
136
137
# File 'lib/tensor_stream/tensor.rb', line 135

def collect(&block)
  @value.collect(&block)
end

#dot(other) ⇒ Object



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

def dot(other)
  _a, other = TensorStream.check_data_types(self, other)
  _op(:matmul, self, other)
end

#dtypeObject



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

def dtype
  @data_type
end

#eval(options = {}) ⇒ Object



147
148
149
# File 'lib/tensor_stream/tensor.rb', line 147

def eval(options = {})
  Session.default_session.run(self, options)
end

#firstObject



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

def first
  _op(:index, self, 0)
end

#internal?Boolean

Returns:

  • (Boolean)


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

def internal?
  !!@internal
end

#matmul(other) ⇒ Object



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

def matmul(other)
  _a, other = TensorStream.check_data_types(self, other)
  _op(:matmul, self, other)
end

#opObject



143
144
145
# File 'lib/tensor_stream/tensor.rb', line 143

def op
  is_const ? _op(:const, self, nil, name: self.name) : _op(:variable, self, nil, name: self.name)
end

#print!(message) ⇒ Object



249
250
251
# File 'lib/tensor_stream/tensor.rb', line 249

def print!(message)
  _op(:print, self, self, message: message)
end

#to_aObject



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

def to_a
  @value
end

#to_fObject



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

def to_f
  @value
end

#to_hObject



151
152
153
154
155
156
157
158
159
# File 'lib/tensor_stream/tensor.rb', line 151

def to_h
  {
    name: @name,
    value: hashify_tensor(@value),
    dtype: @data_type,
    shape: @shape,
    const: !!is_const,
  }
end

#to_iObject



161
162
163
# File 'lib/tensor_stream/tensor.rb', line 161

def to_i
  @value
end

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



177
178
179
180
181
182
183
184
185
# File 'lib/tensor_stream/tensor.rb', line 177

def to_math(name_only = false, max_depth = 99, _unused = 0)
  return @name if max_depth.zero? || name_only || @value.nil?

  if @value.is_a?(Array)
    @value.collect { |v| v.is_a?(Tensor) ? v.to_math(name_only, max_depth - 1) : v }
  else
    is_const ? @value : @name
  end
end

#to_sObject



139
140
141
# File 'lib/tensor_stream/tensor.rb', line 139

def to_s
  @name
end