Skip to content

Fix spurious empty final batch when ntrain is a multiple of batch_size - #3

Open
rezanematpour wants to merge 2 commits into
milenavuletic:mainfrom
rezanematpour:fix/empty-last-batch
Open

Fix spurious empty final batch when ntrain is a multiple of batch_size#3
rezanematpour wants to merge 2 commits into
milenavuletic:mainfrom
rezanematpour:fix/empty-last-batch

Conversation

@rezanematpour

Copy link
Copy Markdown

Problem

Across all training loop functions in Fin-GAN-online.py (e.g., TrainLoopForGAN, and the various TrainLoopMain* variants), batch count is computed as:

nbatches = ntrain//batch_size+1

This unconditionally assumes there is always a remainder batch to add. When ntrain is an exact multiple of batch_size, this creates one extra, spurious batch whose size is computed as:

curr_batch_size = totlen - i*batch_size

which evaluates to 0 for that final batch. This produces empty-tensor batches (e.g., condition/real of shape (0, ...), zero-batch LSTM hidden states h_0/c_0), which can break the forward pass and downstream indexing such as disc_fake_pred[0][0][0] used for logging.

Example

With ntrain=1000, batch_size=100:

  • nbatches = 1000//100 + 1 = 11
  • Batches i=0..9 correctly cover all 1000 rows (size 100 each)
  • Batch i=10 gets curr_batch_size = 1000 - 1000 = 0 — an empty batch that shouldn't exist

Fix

Only add the extra batch when a remainder actually exists:

nbatches = ntrain//batch_size + (1 if ntrain % batch_size != 0 else 0)

This is a minimal, behavior-preserving change:

  • No effect when ntrain % batch_size != 0 (the common case) — same batch count and sizes as before.
  • Fixes the edge case when ntrain % batch_size == 0 — no more empty final batch.

Applied consistently to all occurrences of this pattern in Fin-GAN-online.py.

Testing

Verified the corrected formula against both cases:

  • ntrain=1050, batch_size=100 → 11 batches, sizes [100]*10 + [50] (unchanged from original behavior)
  • ntrain=1000, batch_size=100 → 10 batches, sizes [100]*10, no empty batch (previously would have produced an 11th, empty batch)

No other logic was changed.

nbatches = ntrain//batch_size+1 unconditionally adds an extra batch, which becomes an empty (curr_batch_size=0) batch whenever ntrain is an exact multiple of batch_size. Fixed by only adding the extra batch when a remainder actually exists.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant