refactored forward_step

This commit is contained in:
2026-06-04 19:30:58 +02:00
parent 7f6818670e
commit 06ab8d0166
+9 -9
View File
@@ -24,13 +24,13 @@ from rbm.train import train
from rbm.status import Status
# ── Hyper-parameters ──────────────────────────────────────────────────────────
TEXT = " HALLO SUPER JENS. SUPPE! " # the character sequence to learn (matches diagram example)
TEXT = " HALLO SUPER JENS!" # the character sequence to learn (matches diagram example)
WIN = 3 # sliding-window width (= N in the diagram)
STRIDE = 1 # sliding-window step size
UNITS = 1
H_SIZE = 128 # hidden units per RBM cell
NUM_EPOCHS = 100
NUM_ITERATIONS = 100
NUM_EPOCHS = 1000
NUM_ITERATIONS = 1
TRAIN_PARAMS = TrainingParams(
learning_rate = 0.25,
@@ -84,11 +84,12 @@ class RnnModel(Model):
_c = unit.forward(vc)
vc[1:, WIN*vocab_size():] = _c[0:-1,:]
def forward_step(self, _vc: Mat):
def forward_step(self, _v: Mat, _c: Mat) -> tuple[Mat, Mat]:
_vc = concat(_v.reshape([1, WIN*vocab_size()]), _c.reshape([1, H_SIZE]), axis=1)
for unit in self.units:
_c = unit.forward(_vc)
_vc = unit.reconstruct(_c)
return _vc
return split(_vc.flatten(), H_SIZE)
def forward(self, x: Mat):
pass
@@ -128,10 +129,9 @@ if __name__ == "__main__":
v_forward = seed.flatten()
context = c_train[0]
for i in range(len(TEXT)+5):
print(f"Forward {i:02d}: {vec2str(v_forward.reshape([WIN, vocab_size()]), axis=1)}")
vc_forward = concat(v_forward.reshape([1, WIN*vocab_size()]), context.reshape([1, H_SIZE]), axis=1)
vc_predict = model.forward_step(vc_forward)
v_predict, context = split(vc_predict.flatten(), H_SIZE)
v_mat = v_forward.reshape([WIN, vocab_size()])
print(f"Forward {i:02d}: {vec2str(v_mat, axis=1)}")
v_predict, context = model.forward_step(v_mat, context)
v_predict_clamp = clamp(v_predict.reshape([WIN, vocab_size()]), axis=1)
print(f"Predict {i:02d}: {vec2str(v_predict_clamp, axis=1)}")
v_forward = shift_left(v_predict_clamp.reshape([1, WIN*vocab_size()]), vocab_size())