- 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
+6 -7
View File
@@ -1,8 +1,7 @@
import numpy as np
from collections.abc import Callable
from params import RbmParams
from state import RbmState
from matrix import sample, prob, rms_error_accu
from matrix import sample, prob, rms_error_accu, Mat, np
from status import Status
class Entity:
@@ -10,7 +9,7 @@ class Entity:
self.state = RbmState.from_layer_params(shape)
self.params = params
def train(self, batch: np.ndarray, cd_func: Callable, status: Status):
def train(self, batch: Mat, cd_func: Callable, status: Status):
training_remain = batch.shape[0]
batch_size = min(self.params.mini_batch_size, training_remain)
if batch_size == 0:
@@ -65,14 +64,14 @@ class Entity:
status.on_change({"progress": {"value": round(progress), "unit": "%"},
"err_rms_total": {"value": err_rms, "unit": ""}})
def v_to_ph(self, v: np.ndarray) -> np.ndarray:
def v_to_ph(self, v: Mat) -> Mat:
state = self.state.v_to_h(v)
if self.params.do_gaussian_hidden:
return state
return prob(state)
def h_to_pv(self, h: np.ndarray) -> np.ndarray:
def h_to_pv(self, h: Mat) -> Mat:
state = self.state.h_to_v(h)
if self.params.do_gaussian_visible:
return state
@@ -80,7 +79,7 @@ class Entity:
return prob(state)
def gibbs_v_to_h(self, v: np.ndarray) -> np.ndarray:
def gibbs_v_to_h(self, v: Mat) -> Mat:
h = self.v_to_ph(v)
for i in range(self.params.num_gibbs_samples-1):
h = self.h_to_pv(h)
@@ -88,7 +87,7 @@ class Entity:
return h
def gibbs_h_to_v(self, h: np.ndarray) -> np.ndarray:
def gibbs_h_to_v(self, h: Mat) -> Mat:
v = self.h_to_pv(h)
for i in range(self.params.num_gibbs_samples-1):
v = self.v_to_ph(v)