在 PyStruct 中拟合 SSVM 模型时出现 IndexError
IndexError when fitting SSVM model in PyStruct
我正在使用 pystruct
Python 模块来解决结构化学习问题,以对讨论线程中的帖子进行分类,并且我 运行 在绑定训练时遇到了问题OneSlackSSVM
用于 LinearChainCRF
。我正在关注 OCR example from the docs,但似乎无法在 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 fit
joint_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_feature
unary_marginals[gx, y] = 1
IndexError: index 7 is out of bounds for axis 1 with size 7
下面是我写的代码。我已经厌倦了在文档示例中构造数据,其中整体数据结构是 dict
,键为 data
、labels
和 folds
.
from pystruct.models import LinearChainCRF
from pystruct.learners import OneSlackSSVM
# Printing out keys of overall data structure
print threads.keys()
>>> ['folds', 'labels', 'data']
# Creating instances of models
crf = LinearChainCRF()
ssvm = OneSlackSSVM(model=crf)
# Splitting up data into training and test sets as in example
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 out dimensions of first element in data and labels
print X[0].shape, y[0].shape
>>> (8, 211), (8,)
# Fitting the ssvm model
ssvm.fit(X_train, y_train)
>>> see error above
直接尝试拟合模型后,出现上述错误。 X_train
、X_test
、y_train
和 y_test
的所有实例都有 211 列,所有标签维度似乎都与其相应的训练和测试数据相匹配。任何帮助将不胜感激。
我觉得你做的一切都是对的,这是https://github.com/pystruct/pystruct/issues/114。
您的标签 y 需要从 0 开始到 n_labels。我想你的从 1 开始。
我正在使用 pystruct
Python 模块来解决结构化学习问题,以对讨论线程中的帖子进行分类,并且我 运行 在绑定训练时遇到了问题OneSlackSSVM
用于 LinearChainCRF
。我正在关注 OCR example from the docs,但似乎无法在 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 fit
joint_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_feature
unary_marginals[gx, y] = 1
IndexError: index 7 is out of bounds for axis 1 with size 7
下面是我写的代码。我已经厌倦了在文档示例中构造数据,其中整体数据结构是 dict
,键为 data
、labels
和 folds
.
from pystruct.models import LinearChainCRF
from pystruct.learners import OneSlackSSVM
# Printing out keys of overall data structure
print threads.keys()
>>> ['folds', 'labels', 'data']
# Creating instances of models
crf = LinearChainCRF()
ssvm = OneSlackSSVM(model=crf)
# Splitting up data into training and test sets as in example
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 out dimensions of first element in data and labels
print X[0].shape, y[0].shape
>>> (8, 211), (8,)
# Fitting the ssvm model
ssvm.fit(X_train, y_train)
>>> see error above
直接尝试拟合模型后,出现上述错误。 X_train
、X_test
、y_train
和 y_test
的所有实例都有 211 列,所有标签维度似乎都与其相应的训练和测试数据相匹配。任何帮助将不胜感激。
我觉得你做的一切都是对的,这是https://github.com/pystruct/pystruct/issues/114。 您的标签 y 需要从 0 开始到 n_labels。我想你的从 1 开始。