def test_n_features_float_repeatability(self): X = numpy.arange(20).reshape((5, 4)).astype(numpy.float64) X2 = _modify_dimension(X, 6) X3 = _modify_dimension(X, 6) self.assertEqualArray(X2, X3) X4 = _modify_dimension(X, 6, seed=20) self.assertNotEqualArray(X2, X4)
def test_n_features_int(self): X = numpy.arange(20).reshape((5, 4)).astype(numpy.int64) X2 = _modify_dimension(X, 2) self.assertEqualArray(X[:, :2], X2) X2 = _modify_dimension(X, None) self.assertEqualArray(X, X2) X2 = _modify_dimension(X, 4) self.assertEqualArray(X, X2) X2 = _modify_dimension(X, 6) self.assertEqualArray(X[:, 2:4], X2[:, 2:4]) self.assertNotEqualArray(X[:, :2], X2[:, 4:6]) diff = numpy.sum(numpy.abs(numpy.sign( # pylint: disable=E1101 X[:, :2] - X2[:, :2]).ravel())) # pylint: disable=E1101 self.assertLess(diff, 6)
def test_n_features_float(self): X = numpy.arange(20).reshape((5, 4)).astype(numpy.float64) X2 = _modify_dimension(X, 2) self.assertEqualArray(X[:, :2], X2) X2 = _modify_dimension(X, None) self.assertEqualArray(X, X2) X2 = _modify_dimension(X, 4) self.assertEqualArray(X, X2) X2 = _modify_dimension(X, 6) self.assertEqualArray(X[:, 2:4], X2[:, 2:4]) self.assertNotEqualArray(X[:, :2], X2[:, :2]) self.assertNotEqualArray(X[:, :2], X2[:, 4:6]) cor = numpy.corrcoef(X2) for i in range(0, 2): cor = numpy.corrcoef(X[:, i], X2[:, i]) self.assertLess(cor[0, 1], 0.9999)
def test_rt_svr_simple_test_double(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") logger = getLogger('skl2onnx') logger.disabled = True for nf in range(16, 50): with self.subTest(nf=nf): iris = load_iris() X, y = iris.data, iris.target X = _modify_dimension(X, nf) X_train, X_test, y_train, _ = train_test_split(X, y) clr = SVR(kernel='linear') clr.fit(X_train, y_train) x2 = X_test.astype(numpy.float64) onx = to_onnx(clr, x2) pyrun = OnnxInference(onx, runtime="python") res = pyrun.run({'X': x2}) self.assertIn('variable', res) self.assertEqual(res['variable'].shape, (38, )) self.assertEqualArray(res['variable'], clr.predict(x2), decimal=2)
def _get_dataset(self, nf, dtype): xdtype = self._get_xdtype(dtype) data = load_iris() X, y = data.data, data.target state = numpy.random.RandomState(seed=34) # pylint: disable=E1101 rnd = state.randn(*X.shape) / 3 X += rnd X = _modify_dimension(X, nf) X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42) Xt = X_test.astype(xdtype) yt = y_test.astype(self.par_ydtype) return (X_train, y_train), (Xt, yt)
def test_onnxrt_python_SVR_20(self): iris = load_iris() X, y = iris.data, iris.target X = _modify_dimension(X, 20) X_train, X_test, y_train, _ = train_test_split(X, y, random_state=11) clr = SVR() clr.fit(X_train, y_train) model_def = to_onnx(clr, X_train.astype(numpy.float32)) oinf = OnnxInference(model_def) text = "\n".join(map(lambda x: str(x.ops_), oinf.sequence_)) self.assertIn("SVMRegressor", text) y = oinf.run({'X': X_test.astype(numpy.float32)}) self.assertEqual(list(sorted(y)), ['variable']) lexp = clr.predict(X_test) self.assertEqual(lexp.shape, y['variable'].shape) self.assertEqualArray(lexp, y['variable'], decimal=5)
def _get_dataset(self, nf, dtype): xdtype = self._get_xdtype(dtype) data = load_iris() X, y = data.data, data.target state = numpy.random.RandomState(seed=34) # pylint: disable=E1101 rnd = state.randn(*X.shape) / 3 X += rnd nbclass = len(set(y)) y_ = numpy.zeros((y.shape[0], nbclass), dtype=y.dtype) for i, vy in enumerate(y): y_[i, vy] = 1 y = y_ X = _modify_dimension(X, nf) X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42) X = X_test.astype(xdtype) y = y_test.astype(self.par_ydtype) return (X_train, y_train), (X, y)
def test_onnxrt_python_SVC_proba_double_20(self): iris = load_iris() X, y = iris.data, iris.target X = _modify_dimension(X, 20) X_train, X_test, y_train, _ = train_test_split(X, y, random_state=11) clr = SVC(probability=True) clr.fit(X_train, y_train) model_def = to_onnx(clr, X_train.astype(numpy.float64)) oinf = OnnxInference(model_def) text = "\n".join(map(lambda x: str(x.ops_), oinf.sequence_)) self.assertIn("SVMClassifier", text) y = oinf.run({'X': X_test.astype(numpy.float64)}) self.assertEqual(list(sorted(y)), [ 'output_label', 'output_probability']) lexp = clr.predict(X_test) lprob = clr.predict_proba(X_test) got = y['output_probability'].values self.assertEqual(lexp.shape, y['output_label'].shape) self.assertEqual(lprob.shape, got.shape) self.assertEqualArray(lexp, y['output_label'], decimal=5) self.assertEqualArray(lprob, got, decimal=5)