Class: TensorStream::Tensor

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

Overview

Base class that defines a tensor like interface

Direct Known Subclasses

Constant, Operation, Placeholder, Variable

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TensorMixins

#!=, #%, #*, #**, #+, #-, #-@, #/, #<, #<=, #==, #>, #>=, #[], #and, #cast, #ceil, #dot, #floor, #log, #matmul, #reduce, #reshape, #round, #var, #zero?

Methods included from OpHelper

#_op, #cons, #format_source, #fp_type?, #i_cons, #i_op, #i_var, #int_type?, #reduced_shape, #shape_eval, #shape_full_specified, #shapes_fully_specified_and_equal

Instance Attribute Details

#data_typeObject

Returns the value of attribute data_type.



10
11
12
# File 'lib/tensor_stream/tensor.rb', line 10

def data_type
  @data_type
end

#given_nameObject

Returns the value of attribute given_name.



10
11
12
# File 'lib/tensor_stream/tensor.rb', line 10

def given_name
  @given_name
end

#graphObject (readonly)

Returns the value of attribute graph.



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

def graph
  @graph
end

#internalObject

Returns the value of attribute internal.



10
11
12
# File 'lib/tensor_stream/tensor.rb', line 10

def internal
  @internal
end

#is_constObject

Returns the value of attribute is_const.



10
11
12
# File 'lib/tensor_stream/tensor.rb', line 10

def is_const
  @is_const
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/tensor_stream/tensor.rb', line 10

def name
  @name
end

#native_bufferObject

Returns the value of attribute native_buffer.



10
11
12
# File 'lib/tensor_stream/tensor.rb', line 10

def native_buffer
  @native_buffer
end

#opObject

Returns the value of attribute op.



10
11
12
# File 'lib/tensor_stream/tensor.rb', line 10

def op
  @op
end

#outputsObject

Returns the value of attribute outputs.



10
11
12
# File 'lib/tensor_stream/tensor.rb', line 10

def outputs
  @outputs
end

#rankObject

Returns the value of attribute rank.



10
11
12
# File 'lib/tensor_stream/tensor.rb', line 10

def rank
  @rank
end

#shapeObject

Returns the value of attribute shape.



10
11
12
# File 'lib/tensor_stream/tensor.rb', line 10

def shape
  @shape
end

#sourceObject

Returns the value of attribute source.



10
11
12
# File 'lib/tensor_stream/tensor.rb', line 10

def source
  @source
end

#valueObject (readonly)

Returns the value of attribute value.



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

def value
  @value
end

Class Method Details

.cast_dtype(val, dtype) ⇒ Object



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
# File 'lib/tensor_stream/tensor.rb', line 103

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) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/tensor_stream/tensor.rb', line 85

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_countersObject



28
29
30
31
32
# File 'lib/tensor_stream/tensor.rb', line 28

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

Instance Method Details

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



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

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



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

def breakpoint!(&_block)
  self
end

#collect(&block) ⇒ Object



38
39
40
# File 'lib/tensor_stream/tensor.rb', line 38

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

#consumersObject



24
25
26
# File 'lib/tensor_stream/tensor.rb', line 24

def consumers
  op.consumers
end

#deviceObject



34
35
36
# File 'lib/tensor_stream/tensor.rb', line 34

def device
  @op.device
end

#dtypeObject



20
21
22
# File 'lib/tensor_stream/tensor.rb', line 20

def dtype
  @data_type
end

#eval(options = {}) ⇒ Object



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

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

#firstObject



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

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

#inspectObject



13
14
# File 'lib/tensor_stream/tensor.rb', line 13

def inspect
end

#internal?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/tensor_stream/tensor.rb', line 16

def internal?
  !!@internal
end

#print!(message) ⇒ Object



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

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

#to_aObject



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

def to_a
  @value
end

#to_fObject



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

def to_f
  @value
end

#to_hObject



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

def to_h
  {
  }
end

#to_iObject



55
56
57
# File 'lib/tensor_stream/tensor.rb', line 55

def to_i
  @value
end

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



71
72
73
74
75
76
77
78
79
# File 'lib/tensor_stream/tensor.rb', line 71

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



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

def to_s
  @name
end