You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyMC中TensorVariable类型与gamma特殊函数使用问题求助

问题描述

在使用包含gamma特殊函数的PyMC模型时,发现scipy特殊函数无法与PyTensor变量兼容,于是尝试通过@as_op装饰器规避该问题,代码如下:

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

该方案在旧版本项目中可正常运行,但现在出现如下错误:

TypeError: We expected inputs of types '[TensorType(float64, scalar)]' but got types '[TensorType(float64, ())]'
解决建议
  • 修正张量类型定义:报错源于PyTensor(原Theano)的张量类型命名规范变更,旧版dscalar对应新版的TensorType(float64, ()),需调整itypes和otypes的写法,同时改用PyTensor模块(新版PyMC已不再依赖Theano):

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

    注意要把类型放在列表中,这是新版@as_op要求的格式。

  • 使用PyTensor内置gamma函数:更简便的方式是直接用PyTensor原生实现的gamma函数,它天然支持PyTensor变量,无需自定义op:

    import pytensor.tensor as pt
    
    # 直接替代自定义函数
    pt.gamma(your_pytensor_variable)
    

    这种方法兼容性更强,也避免了自定义op带来的类型匹配问题。

  • 同步版本依赖:确认PyMC与PyTensor版本适配,新版PyMC完全基于PyTensor开发,需将代码中所有theano相关导入替换为pytensor,避免混合使用旧版模块导致的类型冲突。

内容的提问来源于stack exchange,提问作者Wil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 13:43:12