Out of memory when I need so many ouputs at the same time

Suppose that I have 30K images , the input of my nerwork is an image,and the output is a scalar. According to the general practice, I should divide the 30K images into many batches, and calculate the output separately. But now, I need to calculate softmax of the total 30K outputs, and the loss backward. So out of memory. What should I do?

Where is your code …, ???