[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:
+6
-4
@@ -203,21 +203,23 @@ def train(entity: Entity, batch: Mat, status: Status):
|
|||||||
progress = 0
|
progress = 0
|
||||||
keep_running = True
|
keep_running = True
|
||||||
status.on_change(entity)
|
status.on_change(entity)
|
||||||
cd_func = None
|
|
||||||
if entity.type == Entity.Type.GB_RBM:
|
if entity.type == Entity.Type.GB_RBM:
|
||||||
cd_func = cd_gaussian_binary
|
cd_func = cd_gaussian_binary
|
||||||
if entity.type == Entity.Type.BB_RBM:
|
elif entity.type == Entity.Type.BB_RBM:
|
||||||
cd_func = cd_binary_binary
|
cd_func = cd_binary_binary
|
||||||
if entity.type == Entity.Type.GG_RBM:
|
elif entity.type == Entity.Type.GG_RBM:
|
||||||
cd_func = cd_gaussian_gaussian
|
cd_func = cd_gaussian_gaussian
|
||||||
if entity.type == Entity.Type.BG_RBM:
|
elif entity.type == Entity.Type.BG_RBM:
|
||||||
cd_func = cd_binary_gaussian
|
cd_func = cd_binary_gaussian
|
||||||
|
else:
|
||||||
|
raise ValueError(f"Unknown entity type: {entity.type}")
|
||||||
|
|
||||||
entity.grad_zero()
|
entity.grad_zero()
|
||||||
for epochs in range(params.num_epochs):
|
for epochs in range(params.num_epochs):
|
||||||
if not keep_running:
|
if not keep_running:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
batch = batch[np.random.permutation(batch.shape[0])]
|
||||||
for mini_batch in to_mini_batch(batch, mini_batch_size):
|
for mini_batch in to_mini_batch(batch, mini_batch_size):
|
||||||
# Contrastive divergence learning: calculate gradients
|
# Contrastive divergence learning: calculate gradients
|
||||||
dwhv, dbv, dbh = cd_func(entity, mini_batch)
|
dwhv, dbv, dbh = cd_func(entity, mini_batch)
|
||||||
|
|||||||
Reference in New Issue
Block a user