def test_onnx_example_cdist_bigger(self):

        from skl2onnx.algebra.complex_functions import onnx_cdist
        data = load_iris()
        X, y = data.data, data.target
        self.assertNotEmpty(y)
        X_train = X[::2]
        # y_train = y[::2]
        X_test = X[1::2]
        # y_test = y[1::2]
        onx = OnnxIdentity(onnx_cdist(OnnxIdentity('X',
                                                   op_version=TARGET_OPSET),
                                      X_train.astype(numpy.float32),
                                      metric="euclidean",
                                      dtype=numpy.float32,
                                      op_version=TARGET_OPSET),
                           output_names=['Y'],
                           op_version=TARGET_OPSET)
        final = onx.to_onnx(inputs=[('X', FloatTensorType([None, None]))],
                            outputs=[('Y', FloatTensorType())],
                            target_opset=TARGET_OPSET)

        oinf = OnnxInference(final, runtime="python")
        res = oinf.run({'X': X_train.astype(numpy.float32)})['Y']
        exp = scipy_cdist(X_train, X_train, metric="euclidean")
        self.assertEqualArray(exp, res, decimal=6)
        res = oinf.run({'X': X_test.astype(numpy.float32)})['Y']
        exp = scipy_cdist(X_test, X_train, metric="euclidean")
        self.assertEqualArray(exp, res, decimal=6)
    def test_onnx_example_cdist_in(self):
        x = np.array([1, 2, 4, 5, 5, 4]).astype(np.float32).reshape((3, 2))
        x2 = np.array([1.1, 2.1, 4.01, 5.01, 5.001, 4.001, 0,
                       0]).astype(np.float32).reshape((4, 2))
        cop = OnnxAdd('input', 'input')
        cop2 = OnnxIdentity(onnx_cdist(cop, x2, 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, 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)
Exemplo n.º 3
0
    def test_onnx_example_cdist_in_minkowski(self):
        x = numpy.array([1, 2, 1, 3, 2, 2, 2,
                         3]).astype(numpy.float32).reshape((4, 2))
        x2 = numpy.array([[1, 2], [2, 2], [2.1, 2.1],
                          [2, 2]]).astype(numpy.float32).reshape((4, 2))
        for pp in [1, 2]:
            with self.subTest(pp=pp):
                cop = OnnxIdentity('input',
                                   op_version=get_opset_number_from_onnx())
                cop2 = OnnxIdentity(onnx_cdist(
                    cop,
                    x2,
                    dtype=numpy.float32,
                    metric="minkowski",
                    p=pp,
                    op_version=get_opset_number_from_onnx()),
                                    output_names=['cdist'],
                                    op_version=get_opset_number_from_onnx())

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

                try:
                    sess = OnnxInference(model_def)
                except RuntimeError as e:
                    raise AssertionError("Issue\n{}".format(model_def)) from e
                res = sess.run({'input': x})['cdist']
                exp = scipy_cdist(x, x2, metric="minkowski", p=pp)
                self.assertEqualArray(exp, res, decimal=5)

        with self.subTest(pp=3):
            x = numpy.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=numpy.float32)
            cop = OnnxAdd('input',
                          'input',
                          op_version=get_opset_number_from_onnx())
            cop2 = OnnxIdentity(onnx_cdist(
                cop,
                x,
                dtype=numpy.float32,
                metric="minkowski",
                p=3,
                op_version=get_opset_number_from_onnx()),
                                output_names=['cdist'],
                                op_version=get_opset_number_from_onnx())

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

            sess = OnnxInference(model_def)
            res = sess.run({'input': x})['cdist']
            exp = scipy_cdist(x * 2, x, metric="minkowski", p=3)
            self.assertEqualArray(exp, res, decimal=4)
    def test_onnx_example_cdist_in(self):
        from skl2onnx.algebra.complex_functions import onnx_cdist
        x = numpy.array([1, 2, 4, 5, 5, 4]).astype(numpy.float32).reshape(
            (3, 2))
        x2 = numpy.array([1.1, 2.1, 4.01, 5.01, 5.001, 4.001, 0,
                          0]).astype(numpy.float32).reshape((4, 2))
        cop = OnnxAdd('input',
                      'input',
                      op_version=get_opset_number_from_onnx())
        cop2 = OnnxIdentity(onnx_cdist(
            cop,
            x2,
            dtype=numpy.float32,
            op_version=get_opset_number_from_onnx()),
                            output_names=['cdist'],
                            op_version=get_opset_number_from_onnx())

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

        sess = OnnxInference(model_def)
        res = sess.run({'input': x})
        exp = scipy_cdist(x * 2, x2, metric="sqeuclidean")
        self.assertEqualArray(exp, res['cdist'], decimal=5)

        x = numpy.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., 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=numpy.float32)
        cop = OnnxAdd('input',
                      'input',
                      op_version=get_opset_number_from_onnx())
        cop2 = OnnxIdentity(onnx_cdist(
            cop,
            x,
            dtype=numpy.float32,
            op_version=get_opset_number_from_onnx()),
                            output_names=['cdist'],
                            op_version=get_opset_number_from_onnx())

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

        sess = OnnxInference(model_def)
        res = sess.run({'input': x})
        exp = scipy_cdist(x * 2, x, metric="sqeuclidean")
        self.assertEqualArray(exp, res['cdist'], decimal=4)
    def test_onnx_example_cdist_in_custom_ops(self):
        x = np.array([1, 2, 4, 5, 5, 4]).astype(np.float32).reshape((3, 2))
        x2 = np.array([1.1, 2.1, 4.01, 5.01, 5.001, 4.001, 0, 0]).astype(
            np.float32).reshape((4, 2))
        opv = _TARGET_OPSET_
        cop = OnnxAdd(
            'input', 'input', op_version=opv)
        cop2 = OnnxIdentity(
            OnnxCDist(cop, x2, op_version=opv),
            output_names=['cdist'],
            op_version=opv)

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

        try:
            sess = InferenceSession(model_def.SerializeToString())
        except RuntimeError as e:
            if "CDist is not a registered" in str(e):
                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=opv)
        cop2 = OnnxIdentity(
            OnnxCDist(cop, x,
                      op_version=opv),
            output_names=['cdist'],
            op_version=opv)

        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)
Exemplo n.º 6
0
    def kneighbors(self, X=None, n_neighbors=None, return_distance=True):
        """Finds the K-neighbors of a point.

        Returns indices of and distances to the neighbors of each point.

        Parameters
        ----------
        X : array-like, shape (n_ts, sz, d)
            The query time series.
            If not provided, neighbors of each indexed point are returned.
            In this case, the query point is not considered its own neighbor.
        n_neighbors : int
            Number of neighbors to get (default is the value passed to the constructor).
        return_distance : boolean, optional. Defaults to True.
            If False, distances will not be returned

        Returns
        -------
        dist : array
            Array representing the distance to points, only present if return_distance=True
        ind : array
            Indices of the nearest points in the population matrix.
        """
        self_neighbors = False
        if n_neighbors is None:
            n_neighbors = self.n_neighbors
        if X is None:
            X = self._fit_X
            self_neighbors = True
        else:
            X = to_time_series_dataset(X)
        if self.metric == "dtw":
            cdist_fun = cdist_dtw
        elif self.metric in ["euclidean", "sqeuclidean", "cityblock"]:
            cdist_fun = lambda X, Xp: scipy_cdist(X.reshape((X.shape[0], -1)),
                                                  Xp.reshape(
                                                      (Xp.shape[0], -1)),
                                                  metric=self.metric)
        else:
            raise ValueError(
                "Unrecognized time series metric string: %s (should be one of 'dtw', 'euclidean', "
                "'sqeuclidean' or 'cityblock')" % self.metric)
        full_dist_matrix = cdist_fun(X, self._fit_X)
        ind = numpy.argsort(full_dist_matrix, axis=1)

        if self_neighbors:
            ind = ind[:, 1:]
        if n_neighbors > full_dist_matrix.shape[1]:
            n_neighbors = full_dist_matrix.shape[1]
        ind = ind[:, :n_neighbors]

        n_ts = X.shape[0]
        sample_range = numpy.arange(n_ts)[:, None]
        dist = full_dist_matrix[sample_range, ind]

        if return_distance:
            return dist, ind
        else:
            return ind
Exemplo n.º 7
0
    def _score(self, x_array: np.ndarray) -> np.ndarray:
        if len(x_array.shape) == 1:
            x_array = x_array.reshape((1, -1))

        num = x_array.shape[0]
        out = np.empty((num), dtype=float)

        for i in range(num):
            x = x_array[i]
            assert x.shape == (self.num_parameters(),)

            samples = self.state_.points_of_failure() / x[np.newaxis, :]
            distances = scipy_cdist(self.ones_, samples)
            out[i] = float(np.min(distances))

        return out * 100
Exemplo n.º 8
0
    def update(self, time):
        sa = np.exp(-SAFETY_AWARENESS)  # 安全意识
        spead_rate = SPREAD_RATE * sa  # 感染率

        death_rate = DEATH_RATE  # 死亡率

        #  平均潜伏时间
        latent_time = np.random.normal(LATENT_TIME,
                                       SCALE,
                                       size=(CITY_PEOPLE_NUM, 1))
        # 平均治疗时间
        theatment_time = np.random.normal(THEATMENT_TIME,
                                          SCALE,
                                          size=(CITY_PEOPLE_NUM, 1))
        # 平均免疫期
        immune_time = np.random.normal(IMMUNE_TIME,
                                       SCALE,
                                       size=(CITY_PEOPLE_NUM, 1))

        coord = self.getCoordinate()
        # 计算坐标矩阵的欧式距离
        coord_dists = scipy_cdist(coord, coord)
        status = self.conf['status']
        for index, people in enumerate(self.peoples):
            if people[status] == UNINFECTED_STATUS:
                people = self.uninfected_people(coord_dists, index, people,
                                                time, spead_rate)
            elif people[status] == LATENT_STATUS:
                people = self.latent_people(people, time, latent_time, index)
            elif people[status] == CONFIRMED_STATUS:
                people = self.confirmed_pepole(people, time, death_rate)
            elif people[status] == ISOLATION_STATUS:
                people = self.isolation_people(people, time, theatment_time,
                                               index, death_rate)
            elif people[status] == IMMUNE_STATUS:
                people = self.immune_people(people, time, immune_time, index)

        x = self.conf['x']
        y = self.conf['y']
        action_rate = ACTION_RATE * sa  # 行动意向

        # 走动
        self.peoples[:, [x, y]] += action_rate * SCALE * np.random.randn(
            CITY_PEOPLE_NUM, 2) / 50
Exemplo n.º 9
0
 def cdist_fun(X, Xp):
     return scipy_cdist(X.reshape((X.shape[0], -1)),
                        Xp.reshape((Xp.shape[0], -1)),
                        metric=self.metric)
Exemplo n.º 10
0
 def scipy_cdist_metric(X, Y, *args):
     return scipy_cdist(X, Y, metric=metric)
Exemplo n.º 11
0
    def kneighbors(self, X=None, n_neighbors=None, return_distance=True):
        """Finds the K-neighbors of a point.

        Returns indices of and distances to the neighbors of each point.

        Parameters
        ----------
        X : array-like, shape (n_ts, sz, d)
            The query time series.
            If not provided, neighbors of each indexed point are returned.
            In this case, the query point is not considered its own neighbor.
        n_neighbors : int
            Number of neighbors to get (default is the value passed to the
            constructor).
        return_distance : boolean, optional. Defaults to True.
            If False, distances will not be returned

        Returns
        -------
        dist : array
            Array representing the distance to points, only present if
            return_distance=True
        ind : array
            Indices of the nearest points in the population matrix.
        """
        self_neighbors = False
        if n_neighbors is None:
            n_neighbors = self.n_neighbors
        if X is None:
            X = self._X_fit
            self_neighbors = True
        if self.metric == "precomputed":
            full_dist_matrix = X
        else:

            if X.ndim == 2:  # sklearn-format case
                X = X.reshape((X.shape[0], -1, self._d))
                fit_X = self._X_fit.reshape((self._X_fit.shape[0],
                                             -1,
                                             self._d))
            elif hasattr(self, '_ts_fit') and self._ts_fit is not None:
                fit_X = self._ts_fit
            else:
                fit_X = self._X_fit

            if (self.metric in TSLEARN_VALID_METRICS or
                    self.metric in [cdist_dtw, cdist_ctw,
                                    cdist_soft_dtw, cdist_sax]):
                full_dist_matrix = self._precompute_cross_dist(X,
                                                               other_X=fit_X)
            elif self.metric in ["euclidean", "sqeuclidean", "cityblock"]:
                full_dist_matrix = scipy_cdist(X.reshape((X.shape[0], -1)),
                                               fit_X.reshape((fit_X.shape[0],
                                                              -1)),
                                               metric=self.metric)
            else:
                raise ValueError("Unrecognized time series metric string: %s "
                                 "(should be one of 'dtw', 'softdtw', "
                                 "'sax', 'euclidean', 'sqeuclidean' "
                                 "or 'cityblock')" % self.metric)

        # Code similar to sklearn (sklearn/neighbors/base.py), to make sure
        # that TimeSeriesKNeighbor~(metric='euclidean') has the same results as
        # feeding a distance matrix to sklearn.KNeighbors~(metric='euclidean')
        kbin = min(n_neighbors - 1, full_dist_matrix.shape[1] - 1)
        # argpartition will make sure the first `kbin` entries are the
        # `kbin` smallest ones (but in arbitrary order) --> complexity: O(n)
        ind = numpy.argpartition(full_dist_matrix, kbin, axis=1)

        if self_neighbors:
            ind = ind[:, 1:]
        if n_neighbors > full_dist_matrix.shape[1]:
            n_neighbors = full_dist_matrix.shape[1]
        ind = ind[:, :n_neighbors]

        n_ts = X.shape[0]
        sample_range = numpy.arange(n_ts)[:, None]
        # Sort the `kbin` nearest neighbors according to distance
        ind = ind[
            sample_range, numpy.argsort(full_dist_matrix[sample_range, ind])]
        dist = full_dist_matrix[sample_range, ind]

        if hasattr(self, '_ts_metric'):
            self.metric = self._ts_metric

        if return_distance:
            return dist, ind
        else:
            return ind
Exemplo n.º 12
0
    def test_onnx_example_cdist_in_euclidean(self):
        for metric in ['euclidean', 'minkowski']:
            for opv in [11, TARGET_OPSET]:
                with self.subTest(metric=metric, opv=opv):
                    x = numpy.array([1, 2, 4, 5, 5,
                                     4]).astype(numpy.float32).reshape((3, 2))
                    x2 = numpy.array(
                        [1.1, 2.1, 4.01, 5.01, 5.001, 4.001, 0,
                         0]).astype(numpy.float32).reshape((4, 2))
                    cop = OnnxAdd('input', 'input', op_version=opv)

                    if metric == "minkowski":
                        cop2 = OnnxIdentity(onnx_cdist(cop,
                                                       x2,
                                                       dtype=numpy.float32,
                                                       metric=metric,
                                                       op_version=opv,
                                                       p=2),
                                            output_names=['cdist'],
                                            op_version=opv)
                    else:
                        cop2 = OnnxIdentity(onnx_cdist(cop,
                                                       x2,
                                                       dtype=numpy.float32,
                                                       metric=metric,
                                                       op_version=opv),
                                            output_names=['cdist'],
                                            op_version=opv)

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

                    sess = OnnxInference(model_def)
                    res = sess.run({'input': x})['cdist']
                    exp = scipy_cdist(x * 2, x2, metric=metric)
                    self.assertEqualArray(exp, res, decimal=5)

                    if metric == "minkowski":
                        continue
                    x = numpy.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=numpy.float32)
                    cop = OnnxAdd('input', 'input', op_version=opv)
                    cop2 = OnnxIdentity(onnx_cdist(cop,
                                                   x,
                                                   dtype=numpy.float32,
                                                   op_version=opv),
                                        output_names=['cdist'],
                                        op_version=opv)

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

                    sess = OnnxInference(model_def)
                    res = sess.run({'input': x})['cdist']
                    exp = scipy_cdist(x * 2, x, metric="sqeuclidean")
                    self.assertEqualArray(exp, res, decimal=4)