syna.core module

Core — Tensor and autograd primitives.

Core Tensor, Parameter, Config and autograd Function primitives used across the syna library. This module contains the lightweight Tensor container and the Function base class which implement forward/backward for automatic differentiation.

class syna.core.Config[source]

Bases: object

Global config flags affecting backprop and training behavior.

enable_backprop = True
train = True
class syna.core.Function[source]

Bases: object

Base Function for forward/backward ops used in autograd.

Subclasses should implement forward (numpy arrays -> numpy arrays) and backward (Tensor grads -> Tensor grads).

backward(*args: Tensor) Tensor | list[Tensor][source]
forward(*args: ndarray) ndarray[source]
class syna.core.Parameter(data, name: str | None = None)[source]

Bases: Tensor

A thin wrapper for trainable parameters (keeps API separate).

class syna.core.Tensor(data, name: str | None = None)[source]

Bases: object

Simple Tensor container holding data, gradient and creator Function.

Most operator behavior delegates to syna.functions.* helpers so this class focuses on bookkeeping for autograd.

property T
backward(retain_grad=False, create_graph=False) None[source]

Run backpropagation to compute gradients of inputs.

Args:

retain_grad: if False, intermediate gradients are cleared to save memory. create_graph: if True, create graph for higher-order gradients.

cleargrad() None[source]

Clear stored gradient.

property dtype
max(**kwargs)[source]
min(**kwargs)[source]
property ndim
reshape(*shape: int)[source]
set_creator(func: Function) None[source]

Mark this tensor as created by func (used for backprop ordering).

property shape
property size
sum(axis=None, keepdims=False)[source]
transpose(*axes: int)[source]
unchain() None[source]

Remove reference to creator to break the computational graph.

unchain_backward()[source]

Remove creators for all upstream tensors (useful for freeing graph).

syna.core.arange(start, stop=None, step=1, dtype=None, requires_grad: bool = False, name: str | None = None) Tensor[source]

Return a 1-D tensor with values from start to stop (exclusive).

syna.core.as_array(x) ndarray[source]

Convert scalars to numpy arrays; leave arrays unchanged.

syna.core.as_tensor(obj) Tensor[source]

Ensure obj is a Tensor; convert scalars/arrays to Tensor if needed.

Delegates to tensor(…), which follows PyTorch-style semantics.

syna.core.eye(n, m=None, dtype=None, requires_grad: bool = False, name: str | None = None) Tensor[source]

Return a 2-D identity matrix (or rectangular identity if m is given).

syna.core.full(shape, fill_value, dtype=None, requires_grad: bool = False, name: str | None = None) Tensor[source]

Return a tensor of shape shape filled with fill_value.

shape may be an int or a tuple/list of ints (matches numpy/torch style).

syna.core.full_like(x, fill_value, dtype=None, requires_grad: bool = False, name: str | None = None) Tensor[source]

Return a tensor with the same shape as x filled with fill_value.

x may be a Tensor or array-like.

syna.core.no_grad()[source]

Context manager to disable gradient tracking.

syna.core.ones(*shape, dtype=None, requires_grad: bool = False, name: str | None = None) Tensor[source]

Return a tensor filled with ones.

syna.core.ones_like(x, dtype=None, requires_grad: bool = False, name: str | None = None) Tensor[source]

Return a tensor of ones with the same shape as x (Tensor or array-like).

syna.core.rand(*shape, dtype=None, requires_grad: bool = False, name: str | None = None) Tensor[source]

Return a tensor with uniform random values in [0, 1).

syna.core.randint(low, high=None, size=None, dtype=None, requires_grad: bool = False, name: str | None = None) Tensor[source]

Return random integers from low (inclusive) to high (exclusive).

syna.core.randn(*shape, dtype=None, requires_grad: bool = False, name: str | None = None) Tensor[source]

Return a tensor with samples from the standard normal distribution.

syna.core.range(start, stop=None, step=1, dtype=None, requires_grad: bool = False, name: str | None = None) Tensor[source]

Alias for arange to match torch.range-like convenience (behaves like arange).

syna.core.tensor(data, dtype=None, requires_grad: bool = False, name: str | None = None) Tensor[source]

Tensor factory.

Args:

data: array-like or scalar. dtype: numpy dtype (optional). requires_grad: if True, the returned Tensor is a leaf that will accumulate gradients. name: optional name for the Tensor.

syna.core.test_mode()[source]

Context manager to set train flag to False.

syna.core.using_config(name: str, value: bool)[source]

Temporarily set a Config attribute inside a context.

syna.core.zeros(*shape, dtype=None, requires_grad: bool = False, name: str | None = None) Tensor[source]

Return a tensor filled with zeros.

syna.core.zeros_like(x, dtype=None, requires_grad: bool = False, name: str | None = None) Tensor[source]

Return a tensor of zeros with the same shape as x (Tensor or array-like).