Exemple #1
0
    def test_not(self):
        x, y, z = ints("xyz")
        fn = gof.DualLinker().accept(FunctionGraph([x, y], [invert(x)])).make_function()
        for a, b in ((0, 1), (0, 0), (1, 0), (1, 1)):
            assert fn(a, b) == ~a, (a,)

        x, y, z = ints("xyz")
        fn = gof.DualLinker().accept(FunctionGraph([x, y], [~x])).make_function()
        for a, b in ((0, 1), (0, 0), (1, 0), (1, 1)):
            assert fn(a, b) == ~a, (a,)
Exemple #2
0
    def test_and(self):
        x, y, z = ints('xyz')
        fn = gof.DualLinker().accept(FunctionGraph([x, y], [and_(x, y)])).make_function()
        for a, b in ((0, 1), (0, 0), (1, 0), (1, 1)):
            self.assertTrue(fn(a, b) == (a & b), (a, b))

        x, y, z = ints('xyz')
        fn = gof.DualLinker().accept(FunctionGraph([x, y], [x & y])).make_function()
        for a, b in ((0, 1), (0, 0), (1, 0), (1, 1)):
            self.assertTrue(fn(a, b) == (a & b), (a, b))
Exemple #3
0
    def test_not(self):
        x, y, z = ints('xyz')
        fn = gof.DualLinker().accept(Env([x, y], [invert(x)])).make_function()
        for a, b in ((0, 1), (0, 0), (1, 0), (1, 1)):
            self.assertTrue(fn(a, b) == ~a, (a, ))

        x, y, z = ints('xyz')
        fn = gof.DualLinker().accept(Env([x, y], [~x])).make_function()
        for a, b in ((0, 1), (0, 0), (1, 0), (1, 1)):
            self.assertTrue(fn(a, b) == ~a, (a, ))
Exemple #4
0
 def test_straightforward(self):
     x, y, z = inputs()
     e = mul(add(x, y), div_proxy(x, y))
     C = Composite([x, y], [e])
     c = C.make_node(x, y)
     # print c.c_code(['x', 'y'], ['z'], dict(id = 0))
     g = FunctionGraph([x, y], [c.out])
     fn = gof.DualLinker().accept(g).make_function()
     assert fn(1.0, 2.0) == 1.5
Exemple #5
0
 def test_with_constants(self):
     x, y, z = inputs()
     e = mul(add(70.0, y), div_proxy(x, y))
     C = Composite([x, y], [e])
     c = C.make_node(x, y)
     assert "70.0" in c.op.c_code(c, 'dummy', ['x', 'y'], ['z'], dict(id=0))
     # print c.c_code(['x', 'y'], ['z'], dict(id = 0))
     g = FunctionGraph([x, y], [c.out])
     fn = gof.DualLinker().accept(g).make_function()
     assert fn(1.0, 2.0) == 36.0
Exemple #6
0
 def test_many_outputs(self):
     x, y, z = inputs()
     e0 = x + y + z
     e1 = x + y * z
     e2 = x / y
     C = Composite([x, y, z], [e0, e1, e2])
     c = C.make_node(x, y, z)
     # print c.c_code(['x', 'y', 'z'], ['out0', 'out1', 'out2'], dict(id = 0))
     g = FunctionGraph([x, y, z], c.outputs)
     fn = gof.DualLinker().accept(g).make_function()
     assert fn(1.0, 2.0, 3.0) == [6.0, 7.0, 0.5]
Exemple #7
0
    def tes_mod(self):
        # We add this test as not all language and C implementation give the same
        # sign to the result. This check that the c_code of `Mod` is implemented
        # as Python. That is what we want.

        x, y = ints('xy')
        fn = gof.DualLinker().accept(FunctionGraph([x, y],
                                                   [x % y])).make_function()
        for a, b in ((0, 1), (1, 1), (0, -1), (1, -1), (-1, -1), (1, 2), (-1,
                                                                          2),
                     (1, -2), (-1, -2), (5, 3), (-5, 3), (5, -3), (-5, -3)):
            self.assertTrue(fn(a, b) == a % b, (a, ))
Exemple #8
0
    def test_clip_grad(self):
        # This is testing for the issue #633
        x, y = floats('xy')
        a = theano.tensor.clip(x, y, x)
        g = theano.gradient.grad(a, x)
        fn = gof.DualLinker().accept(FunctionGraph([x, y],
                                                   [g])).make_function()

        # Test the other way around as well
        a2 = theano.tensor.clip(x, x, y)
        g2 = theano.gradient.grad(a2, x)
        fn2 = gof.DualLinker().accept(FunctionGraph([x, y],
                                                    [g2])).make_function()

        # Test for the equal case too .
        a3 = theano.tensor.clip(x, x, x)
        g3 = theano.gradient.grad(a3, x)
        fn3 = gof.DualLinker().accept(FunctionGraph([x], [g3])).make_function()

        rng = np.random.RandomState(utt.fetch_seed())

        ntests = 50
        for i in xrange(ntests):
            xval = rng.rand(1)
            # To ensure that the min < x .
            yval_mn = rng.rand(1) - 1.0

            # To ensure that the max > x.
            yval_mx = rng.rand(1) + 1.0

            aval = fn(xval, yval_mn)
            aval2 = fn2(xval, yval_mx)
            aval3 = fn3(xval)
            self.assertTrue(aval == 1.)
            self.assertTrue(aval2 == 1.)
            self.assertTrue(aval3 == 1.)
Exemple #9
0
    def test_composite_printing(self):
        x, y, z = floats('xyz')
        e0 = x + y + z
        e1 = x + y * z
        e2 = x / y
        e3 = x // 5
        e4 = -x
        e5 = x - y
        e6 = x**y + (-z)
        e7 = x % 3
        C = Composite([x, y, z], [e0, e1, e2, e3, e4, e5, e6, e7])
        c = C.make_node(x, y, z)
        g = FunctionGraph([x, y, z], c.outputs)
        gof.DualLinker().accept(g).make_function()

        assert str(g) == ('[*1 -> Composite{((i0 + i1) + i2),'
                          ' (i0 + (i1 * i2)), (i0 / i1), '
                          '(i0 // Constant{5}), '
                          '(-i0), (i0 - i1), ((i0 ** i1) + (-i2)),'
                          ' (i0 % Constant{3})}(x, y, z), '
                          '*1::1, *1::2, *1::3, *1::4, *1::5, *1::6, *1::7]')
Exemple #10
0
 def test_eq(self):
     x, y, z = inputs()
     fn = gof.DualLinker().accept(FunctionGraph([x, y], [eq(x, y)])).make_function()
     for a, b in ((3.0, 9), (3, 0.9), (3, 3)):
         assert fn(a, b) == (a == b)
Exemple #11
0
 def test_straightforward(self):
     x, y, z = inputs()
     e = mul(add(x, y), div_proxy(x, y))
     g = FunctionGraph([x, y], [e])
     fn = gof.DualLinker().accept(g).make_function()
     assert fn(1.0, 2.0) == 1.5
Exemple #12
0
 def test_neq(self):
     x, y, z = inputs()
     fn = gof.DualLinker().accept(FunctionGraph(
         [x, y], [neq(x, y)])).make_function()
     for a, b in ((3., 9), (3, 0.9), (3, 3)):
         self.assertTrue(fn(a, b) == (a != b))
Exemple #13
0
        if x != y:
            raise Exception("Output mismatch.", {
                'performlinker': x,
                'clinker': y
            })


# If a string is passed as the linker argument in the constructor for
# Mode, it will be used as the key to retrieve the real linker in this
# dictionary
predefined_linkers = {
    'py': gof.PerformLinker(),  # Use allow_gc Theano flag
    'c': gof.CLinker(),  # Don't support gc. so don't check allow_gc
    'c|py': gof.OpWiseCLinker(),  # Use allow_gc Theano flag
    'c|py_nogc': gof.OpWiseCLinker(allow_gc=False),
    'c&py': gof.DualLinker(checker=check_equal),  # Deprecated
    'vm': gof.vm.VM_Linker(use_cloop=False),  # Use allow_gc Theano flag
    'cvm': gof.vm.VM_Linker(use_cloop=True),  # Use allow_gc Theano flag
    'vm_nogc': gof.vm.VM_Linker(allow_gc=False, use_cloop=False),
    'cvm_nogc': gof.vm.VM_Linker(allow_gc=False, use_cloop=True),
}


def register_linker(name, linker):
    """Add a `Linker` which can be referred to by `name` in `Mode`."""
    if name in predefined_linkers:
        raise ValueError('Linker name already taken: %s' % name)
    predefined_linkers[name] = linker


# If a string is passed as the optimizer argument in the constructor
Exemple #14
0
        if x != y:
            raise Exception("Output mismatch.", {
                'performlinker': x,
                'clinker': y
            })


# If a string is passed as the linker argument in the constructor for
# Mode, it will be used as the key to retrieve the real linker in this
# dictionary
predefined_linkers = {
    'py': gof.PerformLinker(),
    'c': gof.CLinker(),
    'c|py': gof.OpWiseCLinker(),
    'c|py_nogc': gof.OpWiseCLinker(allow_gc=False),
    'c&py': gof.DualLinker(checker=check_equal),
    'vm': gof.vm.VM_Linker(use_cloop=False),
    'cvm': gof.vm.VM_Linker(use_cloop=True),
    'vm_nogc': gof.vm.VM_Linker(allow_gc=False, use_cloop=False),
    'cvm_nogc': gof.vm.VM_Linker(allow_gc=False, use_cloop=True),
}


def register_linker(name, linker):
    """Add a `Linker` which can be referred to by `name` in `Mode`."""
    if name in predefined_linkers:
        raise ValueError('Linker name already taken: %s' % name)
    predefined_linkers[name] = linker


# If a string is passed as the optimizer argument in the constructor
Exemple #15
0
 def test_xor(self):
     x, y, z = ints('xyz')
     fn = gof.DualLinker().accept(Env([x, y], [x ^ y])).make_function()
     for a, b in ((0, 1), (0, 0), (1, 0), (1, 1)):
         self.assertTrue(fn(a, b) == (a ^ b), (a, b))
Exemple #16
0
def test_mul_add_div_proxy():
    x, y, z = floats("xyz")
    e = mul(add(x, y), div_proxy(x, y))
    g = FunctionGraph([x, y], [e])
    fn = gof.DualLinker().accept(g).make_function()
    assert fn(1.0, 2.0) == 1.5
Exemple #17
0
 def test_neq(self):
     x, y, z = floats("xyz")
     fn = gof.DualLinker().accept(FunctionGraph([x, y], [neq(x, y)])).make_function()
     for a, b in ((3.0, 9), (3, 0.9), (3, 3)):
         assert fn(a, b) == (a != b)
Exemple #18
0
 def test_xor(self):
     x, y, z = ints("xyz")
     fn = gof.DualLinker().accept(FunctionGraph([x, y], [x ^ y])).make_function()
     for a, b in ((0, 1), (0, 0), (1, 0), (1, 1)):
         assert fn(a, b) == (a ^ b), (a, b)
Exemple #19
0
 def test_ge(self):
     x, y, z = inputs()
     fn = gof.DualLinker().accept(Env([x, y], [x >= y])).make_function()
     for a, b in ((3., 9), (3, 0.9), (3, 3)):
         self.assertTrue(fn(a, b) == (a >= b))