Compute blocks' std and backpropagation

I want to compute a block-based std on the image, but I don’t know which way is efficient and whether the gradient of std can be backpropagated.

For example, I have images: 10x1x20x20 (NCHW), I want to compute the std within 3x3 window on the images and compute each std in this 3x3 matrix and do backpropagation. Can anyone help me? Thanks!

You could use F.unfold to create these 3x3 windows from the image and compute the std for each of these windows.
You should be able to backpropagate through torch.std.

Thank you. I tried to use unfold, and then computed torch.std on each patch, but got nan…

Could you check the input values of this particular patch, which is creating the NaN output?
torch.std should return a zero, if the input is constant.