unary

The Ops that have a single input and output are called Unary Ops. Usually this means that an Operation is applied elementwise.

This file contains all of the unary operations in Tricycle.

class Batch[source]

Bases: Op

A class for batching a tensor.

back_fn(grad)[source]

Compute the gradient for the batch operation.

Parameters:

grad (Tensor) – The gradient tensor.

Returns:

The computed gradient.

Return type:

Tensor

forward(tensor)[source]

Mark a tensor as batched.

Parameters:

tensor (Tensor) – The input tensor.

Returns:

A new Tensor marked as batched.

Return type:

Tensor

is_batched: bool
class UnaryAdd[source]

Bases: Op

A class for adding a constant to a tensor elementwise.

forward(tensor, constant)[source]

Add a constant, elementwise, to a tensor. The constant is not differentiable.

Parameters:
  • tensor (Tensor) – The input tensor.

  • constant (float) – The constant to add.

Returns:

A new Tensor with the constant added elementwise.

Raises:

AssertionError – If tensor is not a Tensor or constant is not a number.

Return type:

Tensor

class UnaryCos[source]

Bases: Op

A class for computing the cosine of a tensor elementwise.

back_fn(grad)[source]

Compute the gradient for the cosine operation.

Parameters:

grad (Tensor) – The gradient tensor.

Returns:

The computed gradient.

Return type:

Tensor

forward(tensor)[source]

Applies the cosine function, elementwise, to a tensor.

Parameters:

tensor (Tensor) – The input tensor.

Returns:

A new Tensor with the cosine of each element.

Return type:

Tensor

class UnaryDivide[source]

Bases: Op

A class for dividing a constant by a tensor elementwise.

forward(constant, tensor)[source]

Divide a constant by a tensor, elementwise. The constant is not differentiable.

Parameters:
  • constant (float | int) – The constant numerator.

  • tensor (Tensor) – The tensor denominator.

Returns:

A new Tensor with the constant divided by each element of the tensor.

Return type:

Tensor

class UnaryExp[source]

Bases: Op

A class for computing the exponential of a tensor elementwise.

back_fn(grad)[source]

Compute the gradient for the exponential operation.

Parameters:

grad (Tensor) – The gradient tensor.

Returns:

The computed gradient.

Return type:

Tensor

forward(tensor)[source]

Raise every element of a tensor to the power of e.

Parameters:

tensor (Tensor) – The input tensor.

Returns:

A new Tensor with the exponential of each element.

Return type:

Tensor

class UnaryLog[source]

Bases: Op

A class for computing the natural logarithm of a tensor elementwise.

REALLY_SMALL_NUMBER = 1e-06
back_fn(grad)[source]

Compute the gradient for the logarithm operation.

Parameters:

grad (Tensor) – The gradient tensor.

Returns:

The computed gradient.

Return type:

Tensor

forward(tensor)[source]

Compute the natural logarithm of each element in the tensor.

Parameters:

tensor (Tensor) – The input tensor.

Returns:

A new Tensor with the natural logarithm of each element.

Return type:

Tensor

class UnaryMask[source]

Bases: Op

A class for applying a binary mask to a tensor.

back_fn(grad)[source]

Compute the gradient for the mask operation.

Parameters:

grad (Tensor) – The gradient tensor.

Returns:

The computed gradient.

Return type:

Tensor

forward(tensor, mask)[source]

Apply a binary mask to a numpy array, setting values to 0 where the mask is True.

Parameters:
  • tensor (Tensor) – The input tensor.

  • mask (Tensor) – The binary mask tensor.

Returns:

A new Tensor with the mask applied.

Raises:

AssertionError – If tensor and mask shapes don’t match or if mask requires grad.

Return type:

Tensor

class UnaryMax[source]

Bases: Op

A class for computing the elementwise maximum of a tensor and a constant.

back_fn(grad)[source]

Compute the gradient for the maximum operation.

Parameters:

grad (Tensor) – The gradient tensor.

Returns:

The computed gradient.

Return type:

Tensor

forward(tensor, constant)[source]

Return the max of the tensor and the constant, elementwise. The constant is not differentiable.

Parameters:
  • tensor (Tensor) – The input tensor.

  • constant (float) – The constant to compare against.

Returns:

A new Tensor with the elementwise maximum of the input and the constant.

Raises:

AssertionError – If tensor is not a Tensor or constant is not a scalar.

Return type:

Tensor

class UnaryMin[source]

Bases: Op

A class for computing the elementwise minimum of a tensor and a constant.

back_fn(grad)[source]

Compute the gradient for the minimum operation.

Parameters:

grad (Tensor) – The gradient tensor.

Returns:

The computed gradient.

Return type:

Tensor

forward(tensor, constant)[source]

Return the min of the tensor and the constant, elementwise. The constant is not differentiable.

Parameters:
  • tensor (Tensor) – The input tensor.

  • constant (float) – The constant to compare against.

Returns:

A new Tensor with the elementwise minimum of the input and the constant.

Raises:

AssertionError – If tensor is not a Tensor or constant is not a scalar.

Return type:

Tensor

class UnaryMultiply[source]

Bases: Op

A class for multiplying a tensor by a constant elementwise.

back_fn(grad)[source]

Compute the gradient for the multiplication operation.

Parameters:

grad (Tensor) – The gradient tensor.

Returns:

The computed gradient.

Return type:

Tensor

forward(tensor, constant)[source]

Multiply a constant, elementwise, to a tensor. The constant is not differentiable.

Parameters:
  • tensor (Tensor) – The input tensor.

  • constant (float) – The constant to multiply.

Returns:

A new Tensor with the constant multiplied elementwise.

Raises:

AssertionError – If tensor is not a Tensor or constant is not a scalar.

Return type:

Tensor

class UnaryPower[source]

Bases: Op

A class for raising a tensor to a constant power elementwise.

back_fn(grad)[source]

Compute the gradient for the power operation.

Parameters:

grad (Tensor) – The gradient tensor.

Returns:

The computed gradient.

Return type:

Tensor

forward(tensor, constant)[source]

Raise a tensor to a constant, elementwise. The constant is not differentiable.

Parameters:
  • tensor (Tensor) – The input tensor.

  • constant (float) – The constant power.

Returns:

A new Tensor with each element raised to the constant power.

Raises:

AssertionError – If tensor is not a Tensor or constant is not a scalar.

Return type:

Tensor

class UnarySin[source]

Bases: Op

A class for computing the sine of a tensor elementwise.

back_fn(grad)[source]

Compute the gradient for the sine operation.

Parameters:

grad (Tensor) – The gradient tensor.

Returns:

The computed gradient.

Return type:

Tensor

forward(tensor)[source]

Applies the sine function, elementwise, to a tensor.

Parameters:

tensor (Tensor) – The input tensor.

Returns:

A new Tensor with the sine of each element.

Return type:

Tensor

class UnarySquareRoot[source]

Bases: Op

A class for computing the square root of a tensor elementwise.

forward(tensor)[source]

Apply the square root function.

Parameters:

tensor (Tensor) – The input tensor.

Returns:

A new Tensor with the square root of each element.

class UnarySubtract[source]

Bases: Op

A class for subtracting a constant from a tensor elementwise.

forward(tensor, constant)[source]

Subtract a constant, elementwise, from a tensor. The constant is not differentiable.

Parameters:
  • tensor (Tensor) – The input tensor.

  • constant (float) – The constant to subtract.

Returns:

A new Tensor with the constant subtracted elementwise.

Return type:

Tensor

class UnarySum[source]

Bases: Op

A class for summing all values in a tensor.

back_fn(grad)[source]

Compute the gradient for the sum operation.

Parameters:

grad (Tensor) – The gradient tensor.

Returns:

The computed gradient.

Raises:

AssertionError – If grad is not a scalar or an empty tensor.

Return type:

Tensor

forward(tensor)[source]

Sums all the values in a tensor. Note, this function always produces a non-batched output.

Parameters:

tensor (Tensor) – The input tensor.

Returns:

A new Tensor with the sum of all elements.

Raises:

AssertionError – If the sum operation fails.

Return type:

Tensor

class Unbatch[source]

Bases: Op

A class for unbatching a tensor.

back_fn(grad)[source]

Compute the gradient for the unbatch operation.

Parameters:

grad (Tensor) – The gradient tensor.

Returns:

The computed gradient.

Return type:

Tensor

forward(tensor)[source]

Mark a tensor as unbatched.

Parameters:

tensor (Tensor) – The input tensor.

Returns:

A new Tensor marked as unbatched.

Return type:

Tensor

is_batched: bool
batch(tensor)[source]

Tell Tricycle to treat this tensor as a batch of tensors.

Parameters:

tensor (Tensor) – The input tensor.

Returns:

A new Tensor marked as batched.

Return type:

Tensor

nothing(tensor)[source]

Do nothing to a tensor.

Operations like addition have a back_fn of multiplying by 1 which is equivalent to doing nothing. Instead of adding a bunch of checks to see whether a back_fn exists or not, the logic is much simpler by adding this function that does nothing.

Parameters:

tensor – The input tensor.

Returns:

The input tensor unchanged.

unbatch(tensor)[source]

Tell Tricycle to treat this tensor as a single tensor (not a batch of tensors).

Parameters:

tensor (Tensor) – The input tensor.

Returns:

A new Tensor marked as unbatched.

Return type:

Tensor