AI::TensorFlow::Libtensorflow::Tensor - A multi-dimensional array of elements of a single data type
use aliased 'AI::TensorFlow::Libtensorflow::Tensor' => 'Tensor'; use AI::TensorFlow::Libtensorflow::DataType qw(FLOAT); use List::Util qw(product); my $dims = [3, 3]; # Allocate a 3 by 3 ndarray of type FLOAT my $t = Tensor->Allocate(FLOAT, $dims); is $t->ByteSize, product(FLOAT->Size, @$dims), 'correct size'; my $scalar_dims = []; my $scalar_t = Tensor->Allocate(FLOAT, $scalar_dims); is $scalar_t->ElementCount, 1, 'single element'; is $scalar_t->ByteSize, FLOAT->Size, 'single FLOAT';
A TFTensor is an object that contains values of a single type arranged in an n-dimensional array.
TFTensor
For types other than STRING, the data buffer is stored in row major order.
Of note, this is different from the definition of tensor used in mathematics and physics which can also be represented as a multi-dimensional array in some cases, but these tensors are defined not by the representation but by how they transform. For more on this see
Lim, L.-H. (2021). Tensors in computations. Acta Numerica, 30, 555–764. Cambridge University Press. DOI: https://doi.org/10.1017/S0962492921000076.
New( $dtype, $dims, $data, $deallocator, $deallocator_arg )
Creates a TFTensor from a data buffer $data with the given specification of data type $dtype and dimensions $dims.
$data
$dtype
$dims
# Create a buffer containing 0 through 8 single-precision # floating-point data. my $data = pack("f*", 0..8); $t = Tensor->New( FLOAT, [3,3], \$data, sub { undef $data }, undef ); ok $t, 'Created 3-by-3 float TFTensor';
Implementation note: if $dtype is not a STRING or RESOURCE, then the pointer for $data is checked to see if meets the TensorFlow's alignment preferences. If it does not, the contents of $data are copied into a new buffer and $deallocator is called during construction. Otherwise the contents of $data are not owned by the returned TFTensor.
$deallocator
Parameters
DataType for the TFTensor.
An ArrayRef of the size of each dimension.
ArrayRef
Data buffer for the contents of the TFTensor.
A callback used to deallocate $data which is passed the parameters $deallocator->( opaque $pointer, size_t $size, opaque $deallocator_arg).
$deallocator->( opaque $pointer, size_t $size, opaque $deallocator_arg)
undef
Argument that is passed to the $deallocator callback.
Returns
A new TFTensor with the given data and specification.
C API: TF_NewTensor
TF_NewTensor
Allocate($dtype, $dims, $len = )
This constructs a TFTensor with the memory for the TFTensor allocated and owned by the TFTensor itself. Unlike with "New" the allocated memory satisfies TensorFlow's alignment preferences.
See "Data" for how to write to the data buffer.
use AI::TensorFlow::Libtensorflow::DataType qw(DOUBLE); # Allocate a 2-by-2 ndarray of type DOUBLE $dims = [2,2]; my $t = Tensor->Allocate(DOUBLE, $dims, product(DOUBLE->Size, @$dims));
Number of bytes for the data buffer. If a value is not given, this is calculated from $dtype and $dims.
A TFTensor with memory allocated for data buffer.
C API: TF_AllocateTensor
TF_AllocateTensor
Provides a way to access the data buffer for the TFTensor. The ScalarRef that it returns is read-only, but the underlying pointer can be accessed as long as one is careful when handling the data (do not write to memory outside the size of the buffer).
ScalarRef
use AI::TensorFlow::Libtensorflow::DataType qw(DOUBLE); use FFI::Platypus::Buffer qw(scalar_to_pointer); use FFI::Platypus::Memory qw(memcpy); my $t = Tensor->Allocate(DOUBLE, [2,2]); # [2,2] identity matrix my $eye_data = pack 'd*', (1, 0, 0, 1); memcpy scalar_to_pointer(${ $t->Data }), scalar_to_pointer($eye_data), $t->ByteSize; ok ${ $t->Data } eq $eye_data, 'contents are the same';
Returns a read-only ScalarRef for the TFTensor's data buffer.
C API: TF_TensorData
TF_TensorData
Returns the number of bytes for the TFTensor's data buffer.
C API: TF_TensorByteSize
TF_TensorByteSize
The TFTensor's data type.
C API: TF_TensorType
TF_TensorType
The number of dimensions for the TFTensor.
C API: TF_NumDims
TF_NumDims
Number of elements in the TFTensor.
C API: TF_TensorElementCount
TF_TensorElementCount
Dim( $dim_index )
The zero-based index for a given dimension.
The extent of the given dimension.
C API: TF_Dim
TF_Dim
Deletes the TFTensor and returns a new TFTensor with the same content if possible. Returns undef and leaves the TFTensor untouched if not.
C API: TF_TensorMaybeMove
TF_TensorMaybeMove
C API: TF_TensorIsAligned
TF_TensorIsAligned
SetShape( $dims )
Set a new shape for the TFTensor.
C API: TF_SetShape
TF_SetShape
libtensorflow version: v2.10.0
libtensorflow
C API: TF_TensorBitcastFrom
TF_TensorBitcastFrom
Default destructor.
C API: TF_DeleteTensor
TF_DeleteTensor
Provides ndarrays for access from Perl.
Zakariyya Mughal <zmughal@cpan.org>
This software is Copyright (c) 2022-2023 by Auto-Parallel Technologies, Inc.
This is free software, licensed under:
The Apache License, Version 2.0, January 2004
To install AI::TensorFlow::Libtensorflow, copy and paste the appropriate command in to your terminal.
cpanm
cpanm AI::TensorFlow::Libtensorflow
CPAN shell
perl -MCPAN -e shell install AI::TensorFlow::Libtensorflow
For more information on module installation, please visit the detailed CPAN module installation guide.