- wrap numpy and cupy in matrix as np

- added type alias Mat for np.ndarray
This commit is contained in:
2025-12-18 18:14:03 +01:00
parent 9000e70607
commit c1c6c610ad
8 changed files with 51 additions and 47 deletions
+19 -8
View File
@@ -1,25 +1,36 @@
import numpy as np
import time
from typing import TypeAlias
USE_CUDA = 1
if USE_CUDA:
import cupy as np
Mat: TypeAlias = np.array
def convert(src: Mat):
return np.asnumpy(src)
else:
import numpy as np
Mat: TypeAlias = np.array
def convert(src: Mat):
return src
np.random.seed(int(time.monotonic()))
def uniform(shape: tuple, mu: float = 0.5, std: float = 1.0) -> np.ndarray:
def uniform(shape: tuple, mu: float = 0.5, std: float = 1.0) -> Mat:
return std * (np.random.rand(shape[0], shape[1]) + mu - 0.5)
def gaussian(shape: tuple, mu: float = 0.0, std: float = 1.0) -> np.ndarray:
def gaussian(shape: tuple, mu: float = 0.0, std: float = 1.0) -> Mat:
return std * (np.random.randn(shape[0], shape[1]) + mu)
def sample(src: np.ndarray) -> np.ndarray:
def sample(src: Mat) -> Mat:
return (src > uniform(src.shape)).astype(float)
def prob(src: np.ndarray) -> np.ndarray:
def prob(src: Mat) -> Mat:
return 1.0 / (1 + np.exp(-src))
def rms_error(d_err: np.ndarray):
def rms_error(d_err: Mat):
d_err_squared = d_err * d_err
return np.sum(d_err_squared, 1) / d_err_squared[1]
def rms_error_accu(d_err: np.ndarray):
def rms_error_accu(d_err: Mat):
d_err_squared = d_err * d_err
s = np.sum(d_err_squared) / d_err.size
return s