Skip to content

Operations API

All public operations accept or return Variable objects and provide both symbolic and raw-array gradient paths.

Category Functions
Arithmetic add, sub, mul, div, neg, pow
Mathematical exp, log, sin, cos, tanh, square
Activation relu, sigmoid
Matrix and shape matmul, reshape, transpose, broadcast_to
Reduction sum, sum_to
Loss mean_squared_error, softmax_cross_entropy

Matrix multiplication

matmul

matmul(x: Variable, W: Variable) -> Variable
Source code in MiniTorch/ops/matmul.py
def matmul(x: Variable, W: Variable) -> Variable:
    return MatMul()(x, W)  # type: ignore[return-value]

Reductions

sum

sum(x: Variable, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Variable
Source code in MiniTorch/ops/sum.py
def sum(
    x: Variable,
    axis: int | tuple[int, ...] | None = None,
    keepdims: bool = False,
) -> Variable:
    return Sum(axis=axis, keepdims=keepdims)(x)  # type: ignore[return-value]

sum_to

sum_to(x: Variable, shape: tuple[int, ...]) -> Variable
Source code in MiniTorch/ops/sum_to.py
def sum_to(x: Variable, shape: tuple[int, ...]) -> Variable:
    if x.shape == shape:
        return as_variable(x)
    return SumTo(shape)(x)  # type: ignore[return-value]

Activations

relu

relu(x: Variable) -> Variable
Source code in MiniTorch/ops/relu.py
def relu(x: Variable) -> Variable:
    return ReLU()(x)  # type: ignore[return-value]

sigmoid

sigmoid(x: Variable) -> Variable
Source code in MiniTorch/ops/sigmoid.py
def sigmoid(x: Variable) -> Variable:
    return Sigmoid()(x)  # type: ignore[return-value]

tanh

tanh(x: Variable) -> Variable
Source code in MiniTorch/ops/tanh.py
def tanh(x: Variable) -> Variable:
    return Tanh()(x)  # type: ignore[return-value]

Losses

mean_squared_error

mean_squared_error(x0: Variable, x1: Variable) -> Variable
Source code in MiniTorch/ops/meansquarederror.py
def mean_squared_error(x0: Variable, x1: Variable) -> Variable:
    return MeanSquaredError()(x0, x1)  # type: ignore[return-value]

softmax_cross_entropy

softmax_cross_entropy(x: Variable, t: Variable) -> Variable
Source code in MiniTorch/ops/softmax_cross_entropy.py
def softmax_cross_entropy(x: Variable, t: Variable) -> Variable:
    return SoftmaxCrossEntropy()(x, t)  # type: ignore[return-value]