使用CoreMLTools 3 beta创建KNearestNeighborsClassifier时出错,并询问如何正确设置尺寸

2024-05-14 06:58:59 发布

您现在位置:Python中文网/ 问答频道 /正文

对于一个项目,我想创建一个核心的ml3模型,它接收一些文本(即来自邮件)并对其进行分类。此外,模型应该是可更新的,并在设备上进行训练。因此,我发现KNearestNeighborsClassifier是可更新的,并希望在我的方法中使用它们。 然而,首先我犯了一个错误

" RuntimeWarning: You will not be able to run predict() on this Core ML model. Underlying exception message was: Error compiling model: "Error reading protobuf spec. validator error: KNearestNeighborsClassifier requires k to be a positive integer."

在用脚本创建这样一个模型时(见下文)。此外,我不知道如何使用KNearestNeighborsClassifier为我的问题正确。特别是,如果我想对一些文本进行分类,哪个维度是正确的?我该如何在应用程序中正确使用模型?也许你知道一些有用的指南,我还没有找到

我创建KNearestNeighborsClassifier的脚本基于以下指南:https://github.com/apple/coremltools/blob/master/examples/updatable_models/updatable_nearest_neighbor_classifier.ipynb 我已经安装并且正在使用coremltools==3.0b6。你知道吗

下面是我创建模型的实际脚本:

number_of_dimensions = 128

from coremltools.models.nearest_neighbors import KNearestNeighborsClassifierBuilder

builder = KNearestNeighborsClassifierBuilder(input_name='input',
                                             output_name='output',
                                             number_of_dimensions=number_of_dimensions,
                                             default_class_label='defaultLabel',
                                             number_of_neighbors=3,
                                             weighting_scheme='inverse_distance',
                                             index_type='linear')

builder.author = 'Christian'
builder.license = 'MIT'
builder.description = 'Classifies {} dimension vector based on 3 nearest neighbors'.format(number_of_dimensions)

builder.spec.description.input[0].shortDescription = 'Input vector to classify'
builder.spec.description.output[0].shortDescription = 'Predicted label. Defaults to \'defaultLabel\''
builder.spec.description.output[1].shortDescription = 'Probabilities / score for each possible label.'

builder.spec.description.trainingInput[0].shortDescription = 'Example input vector'
builder.spec.description.trainingInput[1].shortDescription = 'Associated true label of each example vector'


#This lets the developer of the app change the number of neighbors at runtime from anywhere between 1 and 10, with a default of 3.
builder.set_number_of_neighbors_with_bounds(3, allowed_range=(1, 10))


# Let's set the index to kd_tree with leaf size of 30
builder.set_index_type('kd_tree', 30)


# By default an empty knn model is updatable
print(builder.is_updatable)

print(builder.number_of_dimensions)
print(builder.number_of_neighbors)
print(builder.number_of_neighbors_allowed_range())
print(builder.index_type)


mlmodel_updatable_path = './UpdatableKNN.mlmodel'

# Save the updated spec
from coremltools.models import MLModel
mlmodel_updatable = MLModel(builder.spec)
mlmodel_updatable.save(mlmodel_updatable_path)

我希望你能帮助我,告诉我,如果我使用KNearestNeighborsClassifier进行文本分类的方法是有意义的,希望你能帮助我成功地创建CoreML模型。你知道吗

非常感谢。你知道吗


Tags: oftheto模型numberbuilderneighborsdescription
1条回答
网友
1楼 · 发布于 2024-05-14 06:58:59

不知道为什么会出现这样的错误,不过请确保您使用的是最新(beta)版本的coremltools(目前为3.0b6)。你知道吗

至于维数,你需要把文本转换成一个固定长度的向量。具体怎么做完全取决于你要解决的问题。你知道吗

例如,您可以使用单词包技术将短语转换为这样的向量。你可以使用文字嵌入,或神经网络,或任何其他常见的技术。你知道吗

但是你需要一些方法把文本转换成特征向量。你知道吗

相关问题 更多 >

    热门问题