Problem with TensorVariable type and special functions in pymc

huangapple go评论67阅读模式
英文:

Problem with TensorVariable type and special functions in pymc

问题

I'm using a pymc model that contains the special function gamma. Since scipy special functions doesn't work with pytensor as variable, I'm trying to escape the problem with an @as_op:

@as_op(itypes=theano.tensor.dscalar, otypes=theano.tensor.dscalar)
def gamma_theano(x):
    return scipy.special.gamma(x)

This very naive solution was used to work perfectly with previous projects (and previous package versions), while now I'm getting the error:

TypeError: We expected inputs of types '[TensorType(float64, scalar)]' but got types '[TensorType(float64, ())]'

Suggestions are very well received. Thanks.

英文:

I'm using a pymc model that contains the special function gamma. Since scipy special functions doesn't work with pytensor as variable, I'm trying to escape the problem with an @as_op:

@as_op(itypes=theano.tensor.dscalar, otypes=theano.tensor.dscalar)
def gamma_theano(x):
    return scipy.special.gamma(x)

This very naive solution was used to work perfectly with previous projects (and previous package versions), while now I'm getting the error:

TypeError: We expected inputs of types '[TensorType(float64, scalar)]' but got types '[TensorType(float64, ())]'

Suggestions are very well received. Thanks.

答案1

得分: 0

Using pytensor而不是theano解决了错误:

import pytensor

@as_op(itypes=pytensor.tensor.dscalar, otypes=pytensor.tensor.dscalar)
def gamma_theano(x):
    return scipy.special.gamma(x)

然而,在我的情况下,这没有解决问题,因为它导致了分段错误。

我使用了pytensor的内置gamma函数找到了解决方案:

import pytensor

pytensor.tensor.math.gamma(x)
英文:

Using pytensor instead of theano resolves the error:

import pytensor

@as_op(itypes=pytensor.tensor.dscalar, otypes=pytensor.tensor.dscalar)
def gamma_theano(x):
    return scipy.special.gamma(x)

However, in my case, this didn't solved the issue because it led to a segmentation error.

I've found the solution with the built-in gamma function of pytensor:

import pytensor

pytensor.tensor.math.gamma(x)

huangapple
  • 本文由 发表于 2023年4月1日 00:03:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900596.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定