Class: Tensorflow::Tensor
Class Method Summary
collapse
Instance Method Summary
collapse
#numo, #shape
Methods included from Operators
#%, #*, #**, #+, #-, #-@, #/
Constructor Details
#initialize(value, dtype: nil, shape: []) ⇒ Tensor
Returns a new instance of Tensor.
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/tensorflow/tensor.rb', line 48
def initialize(value, dtype: nil, shape: [])
value = case value
when Numo::NArray
value
when Array
result = Numo::NArray.cast(value)
else
TensorData.value_with_shape(value, dtype, shape)
end
tensor_data = TensorData.new(value, dtype: dtype, shape: shape)
dtype = tensor_data.dtype
shape = tensor_data.shape
if shape && shape.size > 0
dims_ptr = ::FFI::MemoryPointer.new(:int64, shape.size)
dims_ptr.write_array_of_int64(shape)
else
dims_ptr = nil
end
@pointer = FFI.TF_NewTensor(dtype,
dims_ptr, shape ? shape.size : 0,
tensor_data, tensor_data.byte_size,
TensorData::Deallocator, nil)
ObjectSpace.define_finalizer(self, self.class.finalize(@pointer))
end
|
Class Method Details
.finalize(pointer) ⇒ Object
6
7
8
9
10
|
# File 'lib/tensorflow/tensor.rb', line 6
def self.finalize(pointer)
proc do
FFI.TF_DeleteTensor(pointer)
end
end
|
.from_pointer(pointer) ⇒ Object
41
42
43
44
45
46
|
# File 'lib/tensorflow/tensor.rb', line 41
def self.from_pointer(pointer)
result = self.allocate
result.instance_variable_set(:@pointer, pointer)
ObjectSpace.define_finalizer(result, self.finalize(pointer))
result
end
|
.from_proto(proto) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/tensorflow/tensor.rb', line 27
def self.from_proto(proto)
proto = proto.is_a?(TensorProto) ? proto : TensorProto.decode(proto)
shape = proto.tensor_shape.dim.map(&:size)
dtype = FFI::DataType[DataType.resolve(proto.dtype)]
numo_klass = TensorData::DTYPE_TO_NUMO_TYPE_MAP[dtype]
value = if shape.empty?
array = numo_klass.from_binary(proto.tensor_content)
array[0]
else
numo_klass.from_binary(proto.tensor_content, shape)
end
self.new(value, dtype:dtype, shape:shape)
end
|
.from_value(value, dtype: nil) ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/tensorflow/tensor.rb', line 12
def self.from_value(value, dtype: nil)
case value
when Tensor
value
when Graph::Operation
value
when Eager::TensorHandle
value.tensor
when Data::Dataset
value.variant_tensor
else
Tensor.new(value, dtype: dtype)
end
end
|
Instance Method Details
#byte_size ⇒ Object
94
95
96
|
# File 'lib/tensorflow/tensor.rb', line 94
def byte_size
FFI.TF_TensorByteSize(self)
end
|
#data ⇒ Object
103
104
105
|
# File 'lib/tensorflow/tensor.rb', line 103
def data
TensorData.from_pointer(FFI.TF_TensorData(self), self.byte_size, self.dtype, self.shape)
end
|
#dtype ⇒ Object
82
83
84
|
# File 'lib/tensorflow/tensor.rb', line 82
def dtype
FFI.TF_TensorType(self)
end
|
#inspect ⇒ Object
98
99
100
101
|
# File 'lib/tensorflow/tensor.rb', line 98
def inspect
inspection = %w(numo shape dtype).map { |v| "#{v}: #{send(v).inspect}"}
"#<#{self.class} #{inspection.join(", ")}>"
end
|
#to_ptr ⇒ Object
90
91
92
|
# File 'lib/tensorflow/tensor.rb', line 90
def to_ptr
@pointer
end
|
#to_s ⇒ Object
86
87
88
|
# File 'lib/tensorflow/tensor.rb', line 86
def to_s
inspect
end
|
#value ⇒ Object
78
79
80
|
# File 'lib/tensorflow/tensor.rb', line 78
def value
self.data.value
end
|