I want to calculate mean frame with taking into account zero padding and replace some random frames with it.
This code raises an error.
What is the right way?
def mean_frame_dropout(x, lens, p=0.2):
    x = x.clone()
    batch_size, length, features = x.size()
    mask = torch.distributions.Bernoulli(
        probs=(1 - p)).sample((batch_size, length)).to(x.device)
    mask = ~mask.bool()
    mask = mask.unsqueeze(-1).expand(-1, -1, features)
    mean = x.detach().sum(1) / lens
    x[mask] = mean
    return x