Class: TensorStream::Tensor

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

Direct Known Subclasses

Operation, Placeholder, Variable

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OpHelper

#cons, #dtype_eval, #i_cons, #i_op, #op, #shape_eval, #val_to_dtype

Constructor Details

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/tensor_stream/tensor.rb', line 39

def initialize(data_type, rank, shape, options = {})
  @data_type = data_type
  @rank = rank
  @breakpoint = false
  @shape = TensorShape.new(shape, rank)
  @value = nil
  @source = set_source(caller_locations)
  @is_const = options[:const] || false
  @internal = options[:internal]
  @graph = options[:graph] || TensorStream.get_default_graph
  @name = options[:name] || build_name
  @given_name = @name

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

      @value = options[:value].collect do |v|
        v.kind_of?(Tensor) ? Tensor.cast_dtype(v, data_type) : v
      end
    elsif shape.size > 0
      @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.



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

def breakpoint
  @breakpoint
end

#data_typeObject

Returns the value of attribute data_type.



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

def data_type
  @data_type
end

#given_nameObject

Returns the value of attribute given_name.



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

def given_name
  @given_name
end

#graphObject

Returns the value of attribute graph.



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

def graph
  @graph
end

#internalObject

Returns the value of attribute internal.



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

def internal
  @internal
end

#is_constObject

Returns the value of attribute is_const.



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

def is_const
  @is_const
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#native_bufferObject

Returns the value of attribute native_buffer.



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

def native_buffer
  @native_buffer
end

#rankObject

Returns the value of attribute rank.



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

def rank
  @rank
end

#shapeObject

Returns the value of attribute shape.



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

def shape
  @shape
end

#sourceObject

Returns the value of attribute source.



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

def source
  @source
end

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Class Method Details

.cast_dtype(val, dtype) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/tensor_stream/tensor.rb', line 231

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

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

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

.const_nameObject



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/tensor_stream/tensor.rb', line 9

def self.const_name
  @const_counter ||= 0

  name = if @const_counter == 0
    ""
  else
    "_#{@const_counter}"
  end

  @const_counter += 1
  
  name
end

.detect_type(value) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/tensor_stream/tensor.rb', line 217

def self.detect_type(value)
  dtype = 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

.placeholder_nameObject



31
32
33
34
35
36
37
# File 'lib/tensor_stream/tensor.rb', line 31

def self.placeholder_name
  @placeholder_counter ||= 0
  @placeholder_counter += 1

  return "" if @placeholder_counter == 1
  return "_#{@placeholder_counter}"
end

.reset_countersObject



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

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

.var_nameObject



23
24
25
26
27
28
29
# File 'lib/tensor_stream/tensor.rb', line 23

def self.var_name
  @var_counter ||= 0
  @var_counter += 1

  return "" if @var_counter == 1
  return "_#{@var_counter}"
end

Instance Method Details

#!=(operand) ⇒ Object



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

def !=(operand)
  op(:not_equal, self, operand)
end

#*(operand) ⇒ Object



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

def *(operand)
  TensorStream::Operation.new(:mul, self, auto_wrap(operand))
end

#**(operand) ⇒ Object



108
109
110
# File 'lib/tensor_stream/tensor.rb', line 108

def **(operand)
  TensorStream::Operation.new(:pow, self, auto_wrap(operand))
end

#+(operand) ⇒ Object



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

def +(operand)
  TensorStream::Operation.new(:add, self, auto_wrap(operand))
end

#-(operand) ⇒ Object



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

def -(operand)
  TensorStream::Operation.new(:sub, self, auto_wrap(operand))
end

#-@Object



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

def -@
  TensorStream::Operation.new(:negate, self, nil)
end

#/(operand) ⇒ Object



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

def /(operand)
  TensorStream::Operation.new(:div, self, auto_wrap(operand))
end

#<(operand) ⇒ Object



128
129
130
# File 'lib/tensor_stream/tensor.rb', line 128

def <(operand)
  op(:less, self, operand)
end

#<=(operand) ⇒ Object



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

def <=(operand)
  op(:less_equal, self, operand)
end

#==(operand) ⇒ Object



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

def ==(operand)
  op(:equal, self, operand)
end

#>(operand) ⇒ Object



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

def >(operand)
  op(:greater, self, operand)
end

#>=(operand) ⇒ Object



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

def >=(operand)
  op(:greater_equal, self, operand)
end

#[](index) ⇒ Object



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

def [](index)
  TensorStream::Operation.new(:index, self, index)
end

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



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

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

#breakpoint!(&block) ⇒ Object



264
265
266
267
# File 'lib/tensor_stream/tensor.rb', line 264

def breakpoint!(&block)
  @breakpoint = block
  self
end

#build_bufferObject



86
87
88
89
90
91
92
93
94
# File 'lib/tensor_stream/tensor.rb', line 86

def build_buffer
  @native_buffer = if @data_type == :float32 && @rank == 2
    NArray.sfloat(@shape.cols * @shape.rows)
  elsif @data_type == :float32 && @rank == 0
    NArray.sfloat(1)
  else
    raise "Invalid data type #{@data_type}"
  end
end

#collect(&block) ⇒ Object



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

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

#dtypeObject



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

def dtype
  @data_type
end

#eval(options = {}) ⇒ Object



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

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

#firstObject



199
200
201
# File 'lib/tensor_stream/tensor.rb', line 199

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

#internal?Boolean



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

def internal?
  !!@internal
end

#open_cl_buffer(context) ⇒ Object

open cl methods



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

def open_cl_buffer(context)
  @cl_buffer ||= context.create_buffer(@native_buffer.size * @native_buffer.element_size, :flags => OpenCL::Mem::COPY_HOST_PTR, :host_ptr => @native_buffer)
end

#sync_cl_buffer(queue, events = []) ⇒ Object



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

def sync_cl_buffer(queue, events = [])
  queue.enqueue_read_buffer(@cl_buffer, @native_buffer, :event_wait_list => events)
end

#to_aObject



191
192
193
# File 'lib/tensor_stream/tensor.rb', line 191

def to_a
  @value
end

#to_fObject



195
196
197
# File 'lib/tensor_stream/tensor.rb', line 195

def to_f
  @value
end

#to_hObject



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

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

#to_iObject



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

def to_i
  @value
end

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



203
204
205
206
207
208
209
210
211
# File 'lib/tensor_stream/tensor.rb', line 203

def to_math(name_only = false, max_depth = 99)
  return @name if max_depth==0 || name_only || @value.nil?
  
  if @value.kind_of?(Array)
    @value.collect { |v| v.kind_of?(Tensor) ? v.to_math(name_only, max_depth - 1) : v }
  else
    is_const ? @value : @name
  end
end

#to_sObject



152
153
154
# File 'lib/tensor_stream/tensor.rb', line 152

def to_s
  @name
end