def test_speedup_transform64_op_version(self): data = load_iris() X, _ = data.data, data.target spd = OnnxSpeedupTransformer(PCA(), target_opset=self.opset(), enforce_float32=False) spd.fit(X) opset = spd.op_version self.assertGreater(self.opset(), opset[''])
def test_speedup_transform64_onnx(self): data = load_iris() X, _ = data.data, data.target spd = OnnxSpeedupTransformer(PCA(), target_opset=self.opset(), enforce_float32=False) spd.fit(X) expected = spd.transform(X) onx = to_onnx(spd, X[:1]) oinf = OnnxInference(onx) got = oinf.run({'X': X})['variable'] self.assertEqualArray(expected, got)
def test_speedup_transform64(self): data = load_iris() X, _ = data.data, data.target spd = OnnxSpeedupTransformer(PCA(), target_opset=self.opset(), enforce_float32=False) spd.fit(X) spd.assert_almost_equal(X)
def test_speedup_transform32_numpy(self): data = load_iris() X, _ = data.data, data.target spd = OnnxSpeedupTransformer(PCA(), target_opset=self.opset(), runtime="numpy") spd.fit(X) spd.assert_almost_equal(X, decimal=5)
def test_speedup_transform32_weight(self): data = load_iris() X, y = data.data, data.target spd = OnnxSpeedupTransformer(StandardScaler(), target_opset=self.opset()) w = numpy.ones(y.shape, dtype=X.dtype) spd.fit(X, sample_weight=w) spd.assert_almost_equal(X, decimal=5)
def test_speedup_transform32_numba(self): data = load_iris() X, _ = data.data, data.target X = X.astype(numpy.float32) spd = OnnxSpeedupTransformer(PCA(), target_opset=self.opset(), runtime="numba") spd.fit(X) spd.assert_almost_equal(X, decimal=5) self.assertIn("CPUDispatch", str(spd.onnxrt_.func))
def test_speedup_transform64_pickle(self): data = load_iris() X, _ = data.data, data.target spd = OnnxSpeedupTransformer(PCA(), target_opset=self.opset(), enforce_float32=False) spd.fit(X) st = BytesIO() pickle.dump(spd, st) st2 = BytesIO(st.getvalue()) spd2 = pickle.load(st2) expected = spd.transform(X) got = spd2.transform(X) self.assertEqualArray(expected, got) expected = spd.raw_transform(X) got = spd2.raw_transform(X) self.assertEqualArray(expected, got)
import matplotlib.pyplot as plt from sklearn.datasets import make_regression from sklearn.decomposition import PCA from pyquickhelper.pycode.profiling import profile from mlprodict.sklapi import OnnxSpeedupTransformer from cpyquickhelper.numbers.speed_measure import measure_time from tqdm import tqdm ################################ # Data and models to test. data, _ = make_regression(1000, n_features=20) data = data.astype(numpy.float32) models = [ ('sklearn', PCA(n_components=10)), ('python', OnnxSpeedupTransformer(PCA(n_components=10), runtime='python')), ('onnxruntime1', OnnxSpeedupTransformer(PCA(n_components=10), runtime='onnxruntime1')), ('numpy', OnnxSpeedupTransformer(PCA(n_components=10), runtime='numpy')), ('numba', OnnxSpeedupTransformer(PCA(n_components=10), runtime='numba')) ] ################################# # Training. for name, model in tqdm(models): model.fit(data) ################################# # Profiling of runtime `onnxruntime1`.