Class: Tensorflow::Eager::TensorHandle

Inherits:
Object
  • Object
show all
Includes:
Operators, TensorMixin
Defined in:
lib/tensorflow/eager/tensor_handle.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Operators

#%, #*, #**, #+, #-, #-@, #/

Methods included from TensorMixin

#numo, #shape

Constructor Details

#initialize(context, value) ⇒ TensorHandle

Returns a new instance of TensorHandle.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tensorflow/eager/tensor_handle.rb', line 30

def initialize(context, value)
  @context = context
  case value
    when ::FFI::Pointer
      @pointer = value
    when Tensor
      Status.check do |status|
        @pointer = FFI.TFE_NewTensorHandle(value, status)
      end
      # We need to keep the tensor live so that it is not freed!
      @tensor = value
    else
      raise(Error::InvalidArgumentError, "Invalid value passed to tensor_handle: #{value}")
  end

  ObjectSpace.define_finalizer(self, self.class.finalize(@pointer))
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



7
8
9
# File 'lib/tensorflow/eager/tensor_handle.rb', line 7

def context
  @context
end

Class Method Details

.finalize(pointer) ⇒ Object



9
10
11
12
13
# File 'lib/tensorflow/eager/tensor_handle.rb', line 9

def self.finalize(pointer)
  proc do
    FFI.TFE_DeleteTensorHandle(pointer)
  end
end

.from_value(context, value, dtype: nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tensorflow/eager/tensor_handle.rb', line 15

def self.from_value(context, value, dtype: nil)
  case value
    when TensorHandle
      value
    when Data::Dataset
      value.variant_tensor
    when Tensor
      TensorHandle.new(context, value)
    when Variable
      value.value_handle
    else
      TensorHandle.new(context, Tensor.new(value, dtype: dtype))
  end
end

Instance Method Details

#dtypeObject



58
59
60
# File 'lib/tensorflow/eager/tensor_handle.rb', line 58

def dtype
  FFI.TFE_TensorHandleDataType(self)
end

#element_countObject



62
63
64
65
66
# File 'lib/tensorflow/eager/tensor_handle.rb', line 62

def element_count
  Status.check do |status|
    FFI.TFE_TensorHandleNumElements(self, status)
  end
end

#tensorObject



52
53
54
55
56
# File 'lib/tensorflow/eager/tensor_handle.rb', line 52

def tensor
  Status.check do |status|
    Tensor.from_pointer(FFI.TFE_TensorHandleResolve(self, status))
  end
end

#to_ptrObject



48
49
50
# File 'lib/tensorflow/eager/tensor_handle.rb', line 48

def to_ptr
  @pointer
end

#valueObject



68
69
70
# File 'lib/tensorflow/eager/tensor_handle.rb', line 68

def value
  self.tensor.value
end