Skip to content

init

sorix.nn.init

uniform_

uniform_(tensor, a=0.0, b=1.0)

Fills the input tensor with values drawn from the uniform distribution U(a, b).

Source code in sorix/nn/init.py
def uniform_(tensor: Any, a: float = 0.0, b: float = 1.0) -> Any:
    """Fills the input tensor with values drawn from the uniform distribution U(a, b)."""
    xp = get_xp(tensor)
    tensor.data = xp.random.uniform(a, b, size=tensor.shape)
    return tensor

normal_

normal_(tensor, mean=0.0, std=1.0)

Fills the input tensor with values drawn from the normal distribution N(mean, std^2).

Source code in sorix/nn/init.py
def normal_(tensor: Any, mean: float = 0.0, std: float = 1.0) -> Any:
    """Fills the input tensor with values drawn from the normal distribution N(mean, std^2)."""
    xp = get_xp(tensor)
    tensor.data = xp.random.normal(mean, std, size=tensor.shape)
    return tensor

constant_

constant_(tensor, val)

Fills the input tensor with the value val.

Source code in sorix/nn/init.py
def constant_(tensor: Any, val: float) -> Any:
    """Fills the input tensor with the value val."""
    xp = get_xp(tensor)
    tensor.data = xp.full(tensor.shape, val)
    return tensor

zeros_

zeros_(tensor)

Fills the input tensor with the scalar value 0.

Source code in sorix/nn/init.py
def zeros_(tensor: Any) -> Any:
    """Fills the input tensor with the scalar value 0."""
    return constant_(tensor, 0.0)

ones_

ones_(tensor)

Fills the input tensor with the scalar value 1.

Source code in sorix/nn/init.py
def ones_(tensor: Any) -> Any:
    """Fills the input tensor with the scalar value 1."""
    return constant_(tensor, 1.0)

xavier_uniform_

xavier_uniform_(tensor, gain=1.0)

Fills the input tensor with values according to the Xavier uniform initialization.

Source code in sorix/nn/init.py
def xavier_uniform_(tensor: Any, gain: float = 1.0) -> Any:
    """Fills the input tensor with values according to the Xavier uniform initialization."""
    xp = get_xp(tensor)
    fan_in, fan_out = _calculate_fan_in_and_fan_out(tensor)
    std = gain * xp.sqrt(6.0 / (fan_in + fan_out))
    return uniform_(tensor, -std, std)

xavier_normal_

xavier_normal_(tensor, gain=1.0)

Fills the input tensor with values according to the Xavier normal initialization.

Source code in sorix/nn/init.py
def xavier_normal_(tensor: Any, gain: float = 1.0) -> Any:
    """Fills the input tensor with values according to the Xavier normal initialization."""
    xp = get_xp(tensor)
    fan_in, fan_out = _calculate_fan_in_and_fan_out(tensor)
    std = gain * xp.sqrt(2.0 / (fan_in + fan_out))
    return normal_(tensor, 0.0, std)

kaiming_uniform_

kaiming_uniform_(
    tensor, a=0, mode="fan_in", nonlinearity="leaky_relu"
)

Fills the input tensor with values according to the Kaiming uniform initialization.

Source code in sorix/nn/init.py
def kaiming_uniform_(tensor: Any, a: float = 0, mode: str = 'fan_in', nonlinearity: str = 'leaky_relu') -> Any:
    """Fills the input tensor with values according to the Kaiming uniform initialization."""
    xp = get_xp(tensor)
    fan = _calculate_correct_fan(tensor, mode)
    gain = _calculate_gain(nonlinearity, a)
    std = gain / xp.sqrt(fan)
    bound = xp.sqrt(3.0) * std  
    return uniform_(tensor, -bound, bound)

kaiming_normal_

kaiming_normal_(
    tensor, a=0, mode="fan_in", nonlinearity="leaky_relu"
)

Fills the input tensor with values according to the Kaiming normal initialization.

Source code in sorix/nn/init.py
def kaiming_normal_(tensor: Any, a: float = 0, mode: str = 'fan_in', nonlinearity: str = 'leaky_relu') -> Any:
    """Fills the input tensor with values according to the Kaiming normal initialization."""
    xp = get_xp(tensor)
    fan = _calculate_correct_fan(tensor, mode)
    gain = _calculate_gain(nonlinearity, a)
    std = gain / xp.sqrt(fan)
    return normal_(tensor, 0.0, std)