- added training params as list for model.train()

- added gaussian sample
- introduced layout concept
- updated README
This commit is contained in:
2025-12-31 16:13:59 +01:00
parent 9b41dd6c02
commit 38b834c640
6 changed files with 179 additions and 5 deletions
+29
View File
@@ -0,0 +1,29 @@
import numpy as np
from .entity import Entity
from .matrix import Mat
from .train import train, cd_jens, TrainingParams
from .status import Status
class Horizontal:
def __init__(self, entities: list[Entity]):
self.units: list[Entity] = entities
def forward(self, x: Mat):
res: Mat = Mat([])
for unit in self.units:
x = unit.forward(x)
res = np.concat((res, x))
def reconstruct(self, x: Mat):
res: Mat = Mat([])
for unit in self.units:
x = unit.forward(x)
res = np.concat((res, x))
def train(self, batch: Mat, params: TrainingParams):
_batch = np.copy(batch)
for unit in self.units:
train(unit, _batch, params, Status(), cd_jens)
_batch = unit.forward(_batch, num_gibbs=params.num_gibbs_samples)
+8 -1
View File
@@ -18,11 +18,18 @@ 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)
if shape.__len__() == 1:
return std * (np.random.randn(shape[0]) + mu)
else:
return std * (np.random.randn(shape[0], shape[1]) + mu)
def sample(src: Mat) -> Mat:
return (src > uniform(src.shape)).astype(float)
def gaussian_sample(src: Mat, mu=0, std=1.0) -> Mat:
return gaussian(src.shape, mu, std)
def prob(src: Mat) -> Mat:
return 1.0 / (1 + np.exp(-src))
+7 -4
View File
@@ -20,11 +20,14 @@ class Model(ABC):
obj_list.append(value)
return obj_list
def train(self, batch: Mat, params: TrainingParams):
def train(self, batch: Mat, params: TrainingParams|list[TrainingParams]):
entities = self.objects(Entity)
if isinstance(params, TrainingParams):
params = [params]*len(entities)
_batch = np.copy(batch)
for entity in self.objects(Entity):
train(entity, _batch, params, Status(), cd_jens)
_batch = entity.forward(_batch, num_gibbs=params.num_gibbs_samples)
for entity, param in zip(entities, params):
train(entity, _batch, param, Status(), cd_jens)
_batch = entity.forward(_batch, num_gibbs=param.num_gibbs_samples)
@abstractmethod
def forward(self, x: Mat) -> Mat: