How does backpropagation works through Max Pooling layer when doing a batch?
When a neural network processes a batch, all activation values for each layer are calculated for each example (maybe in parallel per example if library and hardware support it). Those values are stored for possible later use - i.e. one value per activation per example in the batch, they are not aggregated in any way

During back propagation, those activation values are used as one of the numerical sources to calculate gradients, along with gradients calculated so far working backwards and the connecting weights. Like forward propagation, back propagation is applied per example, it does not work with averaged or summed values. Only when all examples have been processed do you work with the summed or averaged gradients for the batch.

This applies equally to max pool layers. Not only do you know what the output from the pooling layer for each example in the batch was, but you can look at the preceding layer and determine which input to the pool was the maximum. 

Mathematically, and avoiding the need to define indices for NN layers and neurons, the rule can be expressed like this


The forward function is $m = max(a,b)$
We know $\frac{\partial J}{\partial m}$ for some target function J (in the neural network that will be the loss function we want to minimise, and we are assuming we have backpropagated to this point already)
We want to know $\frac{\partial J}{\partial a}$ and $\frac{\partial J}{\partial b}$
If $a &gt; b$


Locally,* $m = a$. So $\frac{\partial J}{\partial a} = \frac{\partial J}{\partial m}$
Locally,* $m$ does not depend on $b$. So $\frac{\partial J}{\partial b} = 0$

Therefore $\frac{\partial J}{\partial a} = \frac{\partial J}{\partial m}$ if $a &gt; b$, else $\frac{\partial J}{\partial a} = 0$
and $\frac{\partial J}{\partial b} = \frac{\partial J}{\partial m}$ if $b &gt; a$, else $\frac{\partial J}{\partial b} = 0$


When back propagation goes across a max pooling layer, the gradient is processed per example and assigned only to the input from the previous layer that was the maximum. Other inputs get zero gradient. When this is batched it is no different, it is just processed per example, maybe in parallel. Across a whole batch this can mean that more than one, maybe all, of the input activations to the max pool get some share of the gradient - each from a different subset of examples in the batch.



* Locally -> when making only infinitesimal changes to $m$. 

** Technically, if $a=b$ exactly then we have a discontinuity, but in practice we can ignore that without issues when training a neural network.