commit stale changes

This commit is contained in:
2026-07-26 21:23:50 +02:00
parent 74371975ad
commit b534be9fad
2 changed files with 17 additions and 20 deletions
+15 -19
View File
@@ -24,10 +24,11 @@ from rbm.train import train
from rbm.status import Status
# ── Hyper-parameters ──────────────────────────────────────────────────────────
TEXT = "CHAPTER 1 LOOMINGS CALL ME ISHMAEL SOME YEARS AGO NEVER MIND HOW LONG PRECISELY HAVING LITTLE OR NO" # the character sequence to learn (matches diagram example)
WIN = 2 # sliding-window width (= N in the diagram)
#TEXT = "CHAPTER 1 LOOMINGS CALL ME ISHMAEL SOME YEARS AGO NEVER MIND HOW LONG PRECISELY HAVING LITTLE OR NO" # the character sequence to learn (matches diagram example)
TEXT = "0123456789 "
WIN = 3 # sliding-window width (= N in the diagram)
UNITS = 5
H_SIZE = 128 # hidden units per RBM cell
H_SIZE = 256 # hidden units per RBM cell
NUM_EPOCHS = 1000
TRAIN_PARAMS = TrainingParams(
@@ -114,7 +115,7 @@ class RnnModel(Model):
print(f"train[{index}:{i:03}]: {unit.name}:{vc2char(_vc[i,:])}")
train(unit, _vc, status)
# For the next unit: Update context portion of vc
_c = unit.forward(_vc)
# _c = unit.forward(_vc)
def forward_step(self, _ch: str, _state: str):
@@ -122,13 +123,13 @@ class RnnModel(Model):
_c = np.zeros([len(_ch), H_SIZE])
_ch_predict = '?'
for index, unit in enumerate(self.units):
_s = _state[index] + SPACE*(WIN-1)
_s = _state[index:index+WIN-1] + SPACE
print(f"forward_step in : {unit.name}:{_s}")
_v = str2vec(_s).reshape(1, WIN*vocab_size())
_vc = concat(_v, _c, axis=1)
_c = unit.forward(_vc)
_vc = unit.reconstruct(_c)
_r, _ = split(_vc, H_SIZE, axis=1)
# _c = unit.forward(_vc)
_vc_r = unit.reconstruct(_c)
_r, _ = split(_vc_r, H_SIZE, axis=1)
_r_cl = clamp(_r.reshape([WIN, vocab_size()]), axis=1)
_ch_predict = v2char(_r_cl)
@@ -143,7 +144,7 @@ if __name__ == "__main__":
if 1:
# Test of helper function
# ToDo: move to tests
vec = idx2vec(ch2idx("JENS"))
vec = idx2vec(ch2idx("1234"))
vec = clamp(vec, axis=1)
indices = vec2idx(vec, axis=1)
ch_str = idx2ch(indices)
@@ -162,20 +163,15 @@ if __name__ == "__main__":
# vc contains vis + context
# context will be updated after training
model.train(win_text, status=Status())
model.save()
# model.train(win_text, status=Status())
# model.save()
# test the model
seed_str = 'CHAP'
state = SPACE*UNITS
state = '01234'
result = seed_str
ch = SPACE
result = state
ch = '5'
# Prime
for s in seed_str:
state, ch = model.forward_step(s, state)
print(f'Prime: {ch}')
for i in range(len(TEXT)):
state, ch = model.forward_step(ch, state)
print(f'Predict: {ch}')
+2 -1
View File
@@ -1,7 +1,8 @@
from rbm.matrix import Mat, np
SPACE=' '
VOCAB = SPACE + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
#VOCAB = SPACE + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
VOCAB = SPACE + '0123456789'
_CH2IDX = {ch: i for i, ch in enumerate(VOCAB)}
def clamp(vec: Mat, axis=0):