How about this? But I’m not sure if you want the mean of all dims together or separately for the channels.
a = torch.tensor(
[[[3,4,0],[0,2,3],[1,3,0]],
[[1,3,0],[0,2,1],[1,2,0]],
[[3,3,0],[0,1,3],[1,2,0]]], dtype=torch.float)

a[a==0] = a.mean()
a = a.to(torch.long)