Exemple #1
0
 def test_multiple_sources(self):
     """Test that passing multiple sources works"""
     class O(gof.op.Op):
         def __init__(self, tst, grad_ok):
             self.tst = tst
             self.grad_ok = grad_ok
         def make_node(self, *inputs):
             outputs = [gof.generic(),gof.generic()]
             return gof.Apply(self, inputs, outputs)
         def grad(self, inputs, grads):
             g0, g1 = grads
             if not self.grad_ok:
                 self.tst.fail()
             else:
                 if g1:
                     return [g0, g0+g1]
                 else:
                     return [g0, g0]
     i = gof.generic()
     j = gof.generic()
     k = gof.generic()
     a1 = O(self,True).make_node(i,j)
     a2 = O(self,True).make_node(k,a1.outputs[1])
     g = _grad_sources_inputs([(a2.outputs[0], 1), (a1.outputs[1],4),
         (a1.outputs[0], 3), (a1.outputs[0], 3)], None)
     self.assertTrue(g[a2.inputs[0]] == 1)
     self.assertTrue(g[a2.inputs[1]] == 5)
     self.assertTrue(g[a1.outputs[0]] == 6)
     self.assertTrue(g[a1.outputs[1]] == 5)
     self.assertTrue(g[a1.inputs[0]] == 6)
     self.assertTrue(g[a1.inputs[1]] == 11)
Exemple #2
0
def test_ifelse():
    a = T.scalar()
    b = generic()
    c = generic()

    notimpl = NotImplementedOp()
    lazys = [True]
    # We need lazy to end up being True for this test.
    if theano.config.vm.lazy in [True, None]:
        lazys = [True, None]
    cloops = [True, False]
    if theano.config.cxx == "":
        cloops = [False]
    for cloop in cloops:
        for lazy in lazys:
            linker = theano.gof.vm.VM_Linker(use_cloop=cloop, lazy=lazy)
            f = function([a, b, c], ifelse(a, notimpl(b), c),
                         mode=Mode(linker=linker, optimizer='fast_run'))

            try:
                # print "case 1"
                f(1, 'a', 'b')
                assert False
            except NotImplementedOp.E:
                pass
            # print "... passed"

            # print "case 2"
            # print f(0, 'a', 'b')
            assert f(0, 'a', 'b') == 'b'
Exemple #3
0
    def test_some_None_igrads(self):
        """Test that traversal works properly when an op return some None"""
        class O(gof.op.Op):
            def __init__(self, tst, grad_ok):
                self.tst = tst
                self.grad_ok = grad_ok
            def make_node(self, *inputs):
                outputs = [gof.generic(),gof.generic()]
                return gof.Apply(self, inputs, outputs)
            def grad(self, inputs, g_out):
                if not self.grad_ok:
                    self.tst.fail()
                else:
                    return [1, None]
        i = gof.generic()
        j = gof.generic()
        k = gof.generic()
        a1 = O(self, True).make_node(i,j)
        a2 = O(self, True).make_node(a1.outputs[1], k)
        g = grad_sources_inputs([(a2.outputs[0], 1)], None, warn_type=False)
        self.assertTrue(g[i] is 1 and j not in g and k not in g)

        a1 = O(self, True).make_node(i,j)
        a2 = O(self, True).make_node(k, a1.outputs[1])
        g = _grad_sources_inputs([(a2.outputs[0], 1)], None)
        self.assertTrue(g[k] is 1 and i not in g and j not in g)
Exemple #4
0
def test_ifelse():
    a = T.scalar()
    b = generic()
    c = generic()

    notimpl = NotImplementedOp()
    lazys = [True]
    # We need lazy to end up being True for this test.
    if theano.config.vm.lazy in [True, None]:
        lazys = [True, None]
    cloops = [True, False]
    if theano.config.cxx == "":
        cloops = [False]
    for cloop in cloops:
        for lazy in lazys:
            linker = theano.gof.vm.VM_Linker(use_cloop=cloop, lazy=lazy)
            f = function([a, b, c],
                         ifelse(a, notimpl(b), c),
                         mode=Mode(linker=linker, optimizer='fast_run'))

            try:
                # print "case 1"
                f(1, 'a', 'b')
                assert False
            except NotImplementedOp.E:
                pass
            # print "... passed"

            # print "case 2"
            # print f(0, 'a', 'b')
            assert f(0, 'a', 'b') == 'b'
Exemple #5
0
    def test_some_None_igrads(self):
        """Test that traversal works properly when an op return some None"""
        class O(gof.op.Op):
            def __init__(self, tst, grad_ok):
                self.tst = tst
                self.grad_ok = grad_ok
            def make_node(self, *inputs):
                outputs = [gof.generic(),gof.generic()]
                return gof.Apply(self, inputs, outputs)
            def grad(self, inputs, g_out):
                if not self.grad_ok:
                    self.tst.fail()
                else:
                    return [1, None]
        i = gof.generic()
        j = gof.generic()
        k = gof.generic()
        a1 = O(self, True).make_node(i,j)
        a2 = O(self, True).make_node(a1.outputs[1], k)
        g = grad_sources_inputs([(a2.outputs[0], 1)], None, warn_type=False)
        self.assertTrue(g[i] is 1 and j not in g and k not in g)

        a1 = O(self, True).make_node(i,j)
        a2 = O(self, True).make_node(k, a1.outputs[1])
        g = _grad_sources_inputs([(a2.outputs[0], 1)], None)
        self.assertTrue(g[k] is 1 and i not in g and j not in g)
Exemple #6
0
def test_ifelse():
    a = tt.scalar()
    b = generic()
    c = generic()

    notimpl = NotImplementedOp()

    lazys = [True]
    # We need lazy to end up being True for this test.
    if theano.config.vm__lazy in [True, None]:
        lazys = [True, None]

    cloops = [True, False]

    if theano.config.cxx == "":
        cloops = [False]

    for cloop in cloops:
        for lazy in lazys:
            linker = theano.link.vm.VMLinker(use_cloop=cloop, lazy=lazy)
            f = function(
                [a, b, c],
                ifelse(a, notimpl(b), c),
                mode=Mode(linker=linker, optimizer="fast_run"),
            )

            with pytest.raises(NotImplementedOpException):
                f(1, "a", "b")

            assert f(0, "a", "b") == "b"
Exemple #7
0
 def test_multiple_sources(self):
     """Test that passing multiple sources works"""
     class O(gof.op.Op):
         def __init__(self, tst, grad_ok):
             self.tst = tst
             self.grad_ok = grad_ok
         def make_node(self, *inputs):
             outputs = [gof.generic(),gof.generic()]
             return gof.Apply(self, inputs, outputs)
         def grad(self, inputs, grads):
             g0, g1 = grads
             if not self.grad_ok:
                 self.tst.fail()
             else:
                 if g1:
                     return [g0, g0+g1]
                 else:
                     return [g0, g0]
     i = gof.generic()
     j = gof.generic()
     k = gof.generic()
     a1 = O(self,True).make_node(i,j)
     a2 = O(self,True).make_node(k,a1.outputs[1])
     g = _grad_sources_inputs([(a2.outputs[0], 1), (a1.outputs[1],4),
         (a1.outputs[0], 3), (a1.outputs[0], 3)], None)
     self.assertTrue(g[a2.inputs[0]] == 1)
     self.assertTrue(g[a2.inputs[1]] == 5)
     self.assertTrue(g[a1.outputs[0]] == 6)
     self.assertTrue(g[a1.outputs[1]] == 5)
     self.assertTrue(g[a1.inputs[0]] == 6)
     self.assertTrue(g[a1.inputs[1]] == 11)
Exemple #8
0
 def test_Nin_Nout(self):
     """Test grad is called correctly for a many-to-many op"""
     gval0 = gof.generic()
     gval1 = gof.generic()
     class O(gof.op.Op):
         def make_node(self):
             inputs = [gof.generic(),gof.generic()]
             outputs = [gof.generic(),gof.generic()]
             return gof.Apply(self, inputs, outputs)
         def grad(self, inp, grads):
             return gval0, gval1
     a1 = O().make_node()
     g = _grad_sources_inputs([(a1.outputs[0], 1)], None)
     self.assertTrue(g[a1.inputs[0]] is gval0)
     self.assertTrue(g[a1.inputs[1]] is gval1)
Exemple #9
0
 def test_Nin_Nout(self):
     """Test grad is called correctly for a many-to-many op"""
     gval0 = gof.generic()
     gval1 = gof.generic()
     class O(gof.op.Op):
         def make_node(self):
             inputs = [gof.generic(),gof.generic()]
             outputs = [gof.generic(),gof.generic()]
             return gof.Apply(self, inputs, outputs)
         def grad(self, inp, grads):
             return gval0, gval1
     a1 = O().make_node()
     g = _grad_sources_inputs([(a1.outputs[0], 1)], None)
     self.assertTrue(g[a1.inputs[0]] is gval0)
     self.assertTrue(g[a1.inputs[1]] is gval1)
Exemple #10
0
    def test_wrong_rval_len1(self):
        """Test that it is not ok to return the wrong number of gradients"""
        class retNone(gof.op.Op):
            def make_node(self, *inputs):
                outputs = [gof.generic()]
                return gof.Apply(self, inputs, outputs)
            def grad(self, inputs, grads):
                return [None]

        i = gof.generic()
        j = gof.generic()
        a1 = retNone().make_node(i)
        g = _grad_sources_inputs([(a1.out, 1)], None)
        a2 = retNone().make_node(i,j)
        try:
            g = _grad_sources_inputs([(a2.out, 1)], None)
        except ValueError, e:
            self.assertTrue(e[0] is gradient._msg_badlen)
            return
Exemple #11
0
    def test_wrong_rval_len1(self):
        """Test that it is not ok to return the wrong number of gradients"""
        class retNone(gof.op.Op):
            def make_node(self, *inputs):
                outputs = [gof.generic()]
                return gof.Apply(self, inputs, outputs)
            def grad(self, inputs, grads):
                return [None]

        i = gof.generic()
        j = gof.generic()
        a1 = retNone().make_node(i)
        g = _grad_sources_inputs([(a1.out, 1)], None)
        a2 = retNone().make_node(i,j)
        try:
            g = _grad_sources_inputs([(a2.out, 1)], None)
        except ValueError, e:
            self.assertTrue(e[0] is gradient._msg_badlen)
            return
Exemple #12
0
 def test_retNone1_b(self):
     """Test that it is ok to return [None] from op.grad()"""
     class retNone(gof.op.Op):
         def make_node(self, *inputs):
             outputs = [gof.generic()]
             return gof.Apply(self, inputs, outputs)
         def grad(self, inp, grads):
             return [None]
     i = gof.generic()
     a = retNone().make_node(i)
     g = _grad_sources_inputs([(a.out, 1)], None)
     self.assertTrue(not i in g)
Exemple #13
0
def test_ifelse():
    a = T.scalar()
    b = generic()
    c = generic()

    notimpl = NotImplementedOp()

    f = function([a, b, c], ifelse(a, notimpl(b), c), mode=Mode(linker="vm", optimizer="fast_run"))

    try:
        print "case 1"
        f(1, "a", "b")
        assert False
    except NotImplementedOp.E:
        pass
    print "... passed"

    print "case 2"
    print f(0, "a", "b")
    assert f(0, "a", "b") == "b"
    print "... passed"
Exemple #14
0
 def test_retNone1_b(self):
     """Test that it is ok to return [None] from op.grad()"""
     class retNone(gof.op.Op):
         def make_node(self, *inputs):
             outputs = [gof.generic()]
             return gof.Apply(self, inputs, outputs)
         def grad(self, inp, grads):
             return [None]
     i = gof.generic()
     a = retNone().make_node(i)
     g = _grad_sources_inputs([(a.out, 1)], None)
     self.assertTrue(not i in g)
Exemple #15
0
def test_ifelse():
    a = generic()
    b = generic()
    c = generic()

    notimpl = NotImplementedOp()

    f = function([a,b,c], ifelse(a, notimpl(b), c),
            mode=Mode(linker='vm', optimizer='fast_run'))

    try:
        print "case 1"
        f( True, 'a', 'b')
        assert False
    except NotImplementedOp.E:
        pass
    print "... passed"

    print "case 2"
    print f( False, 'a', 'b')
    assert f( False, 'a', 'b') == 'b'
    print "... passed"
Exemple #16
0
def more_complex_test():
    notimpl = NotImplementedOp()
    ifelseifelseif = IfElseIfElseIf()

    x1 = T.scalar('x1')
    x2 = T.scalar('x2')
    c1 = generic('c1')
    c2 = generic('c2')
    t1 = ifelse(c1,x1,notimpl(x2))
    t1.name = 't1'
    t2 = t1*10
    t2.name = 't2'
    t3 = ifelse(c2,t2, x1+t1)
    t3.name = 't3'
    t4 = ifelseifelseif(T.eq(x1,x2), x1, T.eq(x1,5), x2, c2, t3, t3+0.5)
    t4.name = 't4'

    f = function([c1,c2,x1,x2], t4, mode=Mode(linker='vm', optimizer='fast_run'))

    print f(1, 0, numpy.array(10,dtype=x1.dtype),0)
    assert f(1,0,numpy.array(10,dtype=x1.dtype),0) == 20.5
    print '... passed'
Exemple #17
0
 def test_1in_1out(self):
     """Test grad is called correctly for a 1-to-1 op"""
     gval = gof.generic()
     class O(gof.op.Op):
         def make_node(self):
             inputs = [gof.generic()]
             outputs = [gof.generic()]
             return gof.Apply(self, inputs, outputs)
         def grad(self, inp, grads):
             return gval,
     a1 = O().make_node()
     g = _grad_sources_inputs([(a1.outputs[0], 1)], None)
     self.assertTrue(g[a1.inputs[0]] is gval)
Exemple #18
0
def test_ifelse():
    a = T.scalar()
    b = generic()
    c = generic()

    notimpl = NotImplementedOp()

    f = function([a,b,c], ifelse(a, notimpl(b), c),
            mode=Mode(linker='vm', optimizer='fast_run'))

    try:
        print "case 1"
        f( 1, 'a', 'b')
        assert False
    except NotImplementedOp.E:
        pass
    print "... passed"

    print "case 2"
    print f( 0, 'a', 'b')
    assert f( 0, 'a', 'b') == 'b'
    print "... passed"
Exemple #19
0
 def test_1in_1out(self):
     """Test grad is called correctly for a 1-to-1 op"""
     gval = gof.generic()
     class O(gof.op.Op):
         def make_node(self):
             inputs = [gof.generic()]
             outputs = [gof.generic()]
             return gof.Apply(self, inputs, outputs)
         def grad(self, inp, grads):
             return gval,
     a1 = O().make_node()
     g = _grad_sources_inputs([(a1.outputs[0], 1)], None)
     self.assertTrue(g[a1.inputs[0]] is gval)
Exemple #20
0
 def make_node(self, frames, n, axis):
     """ compute an n-point fft of frames along given axis """
     _frames = tensor.as_tensor(frames, ndim=2)
     _n = tensor.as_tensor(n, ndim=0)
     _axis = tensor.as_tensor(axis, ndim=0)
     if self.half and _frames.type.dtype.startswith('complex'):
         raise TypeError('Argument to HalfFFT must not be complex', frames)
     spectrogram = tensor.zmatrix()
     buf = generic()
     # The `buf` output is present for future work
     # when we call FFTW directly and re-use the 'plan' that FFTW creates.
     # In that case, buf would store a CObject encapsulating the plan.
     rval = Apply(self, [_frames, _n, _axis], [spectrogram, buf])
     return rval
Exemple #21
0
 def test_some_None_ograds(self):
     """Test grad is called when some output gradients are None"""
     class O(gof.op.Op):
         def __init__(self, tst):
             self.tst = tst
         def make_node(self, *inputs):
             outputs = [gof.generic(),gof.generic()]
             return gof.Apply(self, inputs, outputs)
         def grad(self, inputs, g_out):
             return [1]
     i = gof.generic()
     a1 = O(self).make_node(i)
     g = grad_sources_inputs([(a1.outputs[0], 1)], None, warn_type=False)
     self.assertTrue(g[i] is 1)
Exemple #22
0
    def test_stop_on_all_none(self):
        """Test that op.grad() is not called when output grads are all None"""
        class retNone(gof.op.Op):
            def __init__(self, tst):
                self.tst = tst
            def make_node(self, *inputs):
                outputs = [gof.generic()]
                return gof.Apply(self, inputs, outputs)
            def grad(self, inputs, grads):
                self.tst.fail()

        i = gof.generic()
        a1 = retNone(self).make_node(i)
        g = _grad_sources_inputs([(a1.out, None)], None)
Exemple #23
0
    def test_stop_on_all_none(self):
        """Test that op.grad() is not called when output grads are all None"""
        class retNone(gof.op.Op):
            def __init__(self, tst):
                self.tst = tst
            def make_node(self, *inputs):
                outputs = [gof.generic()]
                return gof.Apply(self, inputs, outputs)
            def grad(self, inputs, grads):
                self.tst.fail()

        i = gof.generic()
        a1 = retNone(self).make_node(i)
        g = _grad_sources_inputs([(a1.out, None)], None)
Exemple #24
0
 def test_some_None_ograds(self):
     """Test grad is called when some output gradients are None"""
     class O(gof.op.Op):
         def __init__(self, tst):
             self.tst = tst
         def make_node(self, *inputs):
             outputs = [gof.generic(),gof.generic()]
             return gof.Apply(self, inputs, outputs)
         def grad(self, inputs, g_out):
             return [1]
     i = gof.generic()
     a1 = O(self).make_node(i)
     g = grad_sources_inputs([(a1.outputs[0], 1)], None, warn_type=False)
     self.assertTrue(g[i] is 1)
Exemple #25
0
 def make_node(self):
     inputs = [gof.generic()]
     outputs = [gof.generic()]
     return gof.Apply(self, inputs, outputs)
Exemple #26
0
 def make_node(self):
     inputs = [gof.generic()]
     outputs = [gof.generic()]
     return gof.Apply(self, inputs, outputs)