Research CommonsResearch Commons
cppgrad/Tensors

Tensors

The cppgrad::Tensor type — construction, properties, and the operations it supports.

cppgrad::Tensor is the core data type: a multi-dimensional array backed by ArrayFire, with optional gradient tracking. It supports arithmetic, reductions, and advanced indexing.

Construction

Tensors are created through static factory functions:

using namespace cppgrad;
 
Tensor z = Tensor::zeros({2, 3});                 // filled with 0
Tensor f = Tensor::full({2, 2}, 3.0f);            // filled with a constant
Tensor r = Tensor::rand({4, 4});                  // random values
Tensor g = Tensor::full({2, 2}, 1.0f, /*requires_grad=*/true);

Pass requires_grad=true when you want the autograd engine to track operations on the tensor and accumulate a gradient for it.

Properties

t.shape();          // dimensions
t.dtype();          // element type
t.requires_grad();  // whether gradients are tracked

Operations

Tensors support elementwise arithmetic and a set of reductions:

Tensor c = a + b;   // also -  *  /
Tensor s = a.sum();
Tensor m = a.mean();
Tensor mx = a.max();
Tensor e = a.exp();

Each differentiable operation registers a gradient function on the tape so the backward pass knows how to propagate through it. The built-in set includes Neg, Exp, Log, Pow, Sum, Mean, Max, and more.

Gradients

After a backward pass, the gradient is available on each tracked tensor:

loss.backward();
Tensor ga = a.grad();

See the Autograd engine for how the graph and backward pass work, and the API reference for the full surface.

Under the hood

Two core components back the public Tensor:

  • TensorImpl — the underlying storage and metadata.
  • GradFn — the base class for gradient functions, with subclasses such as SumFunction and MeanFunction implementing the backward math for each op.