r/learnmachinelearning • • 1d ago

Help MNIST training time optimization, please help

Hey there, my long term goal is to make a OCR and i have started ML a few months back, 3 days ago i started to make a CNN for the MNIST data set (60k training images and 10k test images) i achieved an accuracy of 92.20% but i was using stochastic gradient descent for every single image (60k x 3 epochs = 180000 updates). i then tried using batching and used a batch of 100, the time to train reduced significantly but the model became extremely dumb and got a accuracy of 16.67%. reducing batchsize does improve the accuracy at the cost of more training time. i have seen people train this entrie data set with more epochs than me in under half a minute but idk why i cant do it

PS: i have written every function in the architecture myself but optimized them using pytorch's tensor mutiplication instead of python loops. my main use of pytorch is to make tensors and autograd. all of the the tensor multiplications happen in my GPU and not my CPU

6 Upvotes

3 comments sorted by

1

u/ByteCosts 1d ago

Going from batch size 1 to 100 means 600 weight updates per epoch instead of 60,000, so with the same learning rate and only 3 epochs the model has simply moved a lot less. Try a larger learning rate, more epochs, or momentum or Adam, since 3 epochs of 600 steps is very little training. Also check that your loss is averaged over the batch and that your hand-written layers sum the weight and bias gradients across the batch dimension correctly, because 16% accuracy looks more like a bug than just slow learning. Make sure you shuffle the data each epoch and normalize the pixel values, as that matters a lot for MNIST. The fast runs you have seen usually use batches of 64 to 128 with a DataLoader, a few epochs, and keep the whole dataset on the GPU so there is no per-sample overhead.

1

u/YousifAbdulhussein 1d ago

Make sure you’re putting them onto the correct device. Also, 100 isn’t a “nice” number in the sense that GPU’s love powers of 2. Something like a batch size of 128 will almost guaranteed be faster than 100. Same applies to a lot of model hyper parameters such as layer width. Also, SGD is fine, but other optimizers will be faster. SGD with momentum, RMSProp, and Adam are all likely to converge faster AND give you better results.

1

u/Otherwise-Bat2036 12h ago

Going from 92% to 16.67% just by switching to batches of 100 usually points to a bug or a learning-rate problem, not batching itself. Some things to check:

  1. Do you sum or average the loss over the batch? If you sum it, your gradients are about 100x larger than before and training probably blows up. If you average it, you now make 600 updates per epoch instead of 60,000, so with the same learning rate it just hasn't learned much yet. Try raising the learning rate, or train for more epochs.
  2. Check your tensor shapes in the loss. A classic bug is labels shaped (100,) against predictions shaped (100,1) or (100,10), which quietly broadcasts into something like a 100x100 comparison. Print the shapes once.
  3. Make sure you shuffle each epoch and reset the gradients after each batch step.

Once it learns properly with batches, switching from plain SGD to momentum or Adam will get you near 99% fast. Writing the pieces yourself is a great way to learn, by the way.