Tensors¶
Creation¶
-
Vector(arr)¶ Arguments: - arr (array) – array of values
Creates a tensor with dimension
[m, 1], wheremis the length ofarr.Example:
Vector([1, 2, 3])
-
Matrix(arr)¶ Arguments: - arr (array) – array of arrays of values
Creates a tensor with dimension
[m, n], wheremis the length ofarrandnis the length ofarr[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
dimsout of a flat arrayarr.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
dimsand all elements equal to zero.Example:
zeros([10, 1])
-
ones(dims)¶ Arguments: - dims (array) – dimension of tensor
Creates a tensor with dimension
dimsand all elements equal to one.Example:
ones([10, 1])
-
idMatrix(n)¶ Returns the
nbynidentity matrix.
-
oneHot(k, n)¶ Returns a vector of length
nin which thekth 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 = T.transpose(Vector([1, 2, 3])); // instead of ad.tensor.transpose
var y = Vector([3, 4, 5]);
T.dot(x, y); // instead of ad.tensor.dot