Skip to content

Factory Functions

Factory functions create Tensor objects with common initializations without requiring raw NumPy arrays. They are re-exported at the top-level simplegrad namespace, so you can call sg.zeros(...), sg.ones(...), etc. directly. All factories accept an optional device argument and respect the global default dtype.

import simplegrad as sg

w = sg.normal((4, 8), requires_grad=True)   # Gaussian-initialised weights
b = sg.zeros((4,), requires_grad=True)      # zero bias
x = sg.uniform((2, 8), low=-1, high=1)     # random input

zeros(shape: tuple[int, ...], dtype: str = 'float32', comp_grad: bool | None = None, label: bool | None = None, device: str | None = None) -> Tensor

Create a tensor filled with zeros.

Parameters:

  • shape (tuple[int, ...]) –

    Output shape.

  • dtype (str, default: 'float32' ) –

    Data type string. Defaults to "float32".

  • comp_grad (bool | None, default: None ) –

    Enable gradient tracking. Defaults to the global flag.

  • label (bool | None, default: None ) –

    Optional name for visualization.

  • device (str | None, default: None ) –

    Device string. Defaults to the current global default device.

ones(shape: tuple[int, ...], dtype: str = 'float32', comp_grad: bool | None = None, label: bool | None = None, device: str | None = None) -> Tensor

Create a tensor filled with ones.

Parameters:

  • shape (tuple[int, ...]) –

    Output shape.

  • dtype (str, default: 'float32' ) –

    Data type string. Defaults to "float32".

  • comp_grad (bool | None, default: None ) –

    Enable gradient tracking. Defaults to the global flag.

  • label (bool | None, default: None ) –

    Optional name for visualization.

  • device (str | None, default: None ) –

    Device string. Defaults to the current global default device.

normal(shape: tuple[int, ...], dtype: str = 'float32', comp_grad: bool | None = None, label: bool | None = None, mu: int | float = 0, sigma: int | float = 1, device: str | None = None) -> Tensor

Create a tensor sampled from a normal distribution N(mu, sigma).

Parameters:

  • shape (tuple[int, ...]) –

    Output shape.

  • dtype (str, default: 'float32' ) –

    Data type string. Defaults to "float32".

  • comp_grad (bool | None, default: None ) –

    Enable gradient tracking. Defaults to the global flag.

  • label (bool | None, default: None ) –

    Optional name for visualization.

  • mu (int | float, default: 0 ) –

    Distribution mean.

  • sigma (int | float, default: 1 ) –

    Distribution standard deviation.

  • device (str | None, default: None ) –

    Device string. Defaults to the current global default device.

uniform(shape: tuple[int, ...], dtype: str = 'float32', comp_grad: bool | None = None, label: bool | None = None, low: int | float = 0, high: int | float = 1, device: str | None = None) -> Tensor

Create a tensor sampled from a uniform distribution U(low, high).

Parameters:

  • shape (tuple[int, ...]) –

    Output shape.

  • dtype (str, default: 'float32' ) –

    Data type string. Defaults to "float32".

  • comp_grad (bool | None, default: None ) –

    Enable gradient tracking. Defaults to the global flag.

  • label (bool | None, default: None ) –

    Optional name for visualization.

  • low (int | float, default: 0 ) –

    Lower bound of the distribution.

  • high (int | float, default: 1 ) –

    Upper bound of the distribution.

  • device (str | None, default: None ) –

    Device string. Defaults to the current global default device.

full(shape: tuple[int, ...], fill_value: float, dtype: str = 'float32', comp_grad: bool | None = None, label: bool | None = None, device: str | None = None) -> Tensor

Create a tensor filled with a constant value.

Parameters:

  • shape (tuple[int, ...]) –

    Output shape.

  • fill_value (float) –

    Value to fill the tensor with.

  • dtype (str, default: 'float32' ) –

    Data type string. Defaults to "float32".

  • comp_grad (bool | None, default: None ) –

    Enable gradient tracking. Defaults to the global flag.

  • label (bool | None, default: None ) –

    Optional name for visualization.

  • device (str | None, default: None ) –

    Device string. Defaults to the current global default device.