Backend Numpy
NumPy backend implementation for tiny-pytorch.
This module provides a NumPy-based backend for tiny-pytorch, implementing device abstractions and tensor operations using NumPy arrays. It serves as a simple, pure Python backend that's useful for development and testing.
Classes:
-
Device
–Base class for device abstractions.
-
CPUDevice
–CPU device implementation using NumPy arrays.
Functions:
-
cpu
–Returns a CPU device instance.
-
default_device
–Returns the default device (CPU).
-
all_devices
–Returns a list of all available devices.
CPUDevice
Bases: Device
Represents data that sits in CPU, using NumPy as backend.
This device implementation uses NumPy arrays for all tensor operations. It provides methods for creating tensors with different initializations and distributions.
Methods:
-
zeros
–Create a tensor filled with zeros.
-
ones
–Create a tensor filled with ones.
-
randn
–Create a tensor with random values from standard normal distribution.
-
rand
–Create a tensor with random values from uniform distribution.
-
one_hot
–Create a one-hot encoded tensor.
__eq__(other)
Check if this device equals another device.
__hash__()
Hash of the device.
__repr__()
String representation of the device.
one_hot(n, i, dtype='float32')
Create a one-hot encoded tensor.
Parameters:
-
n
(int
) –Number of columns (classes).
-
i
(int or list[int]
) –Number of one-hot vectors (rows) or indices to encode.
-
dtype
(str
, default:'float32'
) –The data type of the tensor. Default is "float32".
Returns:
-
ndarray
–A one-hot encoded tensor.
ones(shape, dtype='float32')
Create a tensor filled with ones.
Parameters:
-
shape
(int or Sequence[int]
) –The shape of the tensor to create.
-
dtype
(str
, default:'float32'
) –The data type of the tensor. Default is "float32".
Returns:
-
ndarray
–A tensor filled with ones.
rand(*shape)
Create a tensor with random values from uniform distribution.
Parameters:
-
*shape
(int
, default:()
) –The shape of the tensor to create.
Returns:
-
ndarray
–A tensor with random values from U[0, 1).
randn(*shape)
Create a tensor with random values from standard normal distribution.
Parameters:
-
*shape
(int
, default:()
) –The shape of the tensor to create.
Returns:
-
ndarray
–A tensor with random values from N(0, 1).
zeros(shape, dtype='float32')
Create a tensor filled with zeros.
Parameters:
-
shape
(int or Sequence[int]
) –The shape of the tensor to create.
-
dtype
(str
, default:'float32'
) –The data type of the tensor. Default is "float32".
Returns:
-
ndarray
–A tensor filled with zeros.
Device
Base class for device abstractions.
This class defines the interface that all device implementations must follow. Devices represent where tensor data is stored and provide methods for creating tensors on that device.
all_devices()
Returns a list of all available devices.
Returns:
-
list[CPUDevice]
–A list containing the CPU device.