Extensor v0.1.4 Extensor.Tensor
This is a simple wrapper struct for a tensorflow tensor. It holds the data type, shape (dimensions), and binary data buffer for a tensor.
The layout of the buffer is the same as what is used in tensorflow - row-major dimension ordering with native endian byte order. Extensor performs very little manipulation of the data buffer, in order to minimize the performance impact of using tensorflow from Elixir.
The following atoms are used to represent the corresponding tensorflow data types.
atom | tensorflow type |
---|---|
:float | TF_FLOAT |
:double | TF_DOUBLE |
:int32 | TF_INT32 |
:uint8 | TF_UINT8 |
:int16 | TF_INT16 |
:int8 | TF_INT8 |
:string | TF_STRING |
:complex64 | TF_COMPLEX64 |
:complex | TF_COMPLEX |
:int64 | TF_INT64 |
:bool | TF_BOOL |
:qint8 | TF_QINT8 |
:quint8 | TF_QUINT8 |
:qint32 | TF_QINT32 |
:bfloat16 | TF_BFLOAT16 |
:qint16 | TF_QINT16 |
:quint16 | TF_QUINT16 |
:uint16 | TF_UINT16 |
:complex128 | TF_COMPLEX128 |
:half | TF_HALF |
:resource | TF_RESOURCE |
:variant | TF_VARIANT |
:uint32 | TF_UINT32 |
:uint64 | TF_UINT64 |
For convenience, though, functions are provided for constructing tensors from (nested) lists. These functions use binary pattern matching and concatenation to convert Elixir data types to/from the tensorflow binary standard.
Example:
iex> tensor = Extensor.Tensor.from_list([[1, 2], [3, 4]], :double)
%Extensor.Tensor{
type: :double,
shape: {2, 2},
data: <<0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 128, 64>>
}
iex> Extensor.Tensor.to_list(tensor)
[[1.0, 2.0], [3.0, 4.0]]
This module can also be used to verify that a tensor’s shape is consistent with its binary size. This can avoid segfaults in tensorflow when shape/size don’t match.
Link to this section Summary
Functions
converts a (nested) list to a tensor structure
converts a tensor to a nested list
validates the tensor shape/size
validates the tensor shape/size
Link to this section Types
data_type() :: :float | :double | :int32 | :uint8 | :int16 | :int8 | :string | :complex64 | :complex | :int64 | :bool | :qint8 | :quint8 | :qint32 | :bfloat16 | :qint16 | :quint16 | :uint16 | :complex128 | :half | :resource | :variant | :uint32 | :uint64
Link to this section Functions
converts a (nested) list to a tensor structure
converts a tensor to a nested list
validates the tensor shape/size
validates the tensor shape/size