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

如何解决pomegranate中ConditionalCategorical的TypeError问题

解决pomegranate中ConditionalCategorical实例化的TypeError错误

问题代码

from pomegranate.distributions import ConditionalCategorical
import numpy as np

prob_table = [
    [1.0, 0.0],  # parent = 0 -> child = 0
    [0.0, 1.0],  # parent = 1 -> child = 1
]
probs_array = np.array(prob_table, dtype=np.float32)
n_categories = [2, 2]  # 格式错误

cc = ConditionalCategorical(probs_array, n_categories=n_categories)
print("Created ConditionalCategorical:", cc)

报错信息

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[297], line 11
      8 probs_array = np.array(prob_table, dtype=np.float32)  # ✅ Use NumPy
      9 n_categories = [2, 2]  # One binary parent, one binary child
---> 11 cc = ConditionalCategorical(probs_array, n_categories=n_categories)
     12 print("Created ConditionalCategorical:", cc)

File ~/anaconda3/lib/python3.10/site-packages/pomegranate/distributions/conditional_categorical.py:107, in ConditionalCategorical.__init__(self, probs, n_categories, pseudocount, inertia, frozen, check_data)
    105 self.d = len(self.probs) if self._initialized else None
    106 self.n_parents = len(self.probs[0].shape) if self._initialized else None
--> 107 self._reset_cache()

File ~/anaconda3/lib/python3.10/site-packages/pomegranate/distributions/conditional_categorical.py:157, in ConditionalCategorical._reset_cache(self)
    154 _xw_sum = []
    156 for n_categories in self.n_categories:
--> 157     _w_sum.append(torch.zeros(*n_categories[:-1], 
    158         dtype=self.probs[0].dtype, device=self.device))
    159     _xw_sum.append(torch.zeros(*n_categories, 
    160         dtype=self.probs[0].dtype, device=self.device))
    162 self._w_sum = BufferList(_w_sum)

TypeError: zeros()接收到无效的参数组合 - 得到(device=torch.device, dtype=torch.dtype, ),但预期为以下其中一种:
 * (tuple of ints size, *, tuple of names names, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)
 * (tuple of ints size, *, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)

错误原因

ConditionalCategorical的n_categories参数格式要求错误。该参数需要是列表,列表内每个元素为元组:元组最后一位是子变量的类别数,前面的元素对应父变量的类别数。

你传入的[2,2]是两个独立整数,代码循环时会单独取出每个整数,执行n_categories[:-1](对整数做切片操作),导致torch.zeros()未收到必要的size参数,触发参数不匹配错误。

解决方案

将n_categories修改为包含元组的列表,对应单父单子变量的场景,写成[(2, 2)]即可:

from pomegranate.distributions import ConditionalCategorical
import numpy as np

prob_table = [
    [1.0, 0.0],  # parent = 0 -> child = 0
    [0.0, 1.0],  # parent = 1 -> child = 1
]
probs_array = np.array(prob_table, dtype=np.float32)
n_categories = [(2, 2)]  # 修正格式:元组表示(父变量类别数, 子变量类别数),放入列表中

cc = ConditionalCategorical(probs_array, n_categories=n_categories)
print("Created ConditionalCategorical:", cc)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 15:17:33