# Introduction to working with tensors in Vespa

Tensors allow Vespa to support more advanced ranking models such as large logistic regression models and neural networks. In this document, tensors are introduced with some examples of use. For details, refer to the tensor user guide and the tensor reference. Also try the blog recommendation tutorial.

## Tensor concepts

In Vespa, a tensor is a data structure which is a generalization of scalars, vectors and matrices. Tensors can have any order:

• A scalar is a tensor of order 0.
• A vector is a tensor of order 1.
• A matrix is a tensor of order 2.

Tensors consist of a set of double valued cells, with each cell having a unique address. A cell’s address is specified by its index or label along all dimensions. The number of dimensions in a tensor is the order of the tensor. Each dimension can be either sparse or dense. Sparse dimensions have labels designating their address, while dense dimensions have indices.

Example: Using literal form, the tensor:

{
{x:2, y:1}:1.0,
{x:0, y:2}:1.0
}


has two dimensions named x and y, and has two cells with defined values:

A type declaration is needed for tensors. This defines a sparse matrix:

tensor(x{},y{})


This is a dense matrix:

tensor(x[],y[])


Combination:

tensor(x[],y{})


Vespa uses the tensor type to optimize the ranking pipeline. As seen above, the dimensions themselves define if they are sparse or dense.

## Tensor examples

The following examples, uses the tensor playground to visualize tensor operations. The playground can be started by deploying the tensor sample app and opening “http://[host]:[port]/playground/index.html”. By clicking on the links below, a setup string will be copied to the clipboard - paste the string into the setup input box in the playground and press enter.

The neural network example is quite a bit more involved. Here, the network has 3 input neurons, 5 hidden neurons and a single output neuron. An example of neural networks in action can be found in the blog recommendation tutorial part 3.