class TensorStream::Tensor

Base class that defines a tensor like interface

Attributes

data_type[RW]
given_name[RW]
graph[R]
internal[RW]
is_const[RW]
name[RW]
native_buffer[RW]
op[RW]
outputs[RW]
rank[RW]
shape[RW]
source[RW]
value[R]

Public Class Methods

cast_dtype(val, dtype) click to toggle source
# File lib/tensor_stream/tensor.rb, line 104
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

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

  case dtype.to_sym
  when :float64, :float32, :float16, :float
    if !!val == val
      val ? 1.0 : 0.0
    else
      val.to_f
    end
  when :string
    val.to_s
  when :uint32, :int32, :uint64, :uint16, :int16, :int64, :uint8, :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) click to toggle source
# File lib/tensor_stream/tensor.rb, line 86
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)
    detect_type(value[0])
  elsif value.is_a?(Tensor)
    value.data_type
  else
    :float32
  end
end
reset_counters() click to toggle source
# File lib/tensor_stream/tensor.rb, line 29
def self.reset_counters
  @const_counter = 0
  @var_counter = 0
  @placeholder_counter = 0
end

Public Instance Methods

auto_math(tensor, name_only = false, max_depth = 99, cur_depth = 0) click to toggle source
# File lib/tensor_stream/tensor.rb, line 82
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) click to toggle source
# File lib/tensor_stream/tensor.rb, line 140
def breakpoint!(&_block)
  self
end
collect(&block) click to toggle source
# File lib/tensor_stream/tensor.rb, line 39
def collect(&block)
  @value.collect(&block)
end
consumers() click to toggle source
# File lib/tensor_stream/tensor.rb, line 25
def consumers
  op.consumers
end
device() click to toggle source
# File lib/tensor_stream/tensor.rb, line 35
def device
  @op.device
end
dtype() click to toggle source
# File lib/tensor_stream/tensor.rb, line 21
def dtype
  @data_type
end
eval(options = {}) click to toggle source
# File lib/tensor_stream/tensor.rb, line 47
def eval(options = {})
  Session.default_session.run(self, options)
end
first() click to toggle source
# File lib/tensor_stream/tensor.rb, line 68
def first
  _op(:index, self, 0)
end
inspect() click to toggle source
# File lib/tensor_stream/tensor.rb, line 14
def inspect
end
internal?() click to toggle source
# File lib/tensor_stream/tensor.rb, line 17
def internal?
  !!@internal
end
print!(message) click to toggle source
to_a() click to toggle source
# File lib/tensor_stream/tensor.rb, line 60
def to_a
  @value
end
to_f() click to toggle source
# File lib/tensor_stream/tensor.rb, line 64
def to_f
  @value
end
to_h() click to toggle source
# File lib/tensor_stream/tensor.rb, line 51
def to_h
  {
  }
end
to_i() click to toggle source
# File lib/tensor_stream/tensor.rb, line 56
def to_i
  @value
end
to_math(name_only = false, max_depth = 99, _unused = 0) click to toggle source
# File lib/tensor_stream/tensor.rb, line 72
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_s() click to toggle source
# File lib/tensor_stream/tensor.rb, line 43
def to_s
  @name
end

Protected Instance Methods

_reshape(arr, shape) click to toggle source
# File lib/tensor_stream/tensor.rb, line 170
def _reshape(arr, shape)
  if arr.is_a?(Array)
    return arr if shape.size < 2

    slice = shape.shift
    arr.each_slice(slice).collect do |s|
      _reshape(s, shape)
    end
  else
    return arr if shape.empty?

    slice = shape.shift
    return arr if slice.nil?

    Array.new(slice) do
      _reshape(arr, shape.dup)
    end
  end
end
build_name() click to toggle source
# File lib/tensor_stream/tensor.rb, line 190
def build_name
  @is_const ? "Const#{graph.get_const_counter}:#{@rank}" : "Variable#{graph.get_var_counter}:#{@rank}"
end
hashify_tensor(tensor) click to toggle source
# File lib/tensor_stream/tensor.rb, line 160
def hashify_tensor(tensor)
  if tensor.is_a?(Tensor)
    tensor.to_h
  elsif tensor.is_a?(Array)
    tensor.collect { |t| hashify_tensor(t) }
  else
    tensor
  end
end
propagate_outputs() click to toggle source
# File lib/tensor_stream/tensor.rb, line 156
def propagate_outputs
  # nop
end
setup_initial_state(options) click to toggle source
# File lib/tensor_stream/tensor.rb, line 150
def setup_initial_state(options)
  @outputs = []
  @graph = options[:__graph] || TensorStream.get_default_graph
  @source = format_source(caller_locations)
end