- wrap numpy and cupy in matrix as np
- added type alias Mat for np.ndarray
This commit is contained in:
+19
-8
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user