我在使用pystruct
Python模块进行讨论线程中帖子分类的结构化学习问题时,遇到了一个问题。当尝试训练OneSlackSSVM
以与LinearChainCRF
一起使用时出现了问题。我按照文档中的OCR示例进行操作,但似乎无法在SSVM上调用.fit()
方法。以下是我遇到的错误:
Traceback (most recent call last):File "<ipython-input-47-da804d135818>", line 1, in <module>ssvm.fit(X_train, y_train)File "/Users/kylefth/anaconda/lib/python2.7/site- packages/pystruct/learners/one_slack_ssvm.py", line 429, in fitjoint_feature_gt = self.model.batch_joint_feature(X, Y)File "/Users/kylefth/anaconda/lib/python2.7/site- packages/pystruct/models/base.py", line 40, in batch_joint_feature joint_feature_ += self.joint_feature(x, y)File "/Users/kylefth/anaconda/lib/python2.7/site- packages/pystruct/models/graph_crf.py", line 197, in joint_featureunary_marginals[gx, y] = 1IndexError: index 7 is out of bounds for axis 1 with size 7
以下是我编写的代码。我尝试按照文档示例中的数据结构进行数据组织,其中整体数据结构是一个dict
,包含data
、labels
和folds
的键。
from pystruct.models import LinearChainCRFfrom pystruct.learners import OneSlackSSVM# 打印整体数据结构的键print threads.keys()>>> ['folds', 'labels', 'data']# 创建模型实例crf = LinearChainCRF()ssvm = OneSlackSSVM(model=crf)# 按照示例将数据分成训练和测试集X, y, folds = threads['data'], threads['labels'], threads['folds']X_train, X_test = X[folds == 1], X[folds != 1]y_train, y_test = y[folds == 1], y[folds != 1]# 打印数据和标签中第一个元素的维度print X[0].shape, y[0].shape>>> (8, 211), (8,)# 拟合ssvm模型ssvm.fit(X_train, y_train)>>> 见上面的错误
在尝试拟合模型后直接出现了上述错误。X_train
、X_test
、y_train
和y_test
的所有实例都有211列,并且所有标签维度似乎与其对应的训练和测试数据相匹配。任何帮助将不胜感激。
回答:
我认为你做的一切都是正确的,这是https://github.com/pystruct/pystruct/issues/114。你的标签y需要从0到n_labels开始。我认为你的标签是从1开始的。