Thanks to visit codestin.com
Credit goes to github.com

Skip to content

torch.distributions.Beta.entropy returns negative values #152845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
MaHaArt opened this issue May 5, 2025 · 1 comment
Closed

torch.distributions.Beta.entropy returns negative values #152845

MaHaArt opened this issue May 5, 2025 · 1 comment
Labels
module: distributions Related to torch.distributions triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@MaHaArt
Copy link

MaHaArt commented May 5, 2025

๐Ÿ› Describe the bug

Beta distribution returns negative values for perfectly legal alpha / beta parameters. It seems that the sign is flipped. The following code shows the results of the pytorch built-in function vs. a custom implementation.

import torch
from torch.special import digamma

def stable_beta_entropy(alpha, beta):
    ab = alpha + beta
    entropy = (
        - torch.lgamma(alpha) - torch.lgamma(beta)
        + torch.lgamma(ab)
        + (alpha - 1) * digamma(alpha)
        + (beta - 1) * digamma(beta)
        - (ab - 2) * digamma(ab)
    )
    return entropy.clamp(min=0.0)

if __name__ == '__main__':

    alpha = torch.tensor([1.1, 1.2, 1.3])
    beta = torch.tensor([1.3, 1.2, 1.1])

    # Pytorch --> *NEGATIVE* Entropy, does mathemaically not make sense
    dist = torch.distributions.Beta(alpha, beta)
    print(f"Pytorch Beta Entropy: {dist.entropy()}")  # should all be > 0

    # Custom Implementation --> *POSITIVE* Entropy
    print(f"Custom Beta Entropy: {stable_beta_entropy(alpha,beta)}")``

Output of above script:

Pytorch Beta Entropy: tensor([-0.0206, -0.0108, -0.0206])
Custom Beta Entropy: tensor([0.0206, 0.0108, 0.0206])

Versions

PyTorch version: 2.7.0+cu128
Is debug build: False
CUDA used to build PyTorch: 12.8
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.5 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: version 3.22.1
Libc version: glibc-2.35

Python version: 3.11.9 (main, Apr 6 2024, 17:59:24) [GCC 9.4.0] (64-bit runtime)
Python platform: Linux-6.8.0-57-generic-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: 12.4.99

cc @fritzo @neerajprad @alicanb @nikitaved

@zou3519 zou3519 added module: distributions Related to torch.distributions triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels May 6, 2025
@fritzo
Copy link
Collaborator

fritzo commented May 7, 2025

Negative entropy is perfectly fine for continuous distributions. Perhaps you're illegally importing intuition from discrete distributions where entropy is always nonnegative?

@fritzo fritzo closed this as completed May 7, 2025
@fritzo fritzo reopened this May 7, 2025
@fritzo fritzo closed this as completed May 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: distributions Related to torch.distributions triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

No branches or pull requests

3 participants