Tensors

Creation

Vector(arr)
Arguments:
  • arr (array) – array of values

Creates a tensor with dimension [m, 1], where m is the length of arr.

Example:

Vector([1, 2, 3])
Matrix(arr)
Arguments:
  • arr (array) – array of arrays of values

Creates a tensor with dimension [m, n], where m is the length of arr and n is the length of arr[0].

Example:

Matrix([[1, 2], [3, 4]])
Tensor(dims, arr)
Arguments:
  • dims (array) – array of dimension sizes
  • arr (array) – array of values

Creates a tensor with dimension dims out of a flat array arr.

Example:

Tensor([2, 2, 2], [1, 2, 3, 4, 5, 6, 7, 8])
zeros(dims)
Arguments:
  • dims (array) – dimension of tensor

Creates a tensor with dimension dims and all elements equal to zero.

Example:

zeros([10, 1])
ones(dims)
Arguments:
  • dims (array) – dimension of tensor

Creates a tensor with dimension dims and all elements equal to one.

Example:

ones([10, 1])
idMatrix(n)

Returns the n by n identity matrix.

oneHot(k, n)

Returns a vector of length n in which the k th entry is one and all other entries are zero.

Operations

WebPPL inherits its Tensor functionality from adnn. It supports all of the tensor functions documented here. Specifically, the ad.tensor module (and all the functions it contains) are globally available in WebPPL. For convenience, WebPPL also aliases ad.tensor to T, so you can write things like:

var x = Vector([1, 2, 3]);
var y = Vector([3, 4, 5]);
var x = T.dot(x, y);        // instead of ad.tensor.dot(x, y)

Other

dims(tensor)

Returns the shape of tensor.

dims(ones([3, 2])) // => [3, 2]
concat(arr)

Returns the vector obtained by concatenating array of vectors arr.

concat([Vector([1, 2]), Vector([3, 4])]) // => Vector([1, 2, 3, 4])