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(12345) 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) -> Mat: return std * (np.random.randn(shape[0], shape[1]) + mu) def sample(src: Mat) -> Mat: return (src > uniform(src.shape)).astype(float) def prob(src: Mat) -> Mat: return 1.0 / (1 + np.exp(-src)) 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: Mat): d_err_squared = d_err * d_err s = np.sum(d_err_squared) / d_err.size return s