[bugfix] - shuffle data each epoch; replace if chains with elif/else in train()

Shuffling eliminates systematic gradient bias from fixed mini-batch ordering.
elif/else raises ValueError for unrecognised entity types instead of silently
calling None and crashing with a cryptic TypeError.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-30 20:43:42 +02:00
co-authored by Claude Sonnet 4.6
parent 96f271f160
commit 003152518a
+6 -4
View File
@@ -203,21 +203,23 @@ def train(entity: Entity, batch: Mat, status: Status):
progress = 0
keep_running = True
status.on_change(entity)
cd_func = None
if entity.type == Entity.Type.GB_RBM:
cd_func = cd_gaussian_binary
if entity.type == Entity.Type.BB_RBM:
elif entity.type == Entity.Type.BB_RBM:
cd_func = cd_binary_binary
if entity.type == Entity.Type.GG_RBM:
elif entity.type == Entity.Type.GG_RBM:
cd_func = cd_gaussian_gaussian
if entity.type == Entity.Type.BG_RBM:
elif entity.type == Entity.Type.BG_RBM:
cd_func = cd_binary_gaussian
else:
raise ValueError(f"Unknown entity type: {entity.type}")
entity.grad_zero()
for epochs in range(params.num_epochs):
if not keep_running:
break
batch = batch[np.random.permutation(batch.shape[0])]
for mini_batch in to_mini_batch(batch, mini_batch_size):
# Contrastive divergence learning: calculate gradients
dwhv, dbv, dbh = cd_func(entity, mini_batch)