tensor
- class tinychain.collection.tensor.Tensor(form)[source]
Bases:
Collection,NDArray,Trigonometric,Boolean,Numeric,Compare,Generic[DType]An n-dimensional array of numbers.
- 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.
- expand_dims(axes=None)[source]
Return a view of this Tensor with extra dimensions of size 1 at the given axes.
- log(base=None)[source]
Calculate the logarithm of this
Numericwith respect to the given base.If no base is given, this will return the natural logarithm (base e).
- 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.
- property ndim
Return the number of dimensions of this Tensor.
- 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.
- 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.
- property schema
Return the schema of this Collection.
- property shape
Return the shape of this Tensor.
- 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.
- 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.
- 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
Densetensor.Call this method to initialize a persistent Tensor in a Chain.
- classmethod load(shape, data, dtype=<class 'tinychain.scalar.number.F32'>)[source]
Load a
Densetensor 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.
- 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.
- class tinychain.collection.tensor.Sparse(form)[source]
Bases:
Tensor,Generic[DType]An n-dimensional array of numbers stored as a
Tableof 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.
- classmethod create(shape, dtype=<class 'tinychain.scalar.number.F32'>)[source]
Create a new, empty
Sparsetensor.Call this method to initialize a persistent
Tensorin aChain.
- classmethod load(shape, data, dtype=<class 'tinychain.scalar.number.F32'>)[source]
Load a
Sparsetensor 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))
- 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.