Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_gpr_fitted_shapes(self):
data = load_iris()
X = data.data.astype(np.float32)
y = data.target.astype(np.float32)
X_train, X_test, y_train, y_test = train_test_split(X, y)
gp = GaussianProcessRegressor()
gp.fit(X_train, y_train)
model_onnx = to_onnx(
gp, initial_types=[('X', FloatTensorType([None, None]))])
self.assertTrue(model_onnx is not None)
self.check_outputs(gp, model_onnx, X_test, {}, skip_if_float32=True)
def test_gpr_fitted_partial_float64(self):
data = load_iris()
X = data.data
y = data.target
X_train, X_test, y_train, y_test = train_test_split(X, y)
gp = GaussianProcessRegressor(kernel=DotProduct(), alpha=10.)
gp.fit(X_train, y_train)
model_onnx = to_onnx(
gp, initial_types=[('X', FloatTensorType([None, None]))])
self.assertTrue(model_onnx is not None)
try:
self.check_outputs(gp, model_onnx, X_test.astype(np.float32), {})
except AssertionError as e:
assert "Max relative difference:" in str(e)
model_onnx = to_onnx(
gp, initial_types=[('X', DoubleTensorType([None, None]))],
dtype=np.float64)
self.assertTrue(model_onnx is not None)
self.check_outputs(gp, model_onnx, X_test, {})
data = load_iris()
X = data.data
y = data.target
X_train, X_test, y_train, y_test = train_test_split(X, y)
gp = GaussianProcessRegressor(kernel=RationalQuadratic(), alpha=100.)
gp.fit(X_train, y_train)
try:
to_onnx(
gp, initial_types=[('X', FloatTensorType([None, None]))],
options={GaussianProcessRegressor: {'optim': 'CDIST'}})
raise AssertionError("CDIST is not implemented")
except ValueError:
pass
model_onnx = to_onnx(
gp, initial_types=[('X', FloatTensorType([None, None]))],
options={GaussianProcessRegressor: {'optim': 'cdist'}})
self.assertTrue(model_onnx is not None)
name_save = inspect.currentframe().f_code.co_name + '.onnx'
with open(name_save, 'wb') as f:
f.write(model_onnx.SerializeToString())
try:
self.check_outputs(gp, model_onnx, X_test.astype(np.float32), {})
except RuntimeError as e:
if "CDist is not a registered" in str(e):
return
except AssertionError as e:
assert "Max relative difference:" in str(e)
model_onnx = to_onnx(
gp, initial_types=[('X', DoubleTensorType([None, None]))],
data = load_iris()
X = data.data
y = data.target
X_train, X_test, y_train, y_test = train_test_split(X, y)
gp = GaussianProcessRegressor(kernel=DotProduct(), alpha=10.)
gp.fit(X_train, y_train)
model_onnx = to_onnx(
gp, initial_types=[('X', FloatTensorType([None, None]))])
self.assertTrue(model_onnx is not None)
try:
self.check_outputs(gp, model_onnx, X_test.astype(np.float32), {})
except AssertionError as e:
assert "Max relative difference:" in str(e)
model_onnx = to_onnx(
gp, initial_types=[('X', DoubleTensorType([None, None]))],
dtype=np.float64)
self.assertTrue(model_onnx is not None)
self.check_outputs(gp, model_onnx, X_test, {})
def test_way2_to_onnx(self):
X = np.arange(20).reshape(10, 2)
tr = KMeans(n_clusters=2)
tr.fit(X)
onx = to_onnx(tr, X.astype(np.float32))
dump_data_and_model(
X.astype(np.float32), tr, onx,
basename="MixinWay2ToOnnx")
def test_gpr_rbf_fitted(self):
gp = GaussianProcessRegressor(alpha=1e-7,
n_restarts_optimizer=15,
normalize_y=True)
gp.fit(Xtrain_, Ytrain_)
# return_cov=False, return_std=False
model_onnx = to_onnx(
gp, initial_types=[('X', FloatTensorType([None, None]))],
dtype=np.float32)
self.assertTrue(model_onnx is not None)
dump_data_and_model(Xtest_.astype(np.float32), gp, model_onnx,
verbose=False,
basename="SklearnGaussianProcessRBF")
model_onnx = to_onnx(
gp, initial_types=[('X', FloatTensorType([None, None]))],
options={GaussianProcessRegressor: {'optim': 'cdist'}})
self.assertTrue(model_onnx is not None)
name_save = inspect.currentframe().f_code.co_name + '.onnx'
with open(name_save, 'wb') as f:
f.write(model_onnx.SerializeToString())
try:
self.check_outputs(gp, model_onnx, X_test.astype(np.float32), {})
except RuntimeError as e:
if "CDist is not a registered" in str(e):
return
except AssertionError as e:
assert "Max relative difference:" in str(e)
model_onnx = to_onnx(
gp, initial_types=[('X', DoubleTensorType([None, None]))],
dtype=np.float64)
self.assertTrue(model_onnx is not None)
self.check_outputs(gp, model_onnx, X_test, {})
# return_std=True
options = {GaussianProcessRegressor: {"return_std": True}}
model_onnx = to_onnx(
gp, options=options,
initial_types=[('X', FloatTensorType([None, None]))],
dtype=np.float32)
self.assertTrue(model_onnx is not None)
self.check_outputs(gp, model_onnx, Xtest_.astype(np.float32),
predict_attributes=options[
GaussianProcessRegressor])
# return_cov=True
options = {GaussianProcessRegressor: {"return_cov": True}}
# model_onnx = to_onnx(gp, Xtrain_.astype(np.float32), options=options)
model_onnx = to_onnx(
gp, options=options,
initial_types=[('X', FloatTensorType([None, None]))],
dtype=np.float32)
self.assertTrue(model_onnx is not None)
self.check_outputs(gp, model_onnx, Xtest_.astype(np.float32),
predict_attributes=options[
GaussianProcessRegressor])
import onnx
import sklearn
import onnxruntime as rt
import numpy as np
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
try:
from onnxruntime.capi.onnxruntime_pybind11_state import InvalidArgument
except ImportError:
# onnxruntime <= 0.5
InvalidArgument = RuntimeError
data = load_iris()
clr = LogisticRegression().fit(data.data[:, :2], data.target)
with open("logreg_iris.onnx", "wb") as f:
f.write(skl2onnx.to_onnx(
clr, data.data[:, :2].astype(np.float32)).SerializeToString())
example2 = "logreg_iris.onnx"
sess = rt.InferenceSession(example2)
input_name = sess.get_inputs()[0].name
output_name = sess.get_outputs()[0].name
#############################
# The first example fails due to *bad types*.
# *onnxruntime* only expects single floats (4 bytes)
# and cannot handle any other kind of floats.
try:
x = np.array([[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0]],
dtype=np.float64)
X = np.arange(20).reshape(10, 2)
tr = make_pipeline(CustomOpTransformer(), KMeans(n_clusters=2))
tr.fit(X)
onx = convert_sklearn(
tr, initial_types=[('X', FloatTensorType((None, X.shape[1])))])
print(predict_with_onnxruntime(onx, X))
#############################
# Way 2
X = np.arange(20).reshape(10, 2)
tr = make_pipeline(CustomOpTransformer(), KMeans(n_clusters=2))
tr.fit(X)
onx = to_onnx(tr, X.astype(np.float32))
print(predict_with_onnxruntime(onx, X))
#############################
# Way 3
X = np.arange(20).reshape(10, 2)
tr = make_pipeline(CustomOpTransformer(), KMeans(n_clusters=2))
tr.fit(X)
tr_mixin = wrap_as_onnx_mixin(tr)
tr_mixin.to_onnx(X.astype(np.float32))
print(predict_with_onnxruntime(onx, X))
#############################
# Way 4