syna.utils.util module

Utilities — helper functions used across syna.

Utility functions used across the syna library: graph plotting, numerical grad checks, array helpers, convolution dimension helpers, file caching, and a few tensor-related utilities.

syna.utils.util.array_allclose(a, b, rtol: float = 0.0001, atol: float = 1e-05) bool[source]

Compare two arrays (or Tensors) for approximate equality.

syna.utils.util.array_equal(a, b) bool[source]

Compare two arrays (or Tensors) for exact equality.

syna.utils.util.get_file(url: str, file_name: str | None = None) str[source]

Download url to the local cache directory and return the local path. If file exists in cache, return the cached path without downloading.

syna.utils.util.gradient_check(f, x, *args, rtol: float = 0.0001, atol: float = 1e-05, **kwargs) bool[source]

Check gradients of function f w.r.t. tensor x using numerical approximation. f should accept and return syna.Tensor. Returns True if gradients match.

syna.utils.util.logsumexp(x: ndarray, axis: int = 1) ndarray[source]

Stable log-sum-exp along an axis. Returns array with summed axis kept.

syna.utils.util.max_backward_shape(x: ndarray, axis: int | Tuple[int, ...] | None) list[source]

Compute the shape of gradient for max reduction so the result can be broadcast back to x.

syna.utils.util.numerical_grad(f, x, *args, **kwargs) ndarray[source]

Compute numerical gradient of f at x using central differences. x can be a syna.Tensor or a numpy array.

syna.utils.util.pair(x: int | Tuple[int, int]) Tuple[int, int][source]

Ensure x is a pair (tuple of two ints).

syna.utils.util.reshape_sum_backward(gy: ndarray, x_shape: Tuple[int, ...], axis: int | Tuple[int, ...] | None, keepdims: bool) ndarray[source]

Reshape gy (gradient of a reduced array) to match the original input shape x_shape before reduction.

syna.utils.util.show_progress(block_num: int, block_size: int, total_size: int)[source]

Simple progress bar used by urllib.request.urlretrieve.

syna.utils.util.sum_to(x: ndarray, shape: Tuple[int, ...]) ndarray[source]

Sum elements of array x so that the result has shape shape. This implements broadcasting-compatible sum reduction.