Initial commit

This commit is contained in:
2025-12-16 14:52:36 +01:00
commit b6e94e75ac
11 changed files with 269 additions and 0 deletions
+50
View File
@@ -0,0 +1,50 @@
import numpy as np
from collections.abc import Callable
from params import RbmParams
from state import RbmState
from helper import sample, prob, uniform
class RbmLayer:
def __init__(self, name: str, num_visible, num_hidden, params: RbmParams):
self.name = name
self.state = RbmState.from_layer_params(num_visible, num_hidden)
self.params = params
self.state_filename = f"{self.name}_state.npz"
def save(self):
self.state.to_file(self.state_filename)
def load(self):
self.state = RbmState.from_file(self.state_filename)
def train_batch(self, v_states: np.ndarray, cd_func: Callable):
for epochs in range(self.params.num_epochs):
# Contrastive divergence learning: calculate gradients
cd_func(v_states, dwhv, dbh, dbv);
if __name__ == "__main__":
p = RbmParams()
l1 = RbmLayer("Layer_0", 2, 3, p)
l1.save()
l1.load()
l1.state.init(mu=0.5, std=1.0)
s1 = RbmState(l1.state.w_hv, l1.state.b_v, l1.state.b_h)
v0 = uniform((1,2))
h0 = uniform((1,3))
h1 = l1.v_to_h(v0)
print(h1.shape)
v1 = l1.h_to_v(h1)
s_v1 = sample(v1)
p_h = prob(s_v1)
print(v1.shape)