# 4d tensor mean by columns

Hi,

I have a 4d tensor with a shape batch_data = `(b, c, h, w)`.

I want to center each sample by subtracting the mean of each column (`w`) for every channel. That means I need to apply `batch_data - batch_data.mean(dim=(???)).unsqueez(???)` such that the shape of the mean data will be `(b, c, h, w)`, and each sample’s channel will have an `(h, w)` tensor where, for each `h`, all `w`’s are the same.

I hope this question is clear Thank you

Hi Rane!

If I understand you correctly, you wish to adjust your original tensor so that
for any `b` and `c`, the mean over the `w` dimension will be independent of `h`
(but will still depend on `b` and `c`).

You could try something like:

``````>>> import torch
>>> torch.__version__
'1.13.0'
>>> _ = torch.manual_seed (2022)
>>> t = torch.rand (2, 2, 3, 5)
>>> u = t - t.mean (dim = 3, keepdim = True) + t.mean (dim = (2, 3), keepdim = True)
>>> u.mean (dim = 3)
tensor([[[0.5798, 0.5798, 0.5798],
[0.4724, 0.4724, 0.4724]],

[[0.5582, 0.5582, 0.5582],
[0.5791, 0.5791, 0.5791]]])
>>> torch.allclose (u - u.mean (dim = 3, keepdim = True), t - t.mean (dim = 3, keepdim = True))
True
``````

Best.

K. Frank