def test_onnxt_runtime_cdist(self): for metric in ['sqeuclidean', 'euclidean']: with self.subTest(metric=metric): X = numpy.array([[2, 1], [0, 1]], dtype=float) Y = numpy.array([[2, 1, 5], [0, 1, 3]], dtype=float).T Z = cdist(X, Y, metric=metric) onx = OnnxCDist('X', 'Y', output_names=['Z'], metric=metric, op_version=get_opset_number_from_onnx()) model_def = onx.to_onnx( { 'X': X.astype(numpy.float32), 'Y': Y.astype(numpy.float32) }, outputs={'Z': Z.astype(numpy.float32)}, target_opset=get_opset_number_from_onnx()) self.assertIn('s: "%s"' % metric, str(model_def)) oinf = OnnxInference(model_def) got = oinf.run({'X': X, 'Y': Y}) self.assertEqual(list(sorted(got)), ['Z']) self.assertEqualArray(Z, got['Z'], decimal=6) python_tested.append(OnnxCDist) oinfpy = OnnxInference(model_def, runtime="python", inplace=True) validate_python_inference(oinfpy, { 'X': X.astype(numpy.float32), 'Y': Y.astype(numpy.float32) }, tolerance=1e-6)
import onnxruntime as rt from onnxruntime import InferenceSession import skl2onnx from skl2onnx.algebra.custom_ops import OnnxCDist from skl2onnx.common.data_types import FloatTensorType X = np.ones((2, 4), dtype=np.float32) Y = np.ones((3, 4), dtype=np.float32) Y *= 2 print(cdist(X, Y, metric='euclidean')) #################################### # ONNX op = OnnxCDist('X', 'Y', op_version=12, output_names=['Z'], metric='euclidean') onx = op.to_onnx({'X': X, 'Y': Y}, outputs=[('Z', FloatTensorType())]) print(onx) ######################################## # CDist and onnxruntime # +++++++++++++++++++++ # # We compute the output of CDist operator # with onnxruntime. sess = InferenceSession(onx.SerializeToString()) res = sess.run(None, {'X': X, 'Y': Y}) print(res) ##################################### # Benchmark