[refactor] move rbm.model → model.model

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-02 08:55:16 +02:00
co-authored by Claude Sonnet 4.6
parent b41274c7a6
commit 1954064a54
17 changed files with 19 additions and 19 deletions
View File
+46
View File
@@ -0,0 +1,46 @@
import os
from abc import ABC, abstractmethod
from rbm.entity import Entity
from rbm.train import train
from rbm.matrix import Mat, np
from rbm.status import Status
class Model(ABC):
known_classes = [Entity]
def __init__(self, name: str = "myStack", work_dir: str = "."):
self.name = name
self.work_dir = work_dir
os.makedirs(self.work_dir, exist_ok=True)
def objects(self, obj_type: type = Entity) -> list[Entity]:
obj_list: list[type[obj_type]] = []
for name, value in self.__dict__.items():
if isinstance(value, obj_type):
obj_list.append(value)
return obj_list
def train(self, batch: Mat, status: Status = None):
entities = self.objects(Entity)
_batch = np.array(batch) # np.array() converts numpy→CuPy; np.copy() does not
for entity in entities:
if entity.enable_training:
train(entity, _batch, status if status is not None else Status())
_batch = entity.forward(_batch)
def forward(self, x: Mat) -> Mat:
pass
def save(self):
for index, entity in enumerate(self.objects(Entity)):
filepath = os.path.join(self.work_dir, f"{self.name}-{index}-state.npz")
entity.state.save(filepath)
def load(self):
for index, entity in enumerate(self.objects(Entity)):
filepath = os.path.join(self.work_dir, f"{self.name}-{index}-state.npz")
entity.state.load(filepath)
def init(self, std: float):
for index, entity in enumerate(self.objects(Entity)):
entity.state.init(std=std)