Tensors

Tensors are the fundamental data structure in Brain4J. All computations, models, and data transformations operate on tensors, which provide an efficient way to represent and manipulate high-dimensional data such as vectors, matrices, sequences, images, and text.

A tensor in Brain4J is defined by the following properties:

  • Order (Rank): Number of indices required to uniquely identify an element. For example, scalars have order 0, vectors have order 1, and matrices have order 2. Note: In Brain4J scalars have rank 1, because they are.

  • Shape: Number of elements along each dimension, or axis of the tensor. For example: an RGB image can be represented by a tensor with shape [color_channels, height, width].

  • Stride: The number of elements to skip in order to access the next element in that dimension.

  • Strides: A list containing the stride value for each dimension of the tensor.

Device placement

In Brain4J, every tensor is associated with a specific execution device. A device defines where the tensor’s data is stored and where operations on that tensor are executed.

Currently, Brain4J supports the following devices:

  • CPU (default)

  • GPU (via OpenCL)

Creating tensors on a device

Tensors are created on the CPU by default. A tensor can be explicitly moved to another device when required.

Device device = Brain4J.firstDevice(); // gets the first device available
Tensor t = Tensors.random(1024, 1024); // CPU tensor

// moves the tensor to the GPU
Tensor gpuTensor = t.to(Device.GPU);

Whenever a tensor is located in the GPU, operations can be batched in a single command queue and planned for execution. Note: the data() method is not recommended for use when a tensor is hosted in the GPU.

Memory Layout

TODO

Broadcasting

Broadcasting allows tensors with compatible shapes to participate in the same operation without explicitly duplicating data.

A tensor can be broadcast along a dimension if its size in that dimension is either:

  • equal to the target size, or

  • equal to 1

Broadcasting is applied per-dimension, from the trailing axes.

Example

Result shape: [4, 3]

Important notes

  • Broadcasting does not allocate new memory for the broadcasted tensor.

  • If shapes are not compatible, the operation fails with an error.

  • Not all operations support broadcasting.

In-place operations & Mutability

TODO

Examples

Basic tensor creation
Random tensors and matrix multiplication
Copy vs in-place operations
Reshaping tensors (views)
Slicing tensors
Mapping values

Last updated