Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
sess = InferenceSession(model_def.SerializeToString())
res = sess.run(None, {'input': x})
exp = scipy_cdist(x * 2, x2, metric="sqeuclidean")
assert_almost_equal(exp, res[0], decimal=5)
x = np.array([[6.1, 2.8, 4.7, 1.2],
[5.7, 3.8, 1.7, 0.3],
[7.7, 2.6, 6.9, 2.3],
[6.0, 2.9, 4.5, 1.5],
[6.8, 2.8, 4.8, 1.4],
[5.4, 3.4, 1.5, 0.4],
[5.6, 2.9, 3.6, 1.3],
[6.9, 3.1, 5.1, 2.3]], dtype=np.float32)
cop = OnnxAdd('input', 'input')
cop2 = OnnxIdentity(onnx_cdist(cop, x, dtype=np.float32),
output_names=['cdist'])
model_def = cop2.to_onnx(
inputs=[('input', FloatTensorType([None, None]))],
outputs=[('cdist', FloatTensorType())])
sess = InferenceSession(model_def.SerializeToString())
res = sess.run(None, {'input': x})
exp = scipy_cdist(x * 2, x, metric="sqeuclidean")
assert_almost_equal(exp, res[0], decimal=4)
assert "u_scan0_" not in str(model_def)
return
res = sess.run(None, {'input': x})
exp = scipy_cdist(x * 2, x2, metric="sqeuclidean")
assert_almost_equal(exp, res[0], decimal=5)
x = np.array([[6.1, 2.8, 4.7, 1.2],
[5.7, 3.8, 1.7, 0.3],
[7.7, 2.6, 6.9, 2.3],
[6.0, 2.9, 4.5, 1.5],
[6.8, 2.8, 4.8, 1.4],
[5.4, 3.4, 1.5, 0.4],
[5.6, 2.9, 3.6, 1.3],
[6.9, 3.1, 5.1, 2.3]], dtype=np.float32)
cop = OnnxAdd(
'input', 'input', op_version=onnx.defs.onnx_opset_version())
cop2 = OnnxIdentity(
OnnxCDist(cop, x,
op_version=onnx.defs.onnx_opset_version()),
output_names=['cdist'],
op_version=onnx.defs.onnx_opset_version())
model_def = cop2.to_onnx(
inputs=[('input', FloatTensorType([None, None]))],
outputs=[('cdist', FloatTensorType())])
sess = InferenceSession(model_def.SerializeToString())
res = sess.run(None, {'input': x})
exp = scipy_cdist(x * 2, x, metric="sqeuclidean")
assert_almost_equal(exp, res[0], decimal=4)
def dummy_converter(scope, operator, container):
X = operator.inputs[0]
out = operator.outputs
id1 = OnnxIdentity(X)
id2 = OnnxIdentity(id1, output_names=out[1:])
id2.add_to(scope, container)
def convert_nearest_neighbors_transform(scope, operator, container):
"""
Converts *NearestNeighbors* into *ONNX*.
"""
many = _convert_nearest_neighbors(operator, container)
top_indices, top_distances = many[:2]
out = operator.outputs
ind = OnnxIdentity(top_indices, output_names=out[:1])
dist = OnnxIdentity(top_distances, output_names=out[1:])
dist.add_to(scope, container)
ind.add_to(scope, container)
def _onnx_cdist_begin(op_version):
diff = OnnxSub('next_in', 'next', output_names=[
'diff'], op_version=op_version)
id_next = OnnxIdentity('next_in', output_names=[
'next_out'], op_version=op_version)
return diff, id_next
def _onnx_cdist_sqeuclidean(XA, XB, dtype=None, op_version=None,
dim_in=None, dim_out=None, **kwargs):
"""
Returns the ONNX graph which computes
``cdist(X, metric='sqeuclidean')``.
"""
diff, id_next = _onnx_cdist_begin(op_version)
norm = OnnxReduceSumSquare(diff, output_names=['norm'], axes=[
1], keepdims=0, op_version=op_version)
flat = OnnxIdentity(norm, output_names=['scan_out'], op_version=op_version)
return _onnx_cdist_end(XA, XB, id_next, flat, dtype, op_version,
dim_in=dim_in, dim_out=dim_out, **kwargs)
def _onnx_cdist_manhattan(XA, XB, dtype=None, op_version=None,
dim_in=None, dim_out=None, **kwargs):
"""
Returns the ONNX graph which computes the Manhattan distance
or ``Manhattan(X, Y)``.
"""
diff, id_next = _onnx_cdist_begin(op_version)
diff_pow = OnnxAbs(diff, op_version=op_version)
norm = OnnxReduceSum(diff_pow, axes=[1], output_names=[
'norm'], keepdims=0, op_version=op_version)
flat = OnnxIdentity(norm, output_names=['scan_out'], op_version=op_version)
return _onnx_cdist_end(XA, XB, id_next, flat, dtype, op_version,
dim_in=dim_in, dim_out=dim_out, **kwargs)
"""
if optim is None:
dists = onnx_cdist(X, Y, dtype=dtype, metric="sqeuclidean",
op_version=op_version)
elif optim == 'cdist':
dists = OnnxCDist(X, Y, metric="sqeuclidean", op_version=op_version)
else:
raise ValueError("Unknown optimization '{}'.".format(optim))
cst = length_scale ** 2 * alpha * 2
t_cst = py_make_float_array(cst, dtype=dtype)
tmp = OnnxDiv(dists, t_cst, op_version=op_version)
t_one = py_make_float_array(1, dtype=dtype)
base = OnnxAdd(tmp, t_one, op_version=op_version)
t_alpha = py_make_float_array(-alpha, dtype=dtype)
K = OnnxPow(base, t_alpha, op_version=op_version)
return OnnxIdentity(K, op_version=op_version, **kwargs)