如何解决Onnx运行时错误“ SVC转换器不支持与前n个整数不同的类”是什么意思?
我正在尝试将sklearn SVC模型转换/存储为.onnx文件,并且遇到了我不理解的运行时错误。通过sklearn随机森林分类器和sklearn k-NN分类器,我已经能够有效地使用相同的代码而不会出现错误。我收到一个我不理解的奇怪的onnx运行时错误。感谢您提供有关此错误的任何帮助。
我首先在下面发布了运行文件svm_time.py的输出,然后在下面显示了svm_time.py文件中包含的代码。
谢谢。
df %>%
mutate(x2 = case_when(a == 3 ~ 0.05 + x))
python3 svm_time.py
'train_model' 4809.58 ms
train score is: 0.8765468473777254
val Accuracy is: 0.7037037037037037
Traceback (most recent call last):
File "svm_time.py",line 97,in <module>
main()
File "svm_time.py",line 91,in main
onx = convert_sklearn(clf,initial_types=initial_type)
File "/home/matt/anaconda3/envs/venv/lib/python3.7/site-packages/skl2onnx/convert.py",line 154,in convert_sklearn
dtype=dtype,options=options)
File "/home/matt/anaconda3/envs/venv/lib/python3.7/site-packages/skl2onnx/common/_topology.py",line 1054,in convert_topology
conv(scope,operator,container)
File "/home/matt/anaconda3/envs/venv/lib/python3.7/site-packages/skl2onnx/common/_registration.py",line 29,in __call__
return self._fct(*args)
File "/home/matt/anaconda3/envs/venv/lib/python3.7/site-packages/skl2onnx/operator_converters/support_vector_machines.py",line 221,in convert_sklearn_svm_classifier
"Classes different from first n integers are not supported "
RuntimeError: Classes different from first n integers are not supported in SVC converter.
解决方法
在我看来,这可能是Onnx和sklearn的兼容性问题。
1)。 https://github.com/onnx/sklearn-onnx/issues/302 2)。 https://github.com/onnx/sklearn-onnx/blob/master/skl2onnx/operator_converters/support_vector_machines.py#L17
基于这两个来源,我更改了代码,以包括OVO决策函数形状而不是OVR,现在,至少在运行svm_time.py文件时,将保存一个.onnx文件。
import numpy as np
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
import datetime
pd.set_option('display.max_rows',None)
# data sample
nperiods = 200
np.random.seed(123)
df = pd.DataFrame(np.random.randint(-10,12,size=(nperiods,4)),columns=list('ABCD'))
datelist = pd.date_range(datetime.datetime(2020,1,1).strftime('%Y-%m-%d'),periods=nperiods).tolist()
df['dates'] = datelist
df = df.set_index(['dates'])
df.index = pd.to_datetime(df.index)
df.iloc[0] = 0
df = df.cumsum().reset_index()
# function to set background color for a
# specified variable and a specified level
# plotly setup
fig = px.line(df,x='dates',y=df.columns[1:])
fig.update_xaxes(showgrid=True,gridwidth=1,gridcolor='rgba(0,255,0.1)')
fig.update_yaxes(showgrid=True,0.1)')
def bgLevels(fig,variable,level,mode,fillcolor,layer):
"""
Set a specified color as background for given
levels of a specified variable using a shape.
Keyword arguments:
==================
fig -- plotly figure
variable -- column name in a pandas dataframe
level -- int or float
mode -- set threshold above or below
fillcolor -- any color type that plotly can handle
layer -- position of shape in plotly fiugre,like "below"
"""
if mode == 'above':
m = df[variable].gt(level)
if mode == 'below':
m = df[variable].lt(level)
df1 = df[m].groupby((~m).cumsum())['dates'].agg(['first','last'])
for index,row in df1.iterrows():
#print(row['first'],row['last'])
fig.add_shape(type="rect",xref="x",yref="paper",x0=row['first'],y0=0,x1=row['last'],y1=1,line=dict(color="rgba(0,0)",width=3,),fillcolor=fillcolor,layer=layer)
return(fig)
fig = bgLevels(fig = fig,variable = 'A',level = 100,mode = 'above',fillcolor = 'rgba(100,100,0.2)',layer = 'below')
fig = bgLevels(fig = fig,variable = 'D',level = -60,mode = 'below',fillcolor = 'rgba(255,layer = 'below')
fig.show()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。