def test_onnx_remove_redundant_subgraphs_full(self):
        from skl2onnx.algebra.complex_functions import onnx_squareform_pdist
        cop = OnnxAdd(OnnxIdentity('input',
                                   op_version=get_opset_number_from_onnx()),
                      'input',
                      op_version=get_opset_number_from_onnx())
        cdist = onnx_squareform_pdist(cop,
                                      dtype=numpy.float32,
                                      op_version=get_opset_number_from_onnx())
        cdist2 = onnx_squareform_pdist(cop,
                                       dtype=numpy.float32,
                                       op_version=get_opset_number_from_onnx())
        cop2 = OnnxAdd(cdist,
                       cdist2,
                       output_names=['cdist'],
                       op_version=get_opset_number_from_onnx())

        model_def = cop2.to_onnx({'input': FloatTensorType()},
                                 outputs=[('cdist', FloatTensorType())],
                                 target_opset=get_opset_number_from_onnx())
        stats = onnx_statistics(model_def, optim=False)
        new_model = onnx_optimisations(model_def)
        stats2 = onnx_statistics(new_model, optim=False)
        self.assertLess(stats2['size'], stats['size'])
        self.assertLess(stats2['nnodes'], stats['nnodes'])
        self.assertLess(stats2['op_Identity'], stats['op_Identity'])
Пример #2
0
    def test_onnx_subgraphs2(self):
        x = numpy.array([1, 2, 4, 5, 5, 4]).astype(numpy.float32).reshape(
            (3, 2))
        cop = OnnxAdd(OnnxIdentity('input', op_version=TARGET_OPSET),
                      'input',
                      op_version=TARGET_OPSET)
        cdist = onnx_squareform_pdist(cop,
                                      dtype=numpy.float32,
                                      op_version=TARGET_OPSET)
        id1 = [id(a) for a in cdist.onx_op.graph_algebra['body']]
        cdist2 = onnx_squareform_pdist(cop,
                                       dtype=numpy.float32,
                                       op_version=TARGET_OPSET)
        id2 = [id(a) for a in cdist2.onx_op.graph_algebra['body']]
        self.assertNotEqual(id1, id2)
        cop2 = OnnxAdd(cdist,
                       cdist2,
                       output_names=['cdist'],
                       op_version=TARGET_OPSET)

        model_def = cop2.to_onnx({'input': FloatTensorType([None, None])},
                                 outputs=[('cdist',
                                           FloatTensorType([None, None]))],
                                 target_opset=TARGET_OPSET)
        sess = InferenceSession(model_def.SerializeToString())
        res = sess.run(None, {'input': x})
        self.assertEqual(len(res), 1)
Пример #3
0
    def test_onnx_remove_redundant_subgraphs(self):
        from skl2onnx.algebra.complex_functions import onnx_squareform_pdist
        x = numpy.array([1, 2, 4, 5, 5, 4]).astype(numpy.float32).reshape(
            (3, 2))
        cop = OnnxAdd(OnnxIdentity('input', op_version=TARGET_OPSET),
                      'input',
                      op_version=TARGET_OPSET)
        cdist = onnx_squareform_pdist(cop,
                                      dtype=numpy.float32,
                                      op_version=TARGET_OPSET)
        cdist2 = onnx_squareform_pdist(cop,
                                       dtype=numpy.float32,
                                       op_version=TARGET_OPSET)
        cop2 = OnnxAdd(cdist,
                       cdist2,
                       output_names=['cdist'],
                       op_version=TARGET_OPSET)

        model_def = cop2.to_onnx({'input': FloatTensorType()},
                                 outputs=[('cdist', FloatTensorType())],
                                 target_opset=TARGET_OPSET)
        c1 = model_def.SerializeToString()
        stats = onnx_statistics(model_def, optim=False)
        c2 = model_def.SerializeToString()
        self.assertEqual(c1, c2)
        self.assertIn('subgraphs', stats)
        self.assertGreater(stats['subgraphs'], 1)
        self.assertGreater(stats['op_Identity'], 2)

        new_model = onnx_remove_node_redundant(model_def)
        stats2 = onnx_statistics(new_model, optim=False)
        self.assertEqual(stats['subgraphs'], 2)
        # The test is unstable, probably due to variables names.
        # They should be renamed before checking redundancy.
        self.assertIn(stats2['subgraphs'], (1, 2))

        oinf1 = OnnxInference(model_def)
        oinf2 = OnnxInference(new_model)
        y1 = oinf1.run({'input': x})['cdist']
        y2 = oinf2.run({'input': x})['cdist']
        self.assertEqualArray(y1, y2)

        new_model = onnx_remove_node_redundant(model_def)
        stats3 = onnx_statistics(new_model, optim=False)
        self.assertEqual(stats2, stats3)

        new_model = onnx_remove_node(model_def)
        stats3 = onnx_statistics(new_model, optim=False)
        self.assertLess(stats3['size'], stats2['size'])
        self.assertLess(stats3['nnodes'], stats2['nnodes'])
        self.assertLess(stats3['op_Identity'], stats2['op_Identity'])
Пример #4
0
    def test_onnx_example_pdist_in(self):
        opv = _TARGET_OPSET_
        x = np.array([1, 2, 4, 5, 5, 4]).astype(np.float32).reshape((3, 2))
        cop = OnnxAdd('input', 'input', op_version=opv)
        cop2 = OnnxIdentity(onnx_squareform_pdist(cop,
                                                  dtype=np.float32,
                                                  op_version=opv),
                            output_names=['pdist'],
                            op_version=opv)

        model_def = cop2.to_onnx(inputs=[('input',
                                          FloatTensorType([None, None]))],
                                 outputs=[('pdist', FloatTensorType())])

        sess = InferenceSession(model_def.SerializeToString())
        res = sess.run(None, {'input': x})
        exp = squareform(pdist(x * 2, metric="sqeuclidean"))
        assert_almost_equal(exp, res[0])

        x = np.array([1, 2, 4, 5]).astype(np.float32).reshape((2, 2))
        sess = InferenceSession(model_def.SerializeToString())
        res = sess.run(None, {'input': x})
        exp = squareform(pdist(x * 2, metric="sqeuclidean"))
        assert_almost_equal(exp, res[0])

        x = np.array([1, 2, 4, 5, 5, 6]).astype(np.float32).reshape((2, 3))
        x = np.array([1, 2, 4, 5, 5, 4]).astype(np.float32).reshape((2, 3))
        sess = InferenceSession(model_def.SerializeToString())
        res = sess.run(None, {'input': x})
        exp = squareform(pdist(x * 2, metric="sqeuclidean"))
        assert_almost_equal(exp, res[0])
    def test_pdist(self):
        from skl2onnx.algebra.complex_functions import onnx_squareform_pdist
        x = numpy.array([1, 2, 4, 5, 5, 4]).astype(numpy.float32).reshape(
            (3, 2))
        cop = OnnxAdd('input', 'input', op_version=TARGET_OPSET)
        cdist = onnx_squareform_pdist(cop,
                                      dtype=numpy.float32,
                                      op_version=TARGET_OPSET)
        cop2 = OnnxIdentity(cdist,
                            output_names=['cdist'],
                            op_version=TARGET_OPSET)

        model_def = cop2.to_onnx({'input': FloatTensorType()},
                                 outputs=[('cdist', FloatTensorType())],
                                 target_opset=TARGET_OPSET)

        sess = OnnxInference(model_def)
        res = sess.run({'input': x})
        self.assertEqual(list(res.keys()), ['cdist'])

        exp = squareform(pdist(x * 2, metric="sqeuclidean"))
        self.assertEqualArray(exp, res['cdist'])

        x = numpy.array([1, 2, 4, 5, 5, 4]).astype(numpy.float32).reshape(
            (2, 3))
        res = sess.run({'input': x})
        self.assertEqual(list(res.keys()), ['cdist'])
Пример #6
0
    def test_onnx_remove_identities2(self):
        from skl2onnx.algebra.complex_functions import onnx_squareform_pdist
        x = numpy.array([1, 2, 4, 5, 5, 4]).astype(numpy.float32).reshape(
            (3, 2))
        cop = OnnxIdentity('input', op_version=get_opset_number_from_onnx())
        cdist = onnx_squareform_pdist(cop,
                                      dtype=numpy.float32,
                                      op_version=get_opset_number_from_onnx())
        cop2 = OnnxIdentity(cdist,
                            output_names=['cdist'],
                            op_version=get_opset_number_from_onnx())

        model_def = cop2.to_onnx({'input': FloatTensorType()},
                                 outputs=[('cdist', FloatTensorType())],
                                 target_opset=get_opset_number_from_onnx())
        stats = onnx_statistics(model_def, optim=False)
        self.assertIn('subgraphs', stats)
        self.assertGreater(stats['subgraphs'], 1)
        self.assertGreater(stats['op_Identity'], 2)

        new_model = onnx_remove_node_identity(model_def)
        stats2 = onnx_statistics(new_model, optim=False)
        self.assertEqual(stats['subgraphs'], stats2['subgraphs'])
        self.assertLesser(stats2['op_Identity'], 2)

        oinf1 = OnnxInference(model_def)
        oinf2 = OnnxInference(new_model)
        y1 = oinf1.run({'input': x})['cdist']
        y2 = oinf2.run({'input': x})['cdist']
        self.assertEqualArray(y1, y2)
        self.assertLesser(stats2['op_Identity'], 1)
 def kernel_call_ynone(X,
                       length_scale=1.2,
                       periodicity=1.1,
                       pi=3.141592653589793):
     dists = onnx_squareform_pdist(X, metric='euclidean')
     arg = dists / periodicity * pi
     sin_of_arg = numpy.sin(arg)
     K = numpy.exp((sin_of_arg / length_scale)**2 * (-2))
     return K
Пример #8
0
    def test_onnxt_pdist_dot(self):
        from skl2onnx.algebra.complex_functions import onnx_squareform_pdist  # pylint: disable=E0401,E0611
        x = numpy.array([1, 2, 4, 5, 5, 4]).astype(numpy.float32).reshape(
            (3, 2))
        cop = OnnxAdd('input', 'input')
        cdist = onnx_squareform_pdist(cop, dtype=numpy.float32)
        cop2 = OnnxIdentity(cdist, output_names=['cdist'])

        model_def = cop2.to_onnx({'input': x},
                                 outputs=[('cdist', FloatTensorType())])

        oinf = OnnxInference(model_def, skip_run=True)
        dot = oinf.to_dot(recursive=True)
        self.assertIn("B_next_out", dot)
        self.assertIn("cluster", dot)
Пример #9
0
    def test_onnx_stat_recursive(self):
        from skl2onnx.algebra.complex_functions import onnx_squareform_pdist
        cop = OnnxAdd(
            OnnxIdentity('input', op_version=__max_supported_opset__),
            'input', op_version=__max_supported_opset__)
        cdist = onnx_squareform_pdist(
            cop, dtype=numpy.float32, op_version=__max_supported_opset__)
        cop2 = OnnxIdentity(cdist, output_names=['cdist'],
                            op_version=__max_supported_opset__)

        model_def = cop2.to_onnx(
            {'input': FloatTensorType()},
            outputs=[('cdist', FloatTensorType())],
            target_opset=__max_supported_opset__)
        stats = onnx_statistics(model_def)
        self.assertIn('subgraphs', stats)
        self.assertGreater(stats['subgraphs'], 1)
        self.assertGreater(stats['op_Identity'], 2)