Class: TensorStream::Tensor

Inherits:
Object
  • Object
show all
Extended by:
OpHelper
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 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

Methods included from TensorMixins

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

Instance Attribute Details

#data_typeObject

Returns the value of attribute data_type.



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

def data_type
  @data_type
end

#given_nameObject

Returns the value of attribute given_name.



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

def given_name
  @given_name
end

#graphObject (readonly)

Returns the value of attribute graph.



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

def graph
  @graph
end

#internalObject

Returns the value of attribute internal.



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

def internal
  @internal
end

#is_constObject

Returns the value of attribute is_const.



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

def is_const
  @is_const
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#native_bufferObject

Returns the value of attribute native_buffer.



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

def native_buffer
  @native_buffer
end

#opObject

Returns the value of attribute op.



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

def op
  @op
end

#outputsObject

Returns the value of attribute outputs.



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

def outputs
  @outputs
end

#rankObject

Returns the value of attribute rank.



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

def rank
  @rank
end

#shapeObject

Returns the value of attribute shape.



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

def shape
  @shape
end

#sourceObject

Returns the value of attribute source.



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

def source
  @source
end

#valueObject (readonly)

Returns the value of attribute value.



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

def value
  @value
end

Class Method Details

.cast_dtype(val, dtype) ⇒ Object



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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 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_countersObject



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

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



82
83
84
# 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) ⇒ Object



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

def breakpoint!(&_block)
  self
end

#collect(&block) ⇒ Object



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

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

#consumersObject



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

def consumers
  op.consumers
end

#deviceObject



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

def device
  @op.device
end

#dtypeObject



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

def dtype
  @data_type
end

#eval(options = {}) ⇒ Object



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

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

#firstObject



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

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

#inspectObject



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

def inspect
end

#internal?Boolean

Returns:

  • (Boolean)


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

def internal?
  !!@internal
end

#print!(message) ⇒ Object



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

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

#to_aObject



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

def to_a
  @value
end

#to_fObject



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

def to_f
  @value
end

#to_hObject



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

def to_h
  {
  }
end

#to_iObject



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

def to_i
  @value
end

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



72
73
74
75
76
77
78
79
80
# 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_sObject



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

def to_s
  @name
end