Extensor v0.1.2 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.

atomtensorflow type
:floatTF_FLOAT
:doubleTF_DOUBLE
:int32TF_INT32
:uint8TF_UINT8
:int16TF_INT16
:int8TF_INT8
:stringTF_STRING
:complex64TF_COMPLEX64
:complexTF_COMPLEX
:int64TF_INT64
:boolTF_BOOL
:qint8TF_QINT8
:quint8TF_QUINT8
:qint32TF_QINT32
:bfloat16TF_BFLOAT16
:qint16TF_QINT16
:quint16TF_QUINT16
:uint16TF_UINT16
:complex128TF_COMPLEX128
:halfTF_HALF
:resourceTF_RESOURCE
:variantTF_VARIANT
:uint32TF_UINT32
:uint64TF_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

Convert a Matrex matrix to a tensor struct

converts a tensor to a nested list

Convert from a tensor struct to a Matrex matrix

validates the tensor shape/size

validates the tensor shape/size

Link to this section Types

Link to this type data_type()
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 type t()
t() :: %Extensor.Tensor{data: binary(), shape: tuple(), type: data_type()}

Link to this section Functions

Link to this function from_list(list, type \\ :float)
from_list(list :: list(), type :: data_type()) :: t()

converts a (nested) list to a tensor structure

Link to this function from_matrix(matrex)
from_matrix(matrix :: Matrex.t()) :: t()

Convert a Matrex matrix to a tensor struct

Link to this function to_list(tensor)
to_list(tensor :: t()) :: list()

converts a tensor to a nested list

Link to this function to_matrix(tensor)
to_matrix(tensor :: t()) :: Matrex.t()

Convert from a tensor struct to a Matrex matrix

Currently, this only works for two dimensional tensors and the type must be :float.

Link to this function validate(tensor)
validate(tensor :: t()) :: :ok | {:error, any()}

validates the tensor shape/size

Link to this function validate!(tensor)
validate!(tensor :: t()) :: :ok | no_return()

validates the tensor shape/size