我想从 Matlab 调用我的 python 模块。我收到错误消息
使用 numpy_ops>init Thinc.backends.numpy_ops 时出错
Python 错误:ValueError:numpy.dtype 大小已更改,可能表示二进制不兼容。预计 C 头文件为 96,从 PyObject 得到 88。
Python脚本如下
import spacy
def text_recognizer(model_path, text):
try:
# Load the trained model
nlp = spacy.load(model_path)
print("Model loaded successfully.")
# Process the given text
doc = nlp(text)
ent_labels = [(ent.text, ent.label_) for ent in doc.ents]
return ent_labels
Matlab脚本如下
% Set up the Python environment
pe = pyenv;
py.importlib.import_module('final_output');
% Add the directory containing the Python script to the Python path
path_add = fileparts(which('final_output.py'));
if count(py.sys.path, path_add) == 0
insert(py.sys.path, int64(0), path_add);
end
% Define model path and text to process
model_path = 'D:\trained_model\\output\\model-best';
text = 'Roses are red';
% Call the Python function
pyOut = py.final_output.text_recognizer(model_path, text);
% Convert the output to a MATLAB cell array
entity_labels = cell(pyOut);
disp(entity_labels);
我找到了一种更新 numpy 的解决方案,我做了什么,但没有任何改变。我正在使用 python 3.9 和 numpy 版本 2.0.0
当我尝试使用Matlab脚本调用Python模块时收到错误 我该如何解决这个问题?
谢谢你。
将你的 numpy 版本降级到 1.26.4