A penalty on the number of non-zero values tries to enforce sparsity. This is usually achieved by computing the L1 loss. So to add this loss you can compute torch.nn.L1Loss(encoded_x, torch.zeros_like(encoded_x)) and add it to your total loss function.