r/learnmachinelearning • u/Weird-Drawer-805 • 6h 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
1
u/ByteCosts 5h 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/Mad-riffer 4h ago
Oh boy. I learned how to train CNNs from scratch on a core 2 duo and 4GB of ram (no gpu). I hated waiting and optimized to the point of only 2-3 minutes training time.
The most critical part of getting a good model for MNIST is data augmentation. This isn't exactly what you're asking but is extremely important for a high quality model. It allows you to use a lower parameter count for your model, as well as some other stuff. Some references for what I was doing: https://www.kaggle.com/code/babbler/mnist-data-augmentation-with-elastic-distortion
Now I can't tell what is causing your slowness, but I would first look at:
- how many params are in your model
- are you switching between CPU and GPU calculations during training? Sometimes it isn't obvious, perhaps the optimizer is using CPU but the model is on GPU
- sgd is slower to converge (but it is not bad for this task, what learning rate are you using? are you also using momentum?)
1
u/YousifAbdulhussein 4h 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.