Exemplo n.º 1
0
 def test_trace_method(self):
     class Test(object):
         def test(self, x):
             return x + 1
     t = Test()
     s = Symbolic(t.test)
     x = np.random.random((3, 4))
     self.assertTrue(np.allclose(s.trace(x)[1].eval(), t.test(x)))
Exemplo n.º 2
0
 def test_compile_function(self):
     def f(x):
         return x * 5 + 10 * np.ones((3, 4))
     s = Symbolic(f)
     x = np.random.random((3, 4))
     o = s.trace(x)[1]
     F = s.compile_function(x, o)
     self.assertTrue(np.allclose(F(x), f(x)))
Exemplo n.º 3
0
 def test_compile_function(self):
     def f(x):
         return x * 5 + 10 * np.ones((3, 4))
     s = Symbolic(f)
     x = np.random.random((3, 4))
     o = s.trace(x)[1]
     F = s.compile_function(x, o)
     self.assertTrue(np.allclose(F(x), f(x)))
Exemplo n.º 4
0
 def test_trace_method(self):
     class Test(object):
         def test(self, x):
             return x + 1
     t = Test()
     s = Symbolic(t.test)
     x = np.random.random((3, 4))
     self.assertTrue(np.allclose(s.trace(x)[1].eval(), t.test(x)))
Exemplo n.º 5
0
    def test_compile_gradient(self):
        def f(x):
            return x ** 2
        s = Symbolic(f)
        x = np.random.random((3, 4))
        o = s.trace(x)[1]
        self.assertRaises(TypeError, s.compile_function_gradient, x, o)

        F = s.compile_gradient(x, o.sum())
        self.assertTrue(np.allclose(F(x), 2 * x))
Exemplo n.º 6
0
    def test_compile_gradient(self):
        def f(x):
            return x ** 2
        s = Symbolic(f)
        x = np.random.random((3, 4))
        o = s.trace(x)[1]
        self.assertRaises(TypeError, s.compile_function_gradient, x, o)

        F = s.compile_gradient(x, o.sum())
        self.assertTrue(np.allclose(F(x), 2 * x))
Exemplo n.º 7
0
    def test_class(self):
        class Test(object):
            def f(self, x):
                return x + 100.0

            @classmethod
            def g(cls, x):
                return x + 100.0

            @staticmethod
            def h(x):
                return x + 100.0

        t = Test()
        s = Symbolic()
        x = 1.0
        o = s.trace(t.f, x)
        f = s.compile_function(x, o)
        assert(f(2.0) == 102.0)

        o = s.trace(t.g, x)
        f = s.compile_function(x, o)
        assert(f(2.0) == 102.0)

        o = s.trace(t.h, x)
        f = s.compile_function(x, o)
        assert(f(2.0) == 102.0)
Exemplo n.º 8
0
    def test_force_floatX(self):
        def f(x):
            return x + 1
        s = Symbolic(f, force_floatX=False)
        x = np.random.random((3, 4)).astype('int')
        o = s.trace(x)[1]
        F = s.compile_function(x, o)
        self.assertTrue(np.allclose(F(x), f(x)))

        s2 = Symbolic(f, force_floatX=True)
        o2 = s2.trace(x)[1]
        F2 = s2.compile_function(x, o2)
        self.assertTrue(np.allclose(F2(x), f(x).astype(theano.config.floatX)))
Exemplo n.º 9
0
def as_symbolic(fn=None, **kwargs):
    """
    Wraps a function with an AutoDiff Symbolic instance, meaning it will act
    as a function expecting and operating on Theano objects.

    The function is not compiled.

    Use:
        @as_symbolic
        def python_function(...):
            return do_something()

        python_function(...) # calls function as if it worked with Theano objs

    """
    if isinstance(fn, collections.Callable):
        return Symbolic(fn, **kwargs)
    else:

        def function_wrapper(pyfn):
            return Symbolic(pyfn, **kwargs)

        return function_wrapper
Exemplo n.º 10
0
    def test_symbolic(self):
        def f1(x):
            return x + 1.0

        def f2(x):
            return x * 2.0

        def f3(x):
            return x ** 2
        s = Symbolic()
        x = np.random.random((3, 4))
        o1 = s.trace(f1, x)
        o2 = s.trace(f2, o1)
        o3 = s.trace(f3, o2)

        # test function
        f = s.compile_function(x, o3)
        self.assertTrue(np.allclose(f(x), f3(f2(f1(x)))))

        # test gradient
        o4 = s.trace(lambda x: x.sum(), o3)
        g = s.compile_gradient(x, o4, wrt=x)
        self.assertTrue(np.allclose(g(x), 8 * (x+1)))
Exemplo n.º 11
0
    def test_symbolic_readme(self):
        """ the README example"""

        # -- a vanilla function
        def f1(x):
            return x + 2

        # -- a function referencing a global variable
        y = np.random.random(10)

        def f2(x):
            return x * y

        # -- a function with a local variable
        def f3(x):
            z = tag(np.ones(10), 'local_var')
            return (x + z) ** 2

        # -- create a general symbolic tracer and apply
        #    it to the three functions
        x = np.random.random(10)
        tracer = Symbolic()

        out1 = tracer.trace(f1, x)
        out2 = tracer.trace(f2, out1)
        out3 = tracer.trace(f3, out2)

        # -- compile a function representing f(x, y, z) = out3
        new_fn = tracer.compile_function(inputs=[x, y, 'local_var'],
                                         outputs=out3)

        # -- compile the gradient of f(x) = out3, with respect to y
        fn_grad = tracer.compile_gradient(inputs=x,
                                          outputs=out3,
                                          wrt=y,
                                          reduction=theano.tensor.sum)

        assert fn_grad  # to stop flake error

        self.assertTrue(np.allclose(new_fn(x, y, np.ones(10)), f3(f2(f1(x)))))
Exemplo n.º 12
0
 def test_trace(self):
     def f(x):
         return x * 5 + 10 * np.ones((3, 4))
     s = Symbolic(f)
     x = np.random.random((3, 4))
     self.assertTrue(np.allclose(s.trace(x)[1].eval(), f(x)))
Exemplo n.º 13
0
 def test_trace(self):
     def f(x):
         return x * 5 + 10 * np.ones((3, 4))
     s = Symbolic(f)
     x = np.random.random((3, 4))
     self.assertTrue(np.allclose(s.trace(x)[1].eval(), f(x)))
Exemplo n.º 14
0
 def function_wrapper(pyfn):
     return Symbolic(pyfn, **kwargs)