现在位置: 首页 > PyTorch 教程 > 正文

PyTorch torch.poisson 函数


Pytorch torch 参考手册 Pytorch torch 参考手册

torch.poisson 是 PyTorch 中用于从泊松分布生成随机数的函数。

函数定义

torch.poisson(lam, generator=None, out=None)

参数说明

  • lam - 泊松分布的参数 lambda(期望/均值),必须为非负数
  • generator - 随机数生成器(可选)
  • out - 输出张量(可选)

使用示例

实例

import torch

# 使用单个 lambda 值生成随机张量
result1 = torch.poisson(lam=5.0, size=(3, 3))
print("lambda=5 的 3x3 泊松分布随机张量:")
print(result1)

# 使用张量作为 lambda 参数
lam_tensor = torch.tensor([1.0, 2.0, 5.0, 10.0])
result2 = torch.poisson(lam_tensor)
print("n使用张量 lambda 的采样结果:")
print(result2)

# 模拟计数数据
lam = torch.tensor([0.5, 1.0, 2.0, 5.0, 10.0])
samples = torch.poisson(lam)
print("n不同 lambda 值的泊松采样:")
for i, (l, s) in enumerate(zip(lam.tolist(), samples.tolist())):
    print(f"  lambda={l}: {s}")

Pytorch torch 参考手册 Pytorch torch 参考手册