Process images in a mini-batch one at a time
I was wondering if it is possible in CNTK to process images in a mini-batch one at a time. In our case, the images we are dealing with can be quite large the GPU memory is a constrain. To save memory, I would like to be able to process images of a mini-batch one at a time. As a workaround, I'm currently specifying a mini-batch size of 1, but I'm worried that this will increase the noise in the gradient calculation, so I would like to be able to use the average gradient over multiple images, while still processing each image at a time. Is this something that is already support or is this a feature you might be interested in implementing in the future?
hi e-thereal, currently the only approach is to set minibatch size to 1 (and the gradient is calculated per minibatch). To the best of my knowledge, there are no plans to change this behavior or support splitting of minibatches to smaller units from the reading perspective. Thanks!
@eldakms Thank you for your answer.
I did some digging in the source code and I found two options in SGD.cpp, which seem to be related, namely: maxSamplesInRAM and numSubminibatches. Both parameters seem complementary. When I'm setting numSubminibatches to be equal to the number of images per mini-batch, training consumes a little bit more memory than when using a minibatch size of 1, so every images is likely processed one after another. Alternatively, I can set numSubminibatches = -1 and maxSamplesInRAM = 1, which has the same effect. Only setting maxSamplesInRAM = 1 without setting numSubminibatches seems to have no effect. Can anybody please comment if those parameters are safe to use and what their expected behaviour is? Thanks.
I've realized that some evaluation nodes produce wrong results when using numSubminibatches or maxSamplesInRam and shareNodeValueMatrices = true. I haven't further investigated this issue since the criterion node seem to produce the same result, so training is likely OK. I can investigate further if there is interest (@frankseide or @zhouwangzw maybe?) otherwise I'll close this issue.
I would personally not worry about noise in the gradient. We ran experiments where I reduced MB size from 1024 to 16 (in a speech task) (paper, Figure 1) and found near-identical convergence behavior--under one assumption: That the MB gradient is not an average but a sum over per-sample gradients. This is what CNTK does.
Based on these experiments, I have come to a different interpretation from others, which often consider it important to reduce the variance of the gradient. Instead, CNTK simply considers the minibatch size the number of samples between model updates; and as long as the model is not too far out of date, it is OK to skip the update. This assumption has so far not been shown to be incorrect.