Learn R Programming

einops (version 0.2.1)

EinopsBackend: Base Backend Class for Einops Tensor Operations

Description

Abstract base class that defines the interface for tensor operations across different frameworks. All backend implementations must inherit from this class and implement the required methods.

Arguments

Methods


Method new()

Initialize the backend and check for required packages. It is assumed that the constructor will fully load and setup all dependencies and error otherwise.

Usage

EinopsBackend$new()

Returns

A new EinopsBackend instance.


Method print()

The print method for EinopsBackend instances

Usage

EinopsBackend$print(...)

Arguments

...

arguments passed to pprint()

Returns

This object, invisibly


Method repr()

Get a string representation of this backend.

Usage

EinopsBackend$repr()

Returns

A character string describing the backend.


Method tensor_type()

Get the type of tensor this backend supports. This method should be overridden in subclasses to return the specific tensor type (e.g., "torch_tensor", "array").

Usage

EinopsBackend$tensor_type()

Returns

A string representing the tensor type.


Method preprocess()

Do any relevant preprocessing of a tensor before any operations are done on it. This should always be called before running any backend operations on a tensor

Usage

EinopsBackend$preprocess(x)

Arguments

x

The input raw tensor-like object

Returns

A preprocessed version of the input, may or may not have changed classes


Method create_tensor()

Create a tensor of the specified type with given values and dimensions.

Usage

EinopsBackend$create_tensor(values, dims, ...)

Arguments

values

A vector of values to initialize the tensor.

dims

A numeric vector specifying the dimensions of the tensor.

...

Additional arguments for specific backend implementations.

Returns

A tensor of the specified type.


Method as_array()

Convert a tensor to a standard base::array()

Usage

EinopsBackend$as_array(x)

Arguments

x

The input tensor/array.

Returns

A standard array representation of the tensor.


Method flatten()

Return a flattened version of the tensor. Note that the order of calling as_array and flatten does matter because different frameworks may store data differently.

Usage

EinopsBackend$flatten(x)

Arguments

x

The input tensor/array

Returns

A 1 dimensional tensor


Method arange()

Usage

EinopsBackend$arange(start, stop)

Arguments

start

integer, inclusive

stop

integer, inclusive

Returns

a sequence from start to stop


Method shape()

Get the shape of a tensor. Shape should return a tuple with integers or "shape symbols" (which will evaluate to actual size).

Usage

EinopsBackend$shape(x)

Arguments

x

The input tensor/array.

Returns

A numeric vector representing the tensor shape.


Method reshape()

Reshape a tensor to the specified dimensions.

Usage

EinopsBackend$reshape(x, shape)

Arguments

x

The input tensor/array.

shape

A numeric vector specifying the new shape.

Returns

The reshaped tensor/array.


Method transpose()

Transpose a tensor along the specified axes.

Usage

EinopsBackend$transpose(x, axes)

Arguments

x

The input tensor/array.

axes

A numeric vector specifying the new axis order.

Returns

The transposed tensor/array.


Method reduce()

Reduce a tensor along specified axes using the given operation.

Usage

EinopsBackend$reduce(x, operation, axes)

Arguments

x

The input tensor/array.

operation

A character string specifying the reduction operation (e.g., "sum", "mean", "max", "min", "prod", "all", "any"), OR a two argument function, with the first argument being the tensor to modify, and the second argument being an integer list of axes to perform the reduction over.

axes

A numeric vector specifying which axes to reduce over.

Returns

The reduced tensor/array.


Method stack_on_zeroth_dimension()

Stack multiple tensors along a new zeroth dimension.

Usage

EinopsBackend$stack_on_zeroth_dimension(tensors)

Arguments

tensors

A list of tensors/arrays to stack.

Returns

A tensor/array with the input tensors stacked along dimension 1.


Method add_axis()

Add a new axis to a tensor at the specified position.

Usage

EinopsBackend$add_axis(x, new_position)

Arguments

x

The input tensor/array.

new_position

The position (1-based) where to insert the new axis.

Returns

The tensor/array with a new axis added.


Method add_axes()

Add multiple axes to a tensor and tile along specified axes.

This function adds new axes to the input tensor at the specified positions and tiles the tensor along those axes according to the provided lengths.

Usage

EinopsBackend$add_axes(x, n_axes, pos2len)

Arguments

x

The input tensor/array.

n_axes

The total number of axes after addition.

pos2len

int->int r2r::hashmap() mapping positions to lengths.

Returns

The tensor/array with new axes added and tiled as specified.


Method tile()

Tile (repeat) a tensor along each axis according to the repeat counts. The repeats vector should have the same length as the tensor's shape.

Usage

EinopsBackend$tile(x, repeats)

Arguments

x

The input tensor/array.

repeats

A numeric vector specifying how many times to repeat along each axis. Must have same length as x.shape.

Returns

The tiled tensor/array.


Method concat()

Concatenate tensors along the specified axis. Assumes identical devices, dtypes and shapes except for the selected axis.

Usage

EinopsBackend$concat(tensors, axis)

Arguments

tensors

A list of tensors/arrays to concatenate.

axis

The axis along which to concatenate (1-based).

Returns

The concatenated tensor/array.


Method is_float_type()

Check if the tensor has a floating point data type.

Usage

EinopsBackend$is_float_type(x)

Arguments

x

The input tensor/array.

Returns

A logical value indicating if the tensor is of float type.


Method layers()

Get neural network layers specific to this backend.

Usage

EinopsBackend$layers()

Returns

Backend-specific layer implementations.


Method einsum()

Perform Einstein summation on tensors.

Usage

EinopsBackend$einsum(pattern, ...)

Arguments

pattern

A character string specifying the einsum pattern.

...

Additional tensors to operate on.

Returns

The result of the einsum operation.