Ejemplo n.º 1
0
    def test_kernel_cosine_float(self):
        ker = PairwiseKernel(metric='cosine')

        # X, X
        onx = convert_kernel(ker, 'X', output_names=['Y'], dtype=np.float32,
                             op_version=_TARGET_OPSET_)
        model_onnx = onx.to_onnx(
            inputs=[('X', FloatTensorType([None, None]))],
            target_opset=TARGET_OPSET)

        x = np.random.randn(4, 3)
        x[0, 0] = x[1, 1] = x[2, 2] = 10.
        x[3, 2] = 5.

        sess = InferenceSession(model_onnx.SerializeToString())
        res = sess.run(None, {'X': x.astype(np.float32)})[0]
        m1 = res
        m2 = ker(x)
        assert_almost_equal(m1, m2, decimal=5)

        # X, x
        onx = convert_kernel(ker, 'X', x_train=x,
                             output_names=['Y'], dtype=np.float32,
                             op_version=_TARGET_OPSET_)
        model_onnx = onx.to_onnx(
            inputs=[('X', FloatTensorType([None, None]))],
            target_opset=TARGET_OPSET)

        sess = InferenceSession(model_onnx.SerializeToString())
        res = sess.run(None, {'X': x.astype(np.float32)})[0]
        m1 = res
        m2 = ker(x)
        assert_almost_equal(m1, m2, decimal=5)
    def test_kernel_ker2_exp_sine_squared(self):
        ker = ExpSineSquared()
        onx = convert_kernel(ker,
                             'X',
                             output_names=['Y'],
                             dtype=np.float32,
                             op_version=onnx_opset_version())
        model_onnx = onx.to_onnx(inputs=[('X', FloatTensorType([None, None]))],
                                 dtype=np.float32)
        sess = InferenceSession(model_onnx.SerializeToString())
        res = sess.run(None, {'X': Xtest_.astype(np.float32)})[0]
        m1 = res
        m2 = ker(Xtest_)
        assert_almost_equal(m1, m2, decimal=4)

        onx = convert_kernel(ker,
                             'X',
                             output_names=['Z'],
                             x_train=Xtest_ * 2,
                             dtype=np.float32,
                             op_version=onnx_opset_version())
        model_onnx = onx.to_onnx(inputs=[('X', FloatTensorType([None, None]))],
                                 dtype=np.float32)
        sess = InferenceSession(model_onnx.SerializeToString())
        res = sess.run(None, {'X': Xtest_.astype(np.float32)})[0]
        m1 = res
        m2 = ker(Xtest_, Xtest_ * 2)
        assert_almost_equal(m1, m2, decimal=4)
    def test_kernel_rational_quadratic(self):
        ker = RationalQuadratic()
        onx = convert_kernel(ker,
                             'X',
                             output_names=['Y'],
                             dtype=np.float32,
                             op_version=_TARGET_OPSET_)
        model_onnx = onx.to_onnx(inputs=[('X', FloatTensorType([None, None]))],
                                 target_opset=_TARGET_OPSET_)
        sess = InferenceSession(model_onnx.SerializeToString())
        res = sess.run(None, {'X': Xtest_.astype(np.float32)})[0]
        m1 = res
        m2 = ker(Xtest_)
        assert_almost_equal(m1, m2, decimal=5)

        onx = convert_kernel(ker,
                             'X',
                             output_names=['Z'],
                             x_train=(Xtest_ * 2).astype(np.float32),
                             dtype=np.float32,
                             op_version=_TARGET_OPSET_)
        model_onnx = onx.to_onnx(inputs=[('X', FloatTensorType([None, None]))])
        sess = InferenceSession(model_onnx.SerializeToString())
        res = sess.run(None, {'X': Xtest_.astype(np.float32)})[0]
        m1 = res
        m2 = ker(Xtest_, Xtest_ * 2)
        assert_almost_equal(m1, m2, decimal=3)
    def test_kernel_dot_product(self):
        ker = DotProduct()
        onx = convert_kernel(ker,
                             'X',
                             output_names=['Y'],
                             dtype=np.float32,
                             op_version=_TARGET_OPSET_)
        model_onnx = onx.to_onnx(inputs=[('X', FloatTensorType([None, None]))],
                                 dtype=np.float32)
        sess = InferenceSession(model_onnx.SerializeToString())
        res = sess.run(None, {'X': Xtest_.astype(np.float32)})[0]
        m1 = res
        m2 = ker(Xtest_)
        assert_almost_equal(m1 / 1000, m2 / 1000, decimal=5)

        onx = convert_kernel(ker,
                             'X',
                             output_names=['Z'],
                             x_train=Xtest_ * 2,
                             dtype=np.float32,
                             op_version=_TARGET_OPSET_)
        model_onnx = onx.to_onnx(inputs=[('X', FloatTensorType([None, None]))],
                                 dtype=np.float32)
        sess = InferenceSession(model_onnx.SerializeToString())
        res = sess.run(None, {'X': Xtest_.astype(np.float32)})[0]
        m1 = res
        m2 = ker(Xtest_, Xtest_ * 2)
        assert_almost_equal(m1 / 1000, m2 / 1000, decimal=5)
Ejemplo n.º 5
0
 def test_kernel_ker2_def_ort(self):
     ker = Sum(
         CK(0.1, (1e-3, 1e3)) * RBF(length_scale=10,
                                    length_scale_bounds=(1e-3, 1e3)),
         CK(0.1, (1e-3, 1e3)) * RBF(length_scale=1,
                                    length_scale_bounds=(1e-3, 1e3)))
     onx = convert_kernel(ker, 'X', output_names=['Y'], dtype=numpy.float32,
                          op_version=get_opset_number_from_onnx())
     model_onnx = onx.to_onnx(
         inputs=[('X', FloatTensorType([None, None]))],
         outputs=[('Y', FloatTensorType([None, None]))],
         target_opset=get_opset_number_from_onnx())
     model_onnx.ir_version = get_ir_version_from_onnx()
     sess = _capture_output(
         lambda: OnnxInference(model_onnx.SerializeToString(),
                               runtime="onnxruntime2"), 'c')[0]
     try:
         res = sess.run({'X': Xtest_.astype(numpy.float32)})
     except RuntimeError as e:
         if "Got invalid dimensions for input" in str(e):
             # probable bug somewhere
             return
         raise e
     m1 = res['Y']
     m2 = ker(Xtest_)
     self.assertEqualArray(m1, m2, decimal=5)
 def test_kernel_rbf1(self):
     ker = RBF(length_scale=1, length_scale_bounds=(1e-3, 1e3))
     onx = convert_kernel(ker, 'X', output_names=['Y'], dtype=np.float32)
     model_onnx = onx.to_onnx(inputs=[('X', FloatTensorType([None, None]))])
     sess = InferenceSession(model_onnx.SerializeToString())
     res = sess.run(None, {'X': Xtest_.astype(np.float32)})[0]
     m1 = res
     m2 = ker(Xtest_)
     assert_almost_equal(m1, m2, decimal=5)
 def test_kernel_constant1(self):
     ker = C(5.)
     onx = convert_kernel(ker, 'X', output_names=['Y'], dtype=np.float32)
     model_onnx = onx.to_onnx(inputs=[('X', FloatTensorType([None, None]))],
                              dtype=np.float32)
     sess = InferenceSession(model_onnx.SerializeToString())
     res = sess.run(None, {'X': Xtest_.astype(np.float32)})[0]
     m1 = res
     m2 = ker(Xtest_)
     assert_almost_equal(m1, m2, decimal=5)
 def test_kernel_exp_sine_squared(self):
     from skl2onnx.operator_converters.gaussian_process import convert_kernel
     ker = ExpSineSquared()
     onx = convert_kernel(ker, 'X', output_names=['Y'], dtype=numpy.float32)
     model_onnx = onx.to_onnx(inputs=[('X', FloatTensorType([None, None]))])
     sess = OnnxInference(model_onnx)
     Xtest_ = numpy.arange(6).reshape((3, 2))
     res = sess.run({'X': Xtest_.astype(numpy.float32)})
     m1 = res['Y']
     m2 = ker(Xtest_)
     self.assertEqualArray(m1, m2, decimal=5)
 def test_kernel_rbf1(self):
     from skl2onnx.operator_converters.gaussian_process import convert_kernel
     ker = RBF(length_scale=1, length_scale_bounds=(1e-3, 1e3))
     onx = convert_kernel(ker, 'X', output_names=['Y'], dtype=numpy.float32)
     model_onnx = onx.to_onnx(inputs=[('X', FloatTensorType([None, None]))])
     sess = OnnxInference(model_onnx)
     Xtest_ = numpy.arange(6).reshape((3, 2))
     res = sess.run({'X': Xtest_.astype(numpy.float32)})
     m1 = res['Y']
     m2 = ker(Xtest_)
     self.assertEqualArray(m1, m2)
 def test_kernel_rbf_mul(self):
     ker = (C(1.0, constant_value_bounds="fixed") *
            RBF(1.0, length_scale_bounds="fixed"))
     onx = convert_kernel(ker, 'X', output_names=['Y'], dtype=np.float32,
                          op_version=_TARGET_OPSET_)
     model_onnx = onx.to_onnx(
         inputs=[('X', FloatTensorType([None, None]))])
     sess = InferenceSession(model_onnx.SerializeToString())
     res = sess.run(None, {'X': Xtest_.astype(np.float32)})[0]
     m1 = res
     m2 = ker(Xtest_)
     assert_almost_equal(m1, m2, decimal=5)
 def test_kernel_ker12_def(self):
     ker = (Sum(CK(0.1, (1e-3, 1e3)), CK(0.1, (1e-3, 1e3)) *
                RBF(length_scale=1, length_scale_bounds=(1e-3, 1e3))))
     onx = convert_kernel(ker, 'X', output_names=['Y'], dtype=numpy.float32)
     model_onnx = onx.to_onnx(
         inputs=[('X', FloatTensorType([None, None]))],
         outputs=[('Y', FloatTensorType([None, None]))])
     sess = OnnxInference(model_onnx.SerializeToString())
     res = sess.run({'X': Xtest_.astype(numpy.float32)})
     m1 = res['Y']
     m2 = ker(Xtest_)
     self.assertEqualArray(m1, m2)
Ejemplo n.º 12
0
 def test_kernel_ker12_def(self):
     ker = (Sum(C(0.1, (1e-3, 1e3)), C(0.1, (1e-3, 1e3)) *
            RBF(length_scale=1, length_scale_bounds=(1e-3, 1e3))))
     onx = convert_kernel(ker, 'X', output_names=['Y'], dtype=np.float32,
                          op_version=_TARGET_OPSET_)
     model_onnx = onx.to_onnx(
         inputs=[('X', FloatTensorType([None, None]))],
         target_opset=_TARGET_OPSET_)
     sess = InferenceSession(model_onnx.SerializeToString())
     res = sess.run(None, {'X': Xtest_.astype(np.float32)})[0]
     m1 = res
     m2 = ker(Xtest_)
     assert_almost_equal(m1, m2, decimal=5)
    def test_kernel_ker2_def_ort1(self):
        ker = Sum(
            CK(0.1, (1e-3, 1e3)) * RBF(length_scale=10,
                                       length_scale_bounds=(1e-3, 1e3)),
            CK(0.1, (1e-3, 1e3)) * RBF(length_scale=1,
                                       length_scale_bounds=(1e-3, 1e3))
        )
        onx = convert_kernel(ker, 'X', output_names=['Y'], dtype=numpy.float32)
        model_onnx = onx.to_onnx(
            inputs=[('X', FloatTensorType([None, None]))],
            outputs=[('Y', FloatTensorType([None, None]))])
        sess = OnnxInference(model_onnx.SerializeToString(),
                             runtime="onnxruntime1")

        rows = []

        def myprint(*args, **kwargs):
            rows.append(" ".join(map(str, args)))

        res = sess.run({'X': Xtest_.astype(numpy.float32)},
                       intermediate=True, verbose=1, fLOG=myprint)
        self.assertGreater(len(rows), 2)
        m1 = res['Y']
        self.assertNotEmpty(m1)
        self.assertGreater(len(res), 2)
        # m2 = ker(Xtest_)
        # self.assertEqualArray(m1, m2, decimal=5)

        cpu = OnnxInference(model_onnx.SerializeToString())
        sbs = side_by_side_by_values(
            [cpu, sess], inputs={'X': Xtest_.astype(numpy.float32)})
        self.assertGreater(len(sbs), 2)
        self.assertIsInstance(sbs, list)
        self.assertIsInstance(sbs[0], dict)
        self.assertIn('step', sbs[0])
        self.assertIn('step', sbs[1])
        self.assertIn('metric', sbs[0])
        self.assertIn('metric', sbs[1])
        self.assertIn('cmp', sbs[0])
        self.assertIn('cmp', sbs[1])

        sess3 = OnnxInference(model_onnx.SerializeToString(),
                              runtime="onnxruntime2")
        sbs = side_by_side_by_values(
            [cpu, sess, sess3], inputs={'X': Xtest_.astype(numpy.float32)})
        self.assertNotEmpty(sbs)

        inputs = {'X': Xtest_.astype(numpy.float32)}
        sbs = side_by_side_by_values(
            [(cpu, inputs), (sess, inputs), (sess3, inputs)])
        self.assertNotEmpty(sbs)
    def test_kernel_ker2_dotproduct(self):
        ker = DotProduct(sigma_0=2.)
        onx = convert_kernel(ker, 'X', output_names=['Y'], dtype=np.float32)
        model_onnx = onx.to_onnx(inputs=[('X', FloatTensorType())],
                                 outputs=[('Y', FloatTensorType())],
                                 dtype=np.float32)
        sess = InferenceSession(model_onnx.SerializeToString())

        x = np.array([[1, 2], [3, 4], [5, 6]], dtype=np.float32)
        res = sess.run(None, {'X': x})
        m1 = res[0]
        m2 = ker(x)
        assert_almost_equal(m1, m2, decimal=5)

        res = sess.run(None, {'X': Xtest_.astype(np.float32)})
        m1 = res[0]
        m2 = ker(Xtest_)
        assert_almost_equal(m1, m2, decimal=2)
Ejemplo n.º 15
0
    def test_kernel_cosine_double(self):
        ker = PairwiseKernel(metric='cosine')
        onx = convert_kernel(ker, 'X', output_names=['Y'], dtype=np.float64,
                             op_version=_TARGET_OPSET_)
        model_onnx = onx.to_onnx(
            inputs=[('X', DoubleTensorType([None, None]))],
            target_opset=TARGET_OPSET)

        x = np.random.randn(4, 3)
        x[0, 0] = x[1, 1] = x[2, 2] = 10.
        x[3, 2] = 5.

        try:
            sess = InferenceSession(model_onnx.SerializeToString())
        except NotImplemented:
            # Failed to find kernel for FusedMatMul(1).
            return
        res = sess.run(None, {'X': x.astype(np.float64)})[0]
        m1 = res
        m2 = ker(x)
        assert_almost_equal(m1, m2, decimal=5)
Ejemplo n.º 16
0
    def test_kernel_ker2_def(self):
        ker = Sum(
            CK(0.1, (1e-3, 1e3)) * RBF(length_scale=10,
                                       length_scale_bounds=(1e-3, 1e3)),
            CK(0.1, (1e-3, 1e3)) * RBF(length_scale=1,
                                       length_scale_bounds=(1e-3, 1e3))
        )
        onx = convert_kernel(ker, 'X', output_names=['Y'], dtype=numpy.float32,
                             op_version=get_opset_number_from_onnx())
        model_onnx = onx.to_onnx(
            inputs=[('X', FloatTensorType([None, None]))],
            outputs=[('Y', FloatTensorType([None, None]))],
            target_opset=get_opset_number_from_onnx())
        sess = OnnxInference(model_onnx.SerializeToString())

        res = sess.run({'X': Xtest_.astype(numpy.float32)})
        m1 = res['Y']
        m2 = ker(Xtest_)
        self.assertEqualArray(m1, m2)

        res = sess.run({'X': Xtest_.astype(numpy.float32)}, intermediate=True)
        self.assertGreater(len(res), 30)
        self.assertIsInstance(res, dict)
Ejemplo n.º 17
0
    def test_kernel_ker2_def_ort1(self):
        ker = Sum(
            CK(0.1, (1e-3, 1e3)) * RBF(length_scale=10,
                                       length_scale_bounds=(1e-3, 1e3)),
            CK(0.1, (1e-3, 1e3)) * RBF(length_scale=1,
                                       length_scale_bounds=(1e-3, 1e3))
        )
        onx = convert_kernel(ker, 'X', output_names=['Y'], dtype=numpy.float32,
                             op_version=get_opset_number_from_onnx())
        model_onnx = onx.to_onnx(
            inputs=[('X', FloatTensorType([None, None]))],
            outputs=[('Y', FloatTensorType([None, None]))],
            target_opset=get_opset_number_from_onnx())
        model_onnx.ir_version = get_ir_version_from_onnx()
        sess = OnnxInference(model_onnx.SerializeToString(),
                             runtime="onnxruntime1")

        rows = []

        def myprint(*args, **kwargs):
            rows.append(" ".join(map(str, args)))

        res = _capture_output(
            lambda: sess.run({'X': Xtest_.astype(numpy.float32)},
                             intermediate=True, verbose=1, fLOG=myprint),
            'c')[0]
        self.assertGreater(len(rows), 2)
        m1 = res['Y']
        self.assertNotEmpty(m1)
        self.assertGreater(len(res), 2)
        # m2 = ker(Xtest_)
        # self.assertEqualArray(m1, m2, decimal=5)

        cpu = OnnxInference(model_onnx.SerializeToString())
        sbs = side_by_side_by_values(
            [cpu, sess], inputs={'X': Xtest_.astype(numpy.float32)})
        self.assertGreater(len(sbs), 2)
        self.assertIsInstance(sbs, list)
        self.assertIsInstance(sbs[0], dict)
        self.assertIn('step', sbs[0])
        self.assertIn('step', sbs[1])
        self.assertIn('metric', sbs[0])
        self.assertIn('metric', sbs[1])
        self.assertIn('cmp', sbs[0])
        self.assertIn('cmp', sbs[1])

        sess3 = _capture_output(
            lambda: OnnxInference(model_onnx.SerializeToString(),
                                  runtime="onnxruntime2"), 'c')[0]
        try:
            sbs = side_by_side_by_values(
                [cpu, sess, sess3], inputs={'X': Xtest_.astype(numpy.float32)})
        except RuntimeError as e:
            if "Got invalid dimensions for input" in str(e):
                # probable bug somewhere
                return
            raise e
        self.assertNotEmpty(sbs)

        inputs = {'X': Xtest_.astype(numpy.float32)}
        sbs = side_by_side_by_values(
            [(cpu, inputs), (sess, inputs), (sess3, inputs)])
        self.assertNotEmpty(sbs)