def test_topk_sanity(self, dtype, axis, sorted):
     x = tensor.vector(name='x', dtype=dtype)
     fn = theano.function([x], topk(x, 1, axis=axis, sorted=sorted))
     xval = np.asarray([1]).astype(dtype)
     yval = fn(xval)
     assert yval == xval
     assert yval.dtype == xval.dtype
示例#2
0
 def test_topk_sanity(self, dtype, axis, sorted):
     x = tensor.vector(name='x', dtype=dtype)
     fn = theano.function([x], topk(x, 1, axis=axis, sorted=sorted))
     xval = np.asarray([1]).astype(dtype)
     yval = fn(xval)
     assert yval == xval
     assert yval.dtype == xval.dtype
示例#3
0
 def test_topk_sanity(self, dtype, axis, sorted):
     x = tensor.vector(name='x', dtype=dtype)
     fn = theano.function([x], topk(x, 1, axis=axis, sorted=sorted), mode=self.mode)
     assert any([isinstance(n.op, self.op_class) for n in fn.maker.fgraph.apply_nodes])
     xval = np.asarray([1]).astype(dtype)
     yval = fn(xval)
     assert yval == xval
     assert yval.dtype == xval.dtype
示例#4
0
 def test_topk_sanity(self, dtype, axis, sorted):
     x = tensor.vector(name="x", dtype=dtype)
     fn = theano.function([x], topk(x, 1, axis=axis, sorted=sorted), mode=self.mode)
     assert any(
         [isinstance(n.op, self.op_class) for n in fn.maker.fgraph.apply_nodes]
     )
     xval = np.asarray([1]).astype(dtype)
     yval = fn(xval)
     assert yval == xval
     assert yval.dtype == xval.dtype
    def test_grad(self, shp, k_, sorted):
        ndim = len(shp)
        for axis in range(-ndim, ndim):
            if isinstance(k_, str):
                k = eval(k_.replace('n', str(shp[axis])))
            else:
                k = k_

            if k == 0:
                continue

            # make input away from undefined gradient (where some inputs are equal)
            xval = gen_unique_vector(reduce(int.__mul__, shp),
                                     dtype=theano.config.floatX).reshape(shp)
            utt.verify_grad(lambda x: topk(x, k, axis=axis, sorted=sorted),
                            [xval],
                            eps=1e-2)
示例#6
0
    def test_grad(self, shp, k_, sorted):
        ndim = len(shp)
        for axis in range(-ndim, ndim):
            if isinstance(k_, str):
                k = eval(k_.replace('n', str(shp[axis])))
            else:
                k = k_

            if k == 0:
                continue

            # make input away from undefined gradient (where some inputs are equal)
            xval = gen_unique_vector(
                reduce(int.__mul__, shp),
                dtype=theano.config.floatX
            ).reshape(shp)
            utt.verify_grad(lambda x: topk(x, k, axis=axis, sorted=sorted), [xval], eps=1e-2)
    def test_topk_1d(self, size, k, dtype, sorted):
        if isinstance(k, str):
            k = eval(k.replace('n', str(size)))

        x = theano.tensor.vector(name='x', dtype=dtype)
        y = topk(x, k, sorted=sorted)
        fn = theano.function([x], y)
        # assert local_useless_topk opt is done properly
        assert 1 == len(fn.maker.fgraph.outputs[0].owner.outputs)

        # generate a all-unique array
        xval = gen_unique_vector(size, dtype)
        yval = fn(xval)
        idx = (slice(-k, None) if k > 0 else slice(-k))
        goal = np.sort(xval)[idx]

        assert yval.dtype == goal.dtype
        utt.assert_allclose(np.sort(yval), goal)
示例#8
0
    def test_topk_1d(self, size, k, dtype, sorted):
        if isinstance(k, str):
            k = eval(k.replace('n', str(size)))

        x = theano.tensor.vector(name='x', dtype=dtype)
        y = topk(x, k, sorted=sorted)
        fn = theano.function([x], y)
        # assert local_useless_topk opt is done properly
        assert 1 == len(fn.maker.fgraph.outputs[0].owner.outputs)

        # generate a all-unique array
        xval = gen_unique_vector(size, dtype)
        yval = fn(xval)
        idx = (slice(-k, None) if k > 0 else slice(-k))
        goal = np.sort(xval)[idx]

        assert yval.dtype == goal.dtype
        utt.assert_allclose(np.sort(yval), goal)
示例#9
0
    def test_topk_1d(self, size, k, dtype, sorted):
        if isinstance(k, str):
            k = eval(k.replace("n", str(size)))

        x = theano.tensor.vector(name="x", dtype=dtype)
        y = topk(x, k, sorted=sorted)
        fn = theano.function([x], y, mode=self.mode)
        assert any(
            [isinstance(n.op, self.op_class) for n in fn.maker.fgraph.apply_nodes]
        )
        # assert local_useless_topk opt is done properly
        assert 1 == len(fn.maker.fgraph.outputs[0].owner.outputs)

        # generate a all-unique array
        xval = gen_unique_vector(size, dtype)
        yval = fn(xval)
        idx = slice(-k, None) if k > 0 else slice(-k)
        goal = np.sort(xval)[idx]

        assert yval.dtype == goal.dtype
        utt.assert_allclose(goal, np.sort(yval))