← 返回首页
Pytorch与分布函数
发表时间:2024-01-23 01:46:43
Pytorch与分布函数

Pytorch与分布函数。

1.Pytorch的分布函数

torch.distributions包含可参数化的概率分布和采样函数。 这允许构建用于优化的随机计算图和随机梯度估计器。旨在为深度学习和概率建模提供强大的支持。可以使用该库生成多种概率分布(例如正态分布、均匀分布、泊松分布等),并使用相关函数进行采样、求概率密度函数、计算累积分布函数等操作。

实例:

import torch
from torch.distributions import  Normal
mean=torch.Tensor([0,2])
normal=Normal(mean,1)

#sample()就是直接在定义的正太分布(均值为mean,标准差std是1)上采样
result = normal.sample()
print("sample():",result)
#rsample()不是在定义的正太分布上采样,而是先对标准正太分布 N(0,1) 进行采样
result = normal.rsample()
print("rsample():",result)

运行结果:

sample(): tensor([-0.8145,  3.5831])
rsample(): tensor([1.0118, 0.3041])