Solved by Sunshine352 in post #2 
              
              
                a=torch.tensor([[[0,1,2]], [[5,7,0]]]).float() 
mask=(a>0).float() 
mask_mean = a.sum(-1) / mask.sum(-1) 
output: 
mask_mean=tensor([[1.5000], [6.0000]]) 
mask_mean.shape=torch.Size([2, 1])