# Tensor Introduction

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 rank of the tensor. Each dimension can be either mapped or indexed. Mapped dimensions are sparse and allow any label (string identifier) designating their address, while indexed dimensions use dense numberic indices starting at 0.

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 2-dimensional mapped tensor (matrix) of float:

tensor<float>(x{},y{})


This is a 2-dimensional indexed tensor (a 2x3 matrix) of double:

tensor<double>(x,y)


A combination of mapped and indexed dimensions is a mixed tensor:

tensor<float>(x{},y)


Vespa uses the tensor type to optimize the ranking pipeline. Best performance is currently achieved when using only indexed dimensions. As seen above, the dimensions themselves define if they are mapped or indexed.

## 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.