Implement mini-batch gradient descent for faster training#1
Open
Implement mini-batch gradient descent for faster training#1
Conversation
This commit introduces mini-batch training to significantly improve training performance. Instead of updating weights after each sample, gradients are accumulated over a batch and applied once per batch. Changes: - Added gradient accumulation to Linear layer - New zeroGradients() method to reset gradients at batch start - Modified backward() to accumulate gradients instead of overwriting - Updated applyGradients() to average gradients by batch size - Updated Network training loop - Added batch_size parameter to train() - Process data in configurable mini-batches - Zero gradients at start of each batch - Accumulate gradients over batch samples - Apply averaged gradients once per batch - Updated all train() calls to include batch_size parameter - main.zig: Uses batch_size=32 for Iris dataset - net.zig test: Uses batch_size=1 for backward compatibility - README.md: Updated example with batch_size=32 Benefits: - Faster training through reduced weight update overhead - More stable gradient estimates from batch averaging - Better convergence properties - Configurable batch size for different datasets The default batch_size of 32 provides a good balance between speed and gradient stability for most datasets.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This commit introduces mini-batch training to significantly improve
training performance. Instead of updating weights after each sample,
gradients are accumulated over a batch and applied once per batch.
Changes:
Added gradient accumulation to Linear layer
Updated Network training loop
Updated all train() calls to include batch_size parameter
Benefits:
The default batch_size of 32 provides a good balance between
speed and gradient stability for most datasets.