简体   繁体   English

如何使用 pytorch 获得 jacobian 以获得多元正态分布的对数概率

[英]how to get jacobian with pytorch for log probability of multivariate normal distribution

I've drawn samples from a multivariate normal distribution and would like to get the gradient of their log probability with respect to the mean.我已经从多元正态分布中抽取样本,并希望获得它们的对数概率相对于均值的梯度。 Since there are many samples, this requires a Jacobian:由于有很多样本,这需要一个雅可比行列式:

import torch

mu = torch.ones((2,), requires_grad=True)
sigma = torch.eye(2)
dist = torch.distributions.multivariate_normal.MultivariateNormal(mu, sigma)

num_samples=10
samples = dist.sample((num_samples,))
logprobs = dist.log_prob(samples)

Now I would like to get the derivative of each entry in logprobs with respect to each entry in mu .现在我logprobs中每个条目相对于mu中每个条目的导数。

A simple solution is a python loop:一个简单的解决方案是 python 循环:

grads = []
for logprob in logprobs:
    grad = torch.autograd.grad(logprob, mu, retain_graph=True)
    grads.append(grad)

If you stack the grads, the result is the desired Jacobian.如果你堆叠毕业生,结果是所需的雅可比行列式。 Is there also built-in and vectorized support for this?是否还有对此的内置和矢量化支持?

Related questions/internet ressources:相关问题/互联网资源:

This is a huge topic, there are lots of related posts.这是一个很大的话题,有很多相关的帖子。 Nevertheless, I think that this specific question (regarding distributions) hasn't been answered yet:尽管如此,我认为这个具体问题(关于分布)尚未得到回答:

  • This question is basically the same as mine (but without example code and solution attempt), sadly it is unanswered: Pytorch custom function jacobian gradient这个问题与我的基本相同(但没有示例代码和解决方案尝试),遗憾的是没有答案: Pytorch custom function jacobian gradient

  • This question shows the calculation of a jacobian in pytorch, but I don't think the solution is applicable to my problem: Pytorch most efficient Jacobian/Hessian calculation It requires stacking the input in a way that seems incompatible with the distribution.这个问题显示了 pytorch 中雅可比的计算,但我认为该解决方案不适用于我的问题: Pytorch 最有效的雅可比/黑森计算它需要以一种似乎与分布不兼容的方式堆叠输入。 I couldn't make it work.我无法让它工作。

  • This gist has some code snippets for Jacobians.这个要点有一些雅可比的代码片段。 In principle they are similar to the approach from the question above.原则上,它们类似于上述问题的方法。

PyTorch 1.5.1 introduced a torch.autograd.functional.jacobian function. PyTorch 1.5.1 引入了torch.autograd.functional.jacobian function。 This computes the Jacobians of a function w.r.t.这将计算 function w.r.t 的雅可比行列式。 the input tensors.输入张量。 Since jacobian requires a python function as the first argument, using it requires some code restructuring.由于jacobian需要 python function 作为第一个参数,因此使用它需要一些代码重组。

import torch

torch.manual_seed(0)    # for repeatable results

mu = torch.ones((2,), requires_grad=True)
sigma = torch.eye(2)
num_samples = 10

def f(mu):
    dist = torch.distributions.multivariate_normal.MultivariateNormal(mu, sigma)
    samples = dist.sample((num_samples,))
    logprobs = dist.log_prob(samples)
    return logprobs

grads = torch.autograd.functional.jacobian(f, mu)

print(grads)
tensor([[-1.1258, -1.1524],
        [-0.2506, -0.4339],
        [ 0.5988, -1.5551],
        [-0.3414,  1.8530],
        [ 0.4681, -0.1577],
        [ 1.4437,  0.2660],
        [ 1.3894,  1.5863],
        [ 0.9463, -0.8437],
        [ 0.9318,  1.2590],
        [ 2.0050,  0.0537]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在多元正态分布中找出概率 - finding probability in multivariate normal distribution 如何在 Python 中采样多元对数正态分布? - How can I sample a multivariate log-normal distribution in Python? 尝试针对多元正态分布的3d绘图概率密度函数 - Try 3d plotting probability density function for multivariate normal distribution 如何从 python 中的对数正态分布概率密度 function 图中计算概率? - How can I calculate probability from the log-normal distribution probability density function graph in python? 如何在pytorch中创建正态分布 - How to create a normal distribution in pytorch 如何在 PyTorch 中获得导数的完整雅可比行列式? - How to get the full Jacobian of a derivative in PyTorch? 如何从标准正态值生成多元正态分布? - How to generate multivariate Normal distribution from a standard normal value? 计算多元正态的概率 - Compute probability over a multivariate normal 如何计算 Scipy 中正态分布的概率 - How to calculate a probability for a normal distribution in Scipy 当我仅在 python 中提供百分比时,如何从多元正态分布中获取上限值和下限值? - How to get the Upper and Lower Values from a Multivariate Normal Distribution when I only supply a percentage in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM