예제 #1
0
    def test_infer_shape(self):
        # Test using default parameters' value
        self._compile_and_check(
            [self.x, self.v],
            [searchsorted(self.x, self.v)],
            [self.a[self.idx_sorted], self.b],
            self.op_class,
        )

        # Test parameter ``sorter``
        sorter = T.vector("sorter", dtype="int32")
        self._compile_and_check(
            [self.x, self.v, sorter],
            [searchsorted(self.x, self.v, sorter=sorter)],
            [self.a, self.b, self.idx_sorted],
            self.op_class,
        )

        # Test parameter ``side``
        la = np.ones(10).astype(config.floatX)
        lb = np.ones(shape=(1, 2, 3)).astype(config.floatX)
        self._compile_and_check(
            [self.x, self.v],
            [searchsorted(self.x, self.v, side="right")],
            [la, lb],
            self.op_class,
        )
예제 #2
0
 def test_searchsortedOp_on_right_side(self):
     f = theano.function(
         [self.x, self.v], searchsorted(self.x, self.v, side="right")
     )
     assert np.allclose(
         np.searchsorted(self.a, self.b, side="right"), f(self.a, self.b)
     )
    def auc_error(self, T_prediction, true_label):
        # get TPR1, TPR2, label_1, FPR1, T1, T2 from the estimated distribution
        idx = searchsorted(self.T, T_prediction, side='left')

        # sometimes you need to extrapolate, sometimes you don't. Choose appropriate points
        idx_l = T.switch(T.eq((1-true_label)*self.extrapolate_left [idx-1],1), 0, self.idx_left [idx-1]                  )
        idx_r = T.switch(T.eq(   true_label *self.extrapolate_right[idx]  ,1), self.N_F+self.N_P+1,self.idx_right[idx  ], )

        T1 = self.T[idx_l]
        TPR1 = self.TPR[idx_l]
        FPR1 = self.FPR[idx_l]

        T2 = self.T[idx_r]
        TPR2 = self.TPR[idx_r]
        FPR2 = self.FPR[idx_r]

        l = true_label
        f = 1-l
        AUC1 = self.AUC0 + f*TPR1 + l*(self.N_F-FPR1)
        dAUC = (TPR2 - TPR1)*f - (FPR2 - FPR1 - 1)*l

        # deal with the fact that T_prediction, T1 and T2 can all be equal, and make the gradient behave nicely
        # when that happens.
        # Epsilon is a bad solution, it messes up the gradient!
        coef = T.switch(T.eq(T2,T1),0.5*(T_prediction-T1),(T_prediction-T1)/(T2-T1) )
        #coef = (T_prediction-T1)/(T2-T1)

        norm = ((self.N_P+l) * (self.N_F+f))
        if self.delta_auc_instead:
            return T.switch(T.eq(norm,0.0), 1.0, coef*dAUC/norm)
        AUCt = AUC1 + coef*dAUC
        return T.switch(T.eq(norm,0.0), 1.0, AUCt/norm)
예제 #4
0
 def test_searchsortedOp_on_int_sorter(self):
     compatible_types = ('int8', 'int16', 'int32', 'int64',)
     # 'uint8', 'uint16', 'uint32', 'uint64')
     for dtype in compatible_types:
         sorter = T.vector('sorter', dtype=dtype)
         f = theano.function([self.x, self.v, sorter],
                             searchsorted(self.x, self.v, sorter=sorter),
                             allow_input_downcast=True)
         assert np.allclose(np.searchsorted(self.a, self.b, sorter=self.idx_sorted),
                            f(self.a, self.b, self.idx_sorted))
예제 #5
0
    def test_infer_shape(self):
        # Test using default parameters' value
        self._compile_and_check([self.x, self.v],
                                [searchsorted(self.x, self.v)],
                                [self.a[self.idx_sorted], self.b],
                                self.op_class)

        # Test parameter ``sorter``
        sorter = T.vector('sorter', dtype="int32")
        self._compile_and_check([self.x, self.v, sorter],
                                [searchsorted(self.x, self.v, sorter=sorter)],
                                [self.a, self.b, self.idx_sorted],
                                self.op_class)

        # Test parameter ``side``
        la = np.ones(10).astype(config.floatX)
        lb = np.ones(shape=(1, 2, 3)).astype(config.floatX)
        self._compile_and_check([self.x, self.v],
                                [searchsorted(self.x, self.v, side='right')],
                                [la, lb],
                                self.op_class)
예제 #6
0
 def test_searchsortedOp_on_int_sorter(self):
     compatible_types = ('int8', 'int16', 'int32')
     if theano.configdefaults.python_int_bitwidth() == 64:
         compatible_types += ('int64',)
     # 'uint8', 'uint16', 'uint32', 'uint64')
     for dtype in compatible_types:
         sorter = T.vector('sorter', dtype=dtype)
         f = theano.function([self.x, self.v, sorter],
                             searchsorted(self.x, self.v, sorter=sorter),
                             allow_input_downcast=True)
         assert np.allclose(np.searchsorted(self.a, self.b, sorter=self.idx_sorted),
                            f(self.a, self.b, self.idx_sorted))
예제 #7
0
    def test_searchsortedOp_on_sorted_input(self):
        f = theano.function([self.x, self.v], searchsorted(self.x, self.v))
        assert np.allclose(np.searchsorted(self.a[self.idx_sorted], self.b),
                           f(self.a[self.idx_sorted], self.b))

        sorter = T.vector('sorter', dtype='int32')
        f = theano.function([self.x, self.v, sorter], self.x.searchsorted(self.v, sorter=sorter, side='right'))
        assert np.allclose(self.a.searchsorted(self.b, sorter=self.idx_sorted, side='right'),
                           f(self.a, self.b, self.idx_sorted))

        sa = self.a[self.idx_sorted]
        f = theano.function([self.x, self.v], self.x.searchsorted(self.v, side='right'))
        assert np.allclose(sa.searchsorted(self.b, side='right'), f(sa, self.b))
예제 #8
0
 def test_searchsortedOp_on_int_sorter(self):
     compatible_types = ("int8", "int16", "int32")
     if PYTHON_INT_BITWIDTH == 64:
         compatible_types += ("int64",)
     # 'uint8', 'uint16', 'uint32', 'uint64')
     for dtype in compatible_types:
         sorter = tt.vector("sorter", dtype=dtype)
         f = theano.function(
             [self.x, self.v, sorter],
             searchsorted(self.x, self.v, sorter=sorter),
             allow_input_downcast=True,
         )
         assert np.allclose(
             np.searchsorted(self.a, self.b, sorter=self.idx_sorted),
             f(self.a, self.b, self.idx_sorted),
         )
예제 #9
0
    def test_searchsortedOp_on_sorted_input(self):
        f = theano.function([self.x, self.v], searchsorted(self.x, self.v))
        assert np.allclose(
            np.searchsorted(self.a[self.idx_sorted], self.b),
            f(self.a[self.idx_sorted], self.b),
        )

        sorter = tt.vector("sorter", dtype="int32")
        f = theano.function(
            [self.x, self.v, sorter],
            self.x.searchsorted(self.v, sorter=sorter, side="right"),
        )
        assert np.allclose(
            self.a.searchsorted(self.b, sorter=self.idx_sorted, side="right"),
            f(self.a, self.b, self.idx_sorted),
        )

        sa = self.a[self.idx_sorted]
        f = theano.function([self.x, self.v], self.x.searchsorted(self.v, side="right"))
        assert np.allclose(sa.searchsorted(self.b, side="right"), f(sa, self.b))
예제 #10
0
 def test_searchsortedOp_on_right_side(self):
     f = theano.function([self.x, self.v],
                         searchsorted(self.x, self.v, side='right'))
     assert np.allclose(np.searchsorted(self.a, self.b, side='right'),
                        f(self.a, self.b))
예제 #11
0
 def test_searchsortedOp_on_no_1d_inp(self):
     no_1d = T.dmatrix("no_1d")
     with pytest.raises(ValueError):
         searchsorted(no_1d, self.v)
     with pytest.raises(ValueError):
         searchsorted(self.x, self.v, sorter=no_1d)
예제 #12
0
 def test_searchsortedOp_wrong_side_kwd(self):
     with pytest.raises(ValueError):
         searchsorted(self.x, self.v, side="asdfa")
예제 #13
0
 def test_searchsortedOp_on_float_sorter(self):
     sorter = T.vector("sorter", dtype="float32")
     with pytest.raises(TypeError):
         searchsorted(self.x, self.v, sorter=sorter)