tensor

class tinychain.collection.tensor.Tensor(form)[source]

Bases: Collection, NDArray, Trigonometric, Boolean, Numeric, Compare, Generic[DType]

An n-dimensional array of numbers.

abs()[source]

Absolute value

acos()[source]

Arccosine

acosh()[source]

Hyperbolic arccosine

add(other)[source]

Addition

asin()[source]

Arcsine

asinh()[source]

Hyperbolic arcsine

atan()[source]

Arctangent

atanh()[source]

Hyperbolic arctangent

broadcast(shape)[source]

Broadcast this NDArray into the given shape.

cast(dtype)[source]

Attempt to cast this State into the given dtype.

cond(then, or_else)[source]

Return a view of then and or_else depending on whether the corresponding element of this tensor is True.

self, then, and or_else must support broadcasting to the same shape.

copy()[source]

Return a copy of this NDArray

cos()[source]

Cosine

cosh()[source]

Hyperbolic cosine

div(other)[source]

Division

eq(other)[source]

Return a boolean Tensor with element-wise equality values.

exp()[source]

Raise e to the power of this Numeric.

expand_dims(axes=None)[source]

Return a view of this Tensor with extra dimensions of size 1 at the given axes.

gt(other)[source]

Return a boolean Tensor with element-wise greater-than values.

gte(other)[source]

Return a boolean Tensor with element-wise greater-or-equal values.

log(base=None)[source]

Calculate the logarithm of this Numeric with respect to the given base.

If no base is given, this will return the natural logarithm (base e).

lt(other)[source]

Return a boolean Tensor with element-wise less-than values.

lte(other)[source]

Return a boolean Tensor with element-wise less-or-equal values.

max(axes=None, keepdims=False)[source]

Return the maximum value of this Tensor along the given axes, or the overall maximum if no axes are given.

min(axes=None, keepdims=False)[source]

Return the minimum value of this Tensor along the given axes, or the overal minimum if no axes are given.

mul(other)[source]

Multiplication

property ndim

Return the number of dimensions of this Tensor.

ne(other)[source]

Return a boolean Tensor with element-wise not-equal values.

norm(axis=None, keepdims=False)[source]

With no axis, computes the Frobenius norm (aka Euclidean norm) of a matrix or batch of matrices.

For a vector norm, specify the axis of the vector.

pow(other)[source]

Raise this Numeric to the given power.

product(axes=None, keepdims=False)[source]

Calculate the product of this Tensor along the given axes, or the total product if no axes are given.

reshape(shape, copy=True)[source]

Return a view of this Tensor with the given shape.

property schema

Return the schema of this Collection.

property shape

Return the shape of this Tensor.

sin()[source]

Sine

sinh()[source]

Hyperbolic sine

slice(bounds)[source]

Return the sub-array of this NDArray within the given bounds.

sub(other)[source]

Subtraction

sum(axes=None, keepdims=False)[source]

Calculate the sum of this Tensor along the given axes, or the total sum if no axes are given.

tan()[source]

Tangent

tanh()[source]

Hyperbolic tangent

transpose(permutation=None)[source]

Return a view of this Tensor with its axes transposed according to the given permutation.

If no permutation is given, the axes will be inverted (e.g. (0, 1, 2) inverts to (2, 1, 0)).

class tinychain.collection.tensor.Dense(form)[source]

Bases: Tensor, Generic[DType]

An n-dimensional array of numbers stored as sequential blocks.

IMPORTANT: for efficiency reasons, serialization of a Dense tensor will stop if a non-numeric value (NaN or +/- infinity) is encountered. If you receive a Dense tensor without enough elements for its shape, you can safely treat this response as a divide-by-zero error.

add(other)[source]

Addition

classmethod arange(shape, start, stop)[source]

Return a Dense tensor with the given shape containing a range of numbers evenly distributed between start and stop.

argsort()[source]

Return the coordinates needed to sort this Tensor.

as_sparse()[source]

Return a Sparse view of this Dense tensor.

cast(dtype)[source]

Attempt to cast this State into the given dtype.

classmethod concatenate(tensors, axis=0)[source]

Create a new Dense tensor by concatenating the given tensors along the given axis.

classmethod constant(shape, value)[source]

Return a Dense tensor of the given shape filled with the given value.

classmethod create(shape, dtype=<class 'tinychain.scalar.number.F32'>)[source]

Create a new, empty Dense tensor.

Call this method to initialize a persistent Tensor in a Chain.

classmethod load(shape, data, dtype=<class 'tinychain.scalar.number.F32'>)[source]

Load a Dense tensor from an existing data set.

Example:
values = [0, 1, 2]
dense = tc.tensor.Dense.load([1, 3], values, tc.I32)
classmethod ones(shape, dtype=<class 'tinychain.scalar.number.F32'>)[source]

Construct a Dense tensor filled with ones.

classmethod ones_like(tensor)[source]

Return a Dense tensor filled with ones, with the same shape as the given tensor.

classmethod random_normal(shape, mean=0.0, std=1.0)[source]

Return a Dense tensor filled with a random normal distribution of F64 s.

classmethod random_uniform(shape, minval=0, maxval=1)[source]

Return a Dense tensor filled with a uniform random distribution of F64 s.

sub(other)[source]

Subtraction

classmethod truncated_normal(shape, mean=0.0, std=1.0, minval=None, maxval=None)[source]

Return a Dense tensor filled with a random normal distribution of F64 s.

Any value x outside the range minval <= x <= maxval will be replaced by a value drawn from a new random normal distribution.

minval and maxval default to two standard deviations.

classmethod zeros(shape, dtype=<class 'tinychain.scalar.number.F32'>)[source]

Construct a Dense tensor filled with zeros.

classmethod zeros_like(tensor)[source]

Return a Dense tensor filled with zeros, with the same shape as the given tensor.

class tinychain.collection.tensor.Sparse(form)[source]

Bases: Tensor, Generic[DType]

An n-dimensional array of numbers stored as a Table of coordinates and values.

IMPORTANT: be careful when broadcasting a Sparse tensor–the result may not be so sparse! For example, broadcasting a Sparse tensor with shape [2, 1] with exactly one element into shape [2, 1000] will result in a Sparse tensor with 1000 elements.

The and, div, and mul methods are optimized to avoid this issue by ignoring right-hand values at coordinates which are not filled in the left-hand Tensor.

as_dense()[source]

Return a Dense view of this Sparse tensor.

cast(dtype)[source]

Attempt to cast this State into the given dtype.

classmethod create(shape, dtype=<class 'tinychain.scalar.number.F32'>)[source]

Create a new, empty Sparse tensor.

Call this method to initialize a persistent Tensor in a Chain.

div(other)[source]

Division

classmethod eye(size, batch=[])[source]

Create a boolean identity matrix of shape [size, size].

classmethod load(shape, data, dtype=<class 'tinychain.scalar.number.F32'>)[source]

Load a Sparse tensor from an existing data set.

Example:
coords = [[0, 0, 1], [0, 1, 0]]
values = [1, 2]
sparse = tc.tensor.Sparse.load([2, 3, 4], zip(coords, values))
mul(other)[source]

Multiplication

classmethod zeros(shape, dtype=<class 'tinychain.scalar.number.F32'>)[source]

Create a Sparse tensor with the given shape and data type.

If dtype is not specified, the data type will be F32.

classmethod zeros_like(tensor)[source]

Create a Sparse tensor with the same shape and data type as the given tensor.

tinychain.collection.tensor.einsum(f, tensors)[source]

Return the Einstein summation of the given tensors according the the given format string.

Example: einsum(“ij,jk->ik”, [A, B]) # multiply matrices A and B

The tensor product is computed from left to right, so when using any Sparse tensors, it’s important to put the sparsest first in the list to avoid redundant broadcasting.

tinychain.collection.tensor.split(tensor, num_or_size_splits, axis=0)[source]

Split the given tensor into multiple slices along the given axis.

This method requires a constant num_or_size_splits, axis, and self.shape[axis].

If num_or_size_splits is a Number, the tensor will be sliced along axis num_or_size_splits times; if self.shape[axis] % num_or_size_splits != 0 then a ValueError error will be raised.

If num_or_size_splits is a Tuple with length n then the tensor will be split into n slices each with shape[axis] == num_or_size_splits[axis]; if the sum of num_or_size_splits is not equal to self.shape[axis] then a ValueError error will be raised.

tinychain.collection.tensor.tile(tensor, multiples)[source]

Construct a new Tensor by tiling the given tensor multiples times.

The values of tensor are repeated multiples[x] times along the x`th axis of the output. `multiples must be a positive integer or a Tuple of length tensor.ndim.