Ejemplo n.º 1
0
    def test_0(self):

        class Opt(opt.Optimizer):  # inheritance buys __hash__
            name = 'blah'

        db = DB()
        db.register('a', Opt())

        db.register('b', Opt())

        db.register('c', Opt(), 'z', 'asdf')

        self.assertTrue('a' in db)
        self.assertTrue('b' in db)
        self.assertTrue('c' in db)

        try:
            db.register('c', Opt())  # name taken
            self.fail()
        except ValueError as e:
            if exc_message(e).startswith("The name"):
                pass
            else:
                raise
        except Exception:
            self.fail()

        try:
            db.register('z', Opt())  # name collides with tag
            self.fail()
        except ValueError as e:
            if exc_message(e).startswith("The name"):
                pass
            else:
                raise
        except Exception:
            self.fail()

        try:
            db.register('u', Opt(), 'b')  # name new but tag collides with name
            self.fail()
        except ValueError as e:
            if exc_message(e).startswith("The tag"):
                pass
            else:
                raise
        except Exception:
            self.fail()
Ejemplo n.º 2
0
    def test_broken_pickle_with_shared(self):
        saves = []
        def pers_save(obj):
            if isinstance(obj, numpy.ndarray):
                saves.append(obj)
                return len(saves)-1
            else:
                return None
        def pers_load(id):
            return saves[id]

        a = numpy.random.rand(4, 5)
        b = numpy.random.rand(5, 4)

        x = theano.tensor.matrix()
        y = theano.shared(b)

        f = theano.function([x], theano.tensor.dot(x, y))

        from theano.compat import BytesIO
        fp = BytesIO()
        p = cPickle.Pickler(fp, 2)
        p.persistent_id = pers_save
        try:
            p.dump(f)
        except NotImplementedError, e:
            if exc_message(e).startswith('DebugMode is not picklable'):
                return
            else:
                raise
Ejemplo n.º 3
0
def test_ambig_data():

    # test that the right error is raised if you
    # add a channel to a monitor that has multiple datasets
    # and don't specify the dataset

    BATCH_SIZE = 2
    num_examples = BATCH_SIZE
    NUM_FEATURES = 3

    model = DummyModel(NUM_FEATURES)
    monitor = Monitor.get_monitor(model)

    first = DummyDataset(num_examples = num_examples,
            num_features = NUM_FEATURES)
    second = DummyDataset(num_examples = num_examples,
            num_features = NUM_FEATURES)

    monitor.add_dataset(first, 'sequential', batch_size=BATCH_SIZE)
    monitor.add_dataset(second, 'sequential', batch_size=BATCH_SIZE)


    name = 'num_prereq_calls'

    try:
        monitor.add_channel(name = name,
            ipt = model.input_space.make_theano_batch(),
            val = 0.,
            data_specs=(model.get_input_space(), model.get_input_source()))
    except ValueError, e:
        assert exc_message(e) == _err_ambig_data
        return
Ejemplo n.º 4
0
def test_ambig_data():

    # test that the right error is raised if you
    # add a channel to a monitor that has multiple datasets
    # and don't specify the dataset

    BATCH_SIZE = 2
    num_examples = BATCH_SIZE
    NUM_FEATURES = 3

    model = DummyModel(NUM_FEATURES)
    monitor = Monitor.get_monitor(model)

    first = DummyDataset(num_examples = num_examples,
            num_features = NUM_FEATURES)
    second = DummyDataset(num_examples = num_examples,
            num_features = NUM_FEATURES)

    monitor.add_dataset(first, 'sequential', batch_size=BATCH_SIZE)
    monitor.add_dataset(second, 'sequential', batch_size=BATCH_SIZE)


    name = 'num_prereq_calls'

    try:
        monitor.add_channel(name = name,
            ipt = model.input_space.make_theano_batch(),
            val = 0.,
            data_specs=(model.get_input_space(), model.get_input_source()))
    except ValueError as e:
        assert exc_message(e) == _err_ambig_data
        return
    assert False
Ejemplo n.º 5
0
def test_badoptimization_opt_err():
    """This variant of test_badoptimization() replace the working code
    with a new apply node that will raise an error.

    """
    @gof.local_optimizer([theano.tensor.add])
    def insert_bigger_b_add(node):
        if node.op == theano.tensor.add:
            inputs = list(node.inputs)
            if inputs[-1].owner is None:
                inputs[-1] = theano.tensor.concatenate(
                    (inputs[-1], inputs[-1]))
                return [node.op(*inputs)]
        return False

    edb = gof.EquilibriumDB()
    edb.register('insert_bigger_b_add', insert_bigger_b_add, 'all')
    opt = edb.query('+all')

    a = theano.tensor.dvector()
    b = theano.tensor.dvector()

    f = theano.function([a, b], a + b, mode=debugmode.DebugMode(optimizer=opt))

    try:
        f(
            [1.0, 2.0, 3.0],
            [2, 3, 4],
        )
    except Exception as e:
        assert 'insert_bigger_b_add' in exc_message(e)
        return  # TEST PASS

    assert False
Ejemplo n.º 6
0
    def test_broken_pickle_with_shared(self):
        saves = []

        def pers_save(obj):
            if isinstance(obj, numpy.ndarray):
                saves.append(obj)
                return len(saves) - 1
            else:
                return None

        def pers_load(id):
            return saves[id]

        a = numpy.random.rand(4, 5)
        b = numpy.random.rand(5, 4)

        x = theano.tensor.matrix()
        y = theano.shared(b)

        f = theano.function([x], theano.tensor.dot(x, y))

        from theano.compat import BytesIO
        fp = BytesIO()
        p = cPickle.Pickler(fp, 2)
        p.persistent_id = pers_save
        try:
            p.dump(f)
        except NotImplementedError, e:
            if exc_message(e).startswith('DebugMode is not picklable'):
                return
            else:
                raise
Ejemplo n.º 7
0
def test_badoptimization_opt_err():
    """This variant of test_badoptimization() replace the working code
    with a new apply node that will raise an error.

    """
    @gof.local_optimizer([theano.tensor.add])
    def insert_bigger_b_add(node):
        if node.op == theano.tensor.add:
            inputs = list(node.inputs)
            if inputs[-1].owner is None:
                inputs[-1] = theano.tensor.concatenate((inputs[-1],
                                                        inputs[-1]))
                return [node.op(*inputs)]
        return False
    edb = gof.EquilibriumDB()
    edb.register('insert_bigger_b_add', insert_bigger_b_add, 'all')
    opt = edb.query('+all')

    a = theano.tensor.dvector()
    b = theano.tensor.dvector()

    f = theano.function([a, b], a + b,
                        mode=debugmode.DebugMode(optimizer=opt))

    try:
        f([1.0, 2.0, 3.0], [2, 3, 4],)
    except Exception, e:
        assert 'insert_bigger_b_add' in exc_message(e)
        return  # TEST PASS
Ejemplo n.º 8
0
    def test_0(self):
        class Opt(opt.Optimizer):  # inheritance buys __hash__
            name = 'blah'

        db = DB()
        db.register('a', Opt())

        db.register('b', Opt())

        db.register('c', Opt(), 'z', 'asdf')

        self.assertTrue('a' in db)
        self.assertTrue('b' in db)
        self.assertTrue('c' in db)

        try:
            db.register('c', Opt())  # name taken
            self.fail()
        except ValueError as e:
            if exc_message(e).startswith("The name"):
                pass
            else:
                raise
        except Exception:
            self.fail()

        try:
            db.register('z', Opt())  # name collides with tag
            self.fail()
        except ValueError as e:
            if exc_message(e).startswith("The name"):
                pass
            else:
                raise
        except Exception:
            self.fail()

        try:
            db.register('u', Opt(), 'b')  # name new but tag collides with name
            self.fail()
        except ValueError as e:
            if exc_message(e).startswith("The tag"):
                pass
            else:
                raise
        except Exception:
            self.fail()
Ejemplo n.º 9
0
    def test_mixed_list(self):
        M = Module()
        M.a = [1,2,T.lscalar()]
        m = M.make()
        assert list(m.a) == [1,2,None]

        assert m.a is not M.a
        try:
            m.a[0] = 4
            assert False
        except Exception, e:
            if exc_message(e).startswith("Cannot set readonly"):
                pass
            else:
                raise
Ejemplo n.º 10
0
 def test_wrappable_as_tensor(self):
     M = Module()
     M.a = [1,2,3]
     M.make()
     m = M.make()
     #print m.a
     #print m.a[0], type(m.a[0]), m.a[0] == 1
     #print list(m.a)
     assert list(m.a) == [1,2,3]
     assert m.a is not M.a
     try:
         m.a = [4, 5, 6]
         assert False
     except Exception, e:
         if exc_message(e).startswith("Cannot set readonly"):
             pass
         else:
             raise
    def test_0(self):
        class Opt(opt.Optimizer):  # inheritance buys __hash__
            name = 'blah'

        db = DB()
        db.register('a', Opt())

        db.register('b', Opt())

        db.register('c', Opt(), 'z', 'asdf')

        try:
            db.register('c', Opt())  # name taken
            self.fail()
        except ValueError, e:
            if exc_message(e).startswith("The name"):
                pass
            else:
                raise
Ejemplo n.º 12
0
    def test_0(self):

        class Opt(opt.Optimizer):  # inheritance buys __hash__
            name = 'blah'

        db = DB()
        db.register('a', Opt())

        db.register('b', Opt())

        db.register('c', Opt(), 'z', 'asdf')

        try:
            db.register('c', Opt())  # name taken
            self.fail()
        except ValueError, e:
            if exc_message(e).startswith("The name"):
                pass
            else:
                raise
Ejemplo n.º 13
0
def test_no_data():

    # test that the right error is raised if you
    # add a channel to a monitor that has no datasets

    BATCH_SIZE = 2
    num_examples = BATCH_SIZE
    NUM_FEATURES = 3

    model = DummyModel(NUM_FEATURES)
    monitor = Monitor.get_monitor(model)

    name = "num_prereq_calls"

    try:
        monitor.add_channel(
            name=name, ipt=model.input_space.make_theano_batch(), data_specs=(model.input_space, "features"), val=0.0
        )
    except ValueError, e:
        assert exc_message(e) == _err_no_data
        return
def test_method_updates():

    # updates work
    M = Module()
    M.x = T.dvector()
    x = T.dvector()
    xval = numpy.asarray([0, 0.5])
    M.f = Method([x], M.x * 4, updates={M.x: M.x * 2}, mode='FAST_COMPILE')
    m = M.make(mode='FAST_RUN')
    m.x = xval
    m.f([9, 9])
    assert numpy.all(m.x == [0, 1])
    assert numpy.all(xval == [0, 0.5])

    # In(update) works
    M = Module()
    M.x = T.dvector()
    x = T.dvector()
    M.f = Method([x, io.In(M.x, value=xval, update=M.x * 2)], M.x * 4)
    m = M.make()
    m.f([9, 9])
    assert m.x is None
    assert numpy.all(m.f[M.x] == [0, 1])

    # when a variable is listed explicitly and in an update, then there's a problem.
    M = Module()
    M.x = T.dvector()
    x = T.dvector()
    M.f = Method([x, io.In(M.x, value=xval, update=M.x * 2)],
                 M.x * 4,
                 updates={M.x: M.x * 7})
    try:
        m = M.make()
        assert False
    except ValueError, e:
        if str(exc_message(e)).startswith(
                'Variable listed in both inputs and up'):
            pass
        else:
            raise
Ejemplo n.º 15
0
def test_method_updates():

    # updates work
    M = Module()
    M.x = T.dvector()
    x = T.dvector()
    xval= numpy.asarray([0, 0.5])
    M.f = Method([x], M.x*4, updates={M.x:M.x * 2}, mode='FAST_COMPILE')
    m = M.make(mode='FAST_RUN')
    m.x = xval
    m.f([9,9])
    assert numpy.all(m.x == [0, 1])
    assert numpy.all(xval == [0, 0.5])

    # In(update) works
    M = Module()
    M.x = T.dvector()
    x = T.dvector()
    M.f = Method([x, io.In(M.x, value=xval, update=M.x*2)], M.x*4)
    m = M.make()
    m.f([9,9])
    assert m.x is None
    assert numpy.all(m.f[M.x] == [0, 1])


    # when a variable is listed explicitly and in an update, then there's a problem.
    M = Module()
    M.x = T.dvector()
    x = T.dvector()
    M.f = Method([x, io.In(M.x, value=xval, update=M.x*2)], M.x*4,
            updates={M.x:M.x * 7})
    try:
        m = M.make()
        assert False
    except ValueError, e:
        if str(exc_message(e)).startswith('Variable listed in both inputs and up'):
            pass
        else:
            raise
Ejemplo n.º 16
0
def test_no_data():

    # test that the right error is raised if you
    # add a channel to a monitor that has no datasets

    BATCH_SIZE = 2
    num_examples = BATCH_SIZE
    NUM_FEATURES = 3

    model = DummyModel(NUM_FEATURES)
    monitor = Monitor.get_monitor(model)

    name = 'num_prereq_calls'

    try:
        monitor.add_channel(name = name,
            ipt = model.input_space.make_theano_batch(),
            data_specs = (model.input_space, 'features'),
            val = 0.)
    except ValueError, e:
        assert exc_message(e) == _err_no_data
        return
Ejemplo n.º 17
0
    def test_broken_pickle_with_shared(self):
        saves = []

        def pers_save(obj):
            if isinstance(obj, np.ndarray):
                saves.append(obj)
                return len(saves) - 1
            else:
                return None

        def pers_load(id):
            return saves[id]

        b = np.random.rand(5, 4)

        x = T.matrix()
        y = theano.shared(b)

        f = theano.function([x], T.dot(x, y))

        from theano.compat import BytesIO

        fp = BytesIO()
        p = pickle.Pickler(fp, 2)
        p.persistent_id = pers_save
        try:
            p.dump(f)
        except NotImplementedError as e:
            if exc_message(e).startswith("DebugMode is not picklable"):
                return
            else:
                raise
        fp2 = BytesIO(fp.getvalue())
        fp.close()
        p = pickle.Unpickler(fp2)
        p.persistent_load = pers_load
        p.load()
        fp2.close()
Ejemplo n.º 18
0
        try:
            db.register('c', Opt())  # name taken
            self.fail()
        except ValueError, e:
            if exc_message(e).startswith("The name"):
                pass
            else:
                raise
        except Exception:
            self.fail()

        try:
            db.register('z', Opt())  # name collides with tag
            self.fail()
        except ValueError, e:
            if exc_message(e).startswith("The name"):
                pass
            else:
                raise
        except Exception:
            self.fail()

        try:
            db.register('u', Opt(), 'b')  # name new but tag collides with name
            self.fail()
        except ValueError, e:
            if exc_message(e).startswith("The tag"):
                pass
            else:
                raise
        except Exception:
Ejemplo n.º 19
0
class T_module(unittest.TestCase):

    def test_empty_module(self):
        m = Module()
        m.make()

    def test_whats_up_with_submembers(self):
        class Blah(Module):
            def __init__(self, stepsize):
                super(Blah, self).__init__()
                self.stepsize = T.constant(stepsize)
                x = T.dscalar()

                self.step = Method([x], x - self.stepsize)

        B = Blah(0.0)
        b = B.make(mode='FAST_RUN')
        assert b.stepsize == 0.0
        b.step(1.0)
        assert b.stepsize == 0.0

    def test_members_in_list_tuple_or_dict(self):
        """Test that a Member which is only included via a list, tuple or dictionary is still treated as if it
        were a toplevel attribute and not shared
        """

        def local_test(x,y):
            m1=Module()
            m1.x=x()
            m1.y=y()
            m1.emtpylist = []
            m1.lx=[x()]#cast Variable]
            m1.ly=[y()]
            m1.llx=[[x()]]#cast Variable]
            m1.lly=[[y()]]
            m1.ltx=[(x(),)]
            m1.lty=[(y(),)]
            m1.ldx=[{"x":x()}]
            m1.ldy=[{"y":y()}]
            m1.tx=(x(),)
            m1.ty=(y(),)
            m1.tlx=[(x(),)]
            m1.tly=[(y(),)]
            m1.ttx=((x(),),)
            m1.tty=((y(),),)
            m1.tdx=({"x":x()},)
            m1.tdy=({"y":y()},)
            m1.dx={"x":x()}
            m1.dy={"y":y()}
            m1.dlx={"x":[x()]}
            m1.dly={"y":[y()]}
            m1.dtx={"x":(x(),)}
            m1.dty={"y":(y(),)}
            m1.ddx={"x":{"x":x()}}
            m1.ddy={"y":{"y":y()}}

            assert isinstance(m1.x,(gof.Variable))
            assert isinstance(m1.y,(gof.Variable))
            for i, obj in enumerate([
                    m1.lx[0], #0
                    m1.llx[0][0],
                    m1.ltx[0][0],
                    m1.ldx[0]['x'],
                    m1.lty[0][0],#5
                    m1.ldy[0]['y'],
                    m1.ly[0],
                    m1.lly[0][0],
                    m1.tx[0], #8
                    m1.ty[0], m1.tlx[0][0],
                    m1.tly[0][0], m1.ttx[0][0], m1.tty[0][0], m1.tdx[0]['x'],
                    m1.tdy[0]['y'], m1.dx['x'],
                    m1.dy['y'], m1.dlx['x'][0], m1.dly['y'][0],
                    m1.dtx['x'][0], m1.dty['y'][0], m1.ddx['x']['x'],
                    m1.ddy['y']['y']]):
                assert isinstance(obj,(gof.Variable))


            inst=m1.make()

            def get_l():
                return [inst.lx, inst.ly, inst.tx, inst.ty, inst.dx, inst.dy, inst.llx, inst.lly, inst.ltx, inst.lty, inst.ldx, inst.ldy, inst.tlx, inst.tly, inst.ttx, inst.tty, inst.tdx, inst.tdy, inst.dly, inst.dlx, inst.dty, inst.dtx, inst.ddy, inst.ddx]
            def get_l2():
#                return [inst.lx[0], inst.ly[0], inst.tx[0], inst.ty[0], inst.dx['x'], inst.dy['y'], inst.llx[0][0], inst.lly[0][0], inst.ltx[0][0], inst.lty[0][0], inst.ldx[0]['x'], inst.ldy[0]['y'], inst.tlx[0][0], inst.tly[0][0], inst.ttx[0][0], inst.tty[0][0], inst.tdx, inst.tdy, inst.dly, inst.dlx, inst.dty, inst.dtx, inst.ddy, inst.ddx]
                return [inst.lx, inst.ly, inst.tx, inst.ty, inst.llx[0], inst.lly[0], inst.ltx[0], inst.lty[0], inst.ldx[0], inst.ldy[0], inst.tlx[0], inst.tly[0], inst.ttx[0], inst.tty[0], inst.tdx[0], inst.tdy[0], inst.dly['y'], inst.dlx['x'], inst.dty['y'], inst.dtx['x']]#, inst.ddy['y'], inst.ddx['x']]


            #test that we can access the data
            inst.x
            inst.y
            for i in get_l():
                assert i

            #test that we can set a value to the data the get this value
            if not isinstance(m1.x, gof.Constant):
                inst.x=-1
                inst.y=-2
                inst.ldx[0]['x']=-3
                inst.ldy[0]['y']=-4
                inst.tdx[0]['x']=-5
                inst.tdy[0]['y']=-6
                inst.ddx['x']['x']=-7
                inst.ddy['y']['y']=-8
                for i,j in zip(get_l2(),range(len(get_l2()))):
                    i[0]=j
                assert inst.x==-1
                assert inst.y==-2
                assert inst.ldx[0]['x']==-3
                assert inst.ldy[0]['y']==-4
                assert inst.tdx[0]['x']==-5
                assert inst.tdy[0]['y']==-6
                assert inst.ddx['x']['x']==-7
                assert inst.ddy['y']['y']==-8
                for i,j in zip(get_l2(),range(len(get_l2()))):
                    assert i[0]==j

        local_test(lambda:T.dscalar(),lambda:T.dscalar())
        local_test(lambda:T.constant(1),lambda:T.constant(2))
        local_test(lambda:T.constant(1),lambda:T.constant(2))

    def test_list_assign(self):
        """Test that list members can be assigned list-wise"""
        def local_test(x,y):
            m1=Module()

            #create a list with some variables in it
            m1.l=[x(), y()]

            # create a Method that makes the second list element a shared Member
            m1.f=Method([], m1.l[1])
            m1.g=Method([], m1.l[0])
            m = m1.make()

            #assign 4 and 5 to the two variables' containers in m
            m.l = [4, 5]
            m.f()
            assert numpy.all(5 == m.f())
            assert numpy.all(4 == m.g())

        local_test(lambda:T.dscalar(),lambda:T.dscalar())

    def test_tuple_assign(self):
        """Test that list members can be assigned tuple-wise"""
        def local_test(x,y):
            m1=Module()
            m1.l=(x(), y())

            # create a Method that makes the second list element a shared Member
            m1.g=Method([], m1.l[0])
            m1.f=Method([], m1.l[1])
            m = m1.make()

            #assign 4 and 5 to the two variables' containers in m
            m.l = (4, 5)
            assert 5 == m.f()
            assert 4 == m.g()

        local_test(lambda:T.dscalar(),lambda:T.dscalar())

    def test_dict_assign(self):
        """Test that list members can be assigned dict-wise"""
        def local_test(x,y):
            m1=Module()
            ##DICT
            m1.l={'x':x(), 'y':y()}

            # create a Method that makes the second list element a shared Member
            m1.f=Method([], m1.l['y'])
            m1.g=Method([], m1.l['x'])
            m = m1.make()

            #assign 4 and 5 to the two variables' containers in m
            m.l = dict(x=4, y=5)
            assert 5 == m.f()
            assert 4 == m.g()

        #print 'dscalar test'
        local_test(lambda:T.dscalar(),lambda:T.dscalar())


    def test_method_in_list_or_dict(self):
        """Test that a Method which is only included via a list or dictionary is still treated as if it
        were a toplevel attribute
        Fred: why we don't do this of direct fct of variables?
        """
        m1=Module()
        x=T.dscalar()
        m1.x=T.dscalar()
        m1.y=Method(x,x*2)
        m1.z=Method([],m1.x*2)
        m1.ly=[Method(x,x*2)]
        m1.lz=[Method([],m1.x*2)]
        m1.ty=(Method(x,x*2),)
        m1.tz=(Method([],m1.x*2),)
        m1.dy={'y':Method(x,x*2)}
        m1.dz={'z':Method([],m1.x*2)}
        m1.lly=[[Method(x,x*2)]]
        m1.llz=[[Method([],m1.x*2)]]
        m1.lty=[(Method(x,x*2),)]
        m1.ltz=[(Method([],m1.x*2),)]
        m1.ldy=[{'y':Method(x,x*2)}]
        m1.ldz=[{'z':Method([],m1.x*2)}]
        m1.tly=([Method(x,x*2)],)
        m1.tlz=([Method([],m1.x*2)],)
        m1.tty=((Method(x,x*2),),)
        m1.ttz=((Method([],m1.x*2),),)
        m1.tdy=({'y':Method(x,x*2)},)
        m1.tdz=({'z':Method([],m1.x*2)},)
        m1.dly={'y':[Method(x,x*2)]}
        m1.dlz={'z':[Method([],m1.x*2)]}
        m1.dty={'y':(Method(x,x*2),)}
        m1.dtz={'z':(Method([],m1.x*2),)}
        m1.ddy={'y':{'y':Method(x,x*2)}}
        m1.ddz={'z':{'z':Method([],m1.x*2)}}

        inst=m1.make()
        inst.x=1
        assert inst.y(2)==4
        assert inst.z()==2
        assert inst.ly[0](2)==4
        assert inst.lz[0]()==2
        assert inst.ty[0](2)==4
        assert inst.tz[0]()==2
        assert inst.dy['y'](2)==4
        assert inst.dz['z']()==2
        for f in inst.lly[0][0], inst.lty[0][0], inst.ldy[0]['y'], inst.tly[0][0], inst.tty[0][0], inst.tdy[0]['y'], inst.dly['y'][0], inst.dty['y'][0], inst.ddy['y']['y']:
            assert f(2)==4
        for f in inst.llz[0][0], inst.ltz[0][0], inst.ldz[0]['z'], inst.tlz[0][0], inst.ttz[0][0], inst.tdz[0]['z'], inst.dlz['z'][0], inst.dtz['z'][0], inst.ddz['z']['z']:
            assert f()==2

        assert isinstance(inst.z,theano.compile.function_module.Function)
        assert isinstance(inst.y,theano.compile.function_module.Function)
        for f in inst.ly,inst.lz,inst.ty,inst.tz:
            assert isinstance(f[0],theano.compile.function_module.Function)
        for f in inst.lly,inst.llz,inst.lty,inst.ltz,inst.tly,inst.tlz,inst.tty,inst.ttz:
            assert isinstance(f[0][0],theano.compile.function_module.Function)
        for f in inst.dly['y'][0],inst.dty['y'][0], inst.dlz['z'][0],inst.dtz['z'][0], inst.ddy['y']['y'], inst.ddz['z']['z']:
            assert isinstance(f,theano.compile.function_module.Function)

    def test_shared_members(self):
        """Test that under a variety of tricky conditions, the shared-ness of Variables and Members
        is respected."""

        def populate_module(m,x):
            m.x=x
            m.lx=[x]
            m.llx=[[x],[x]]
            m.ltx=[(x,)]
            m.ldx=[{'x':x}]
            m.tx=(x,)
            m.tlx=([x],)
            m.ttx=((x,),)
            m.tdx=({'x':x},)
            m.dx={'x':x}
            m.dlx={'x':[x]}
            m.dtx={'x':(x,)}
            m.ddx={'x':{'x':x}}

        def get_element(i):
            return [i.x,i.lx[0],i.tx[0],i.dx['x'],i.llx[0][0], i.llx[1][0], i.ltx[0][0], i.ldx[0]['x'], i.tlx[0][0], i.tlx[0][0], i.tdx[0]['x'], i.dlx['x'][0], i.dtx['x'][0], i.ddx['x']['x']]

        m1=Module()
        m2=Module()
        x=T.dscalar()
        populate_module(m1,x)
        populate_module(m2,x)
        #m1.x and m2.x should not be shared as their is no hierarchi link between them.
        inst1=m1.make()
        inst2=m2.make()
        m1.m2=m2
        #m1.x and m2.x should be shared as their is a hierarchi link between them.
        inst3=m1.make()
        inst1.x=1
        inst2.x=2
        inst3.x=3
        for f in get_element(inst1):
            assert f==1
        for f in get_element(inst2):
            assert f==2
        for f in get_element(inst3)+get_element(inst3.m2):
            assert f==3

        inst3.m2.x=4
        for f in get_element(inst3)+get_element(inst3.m2):
            assert f==4

    def test_shared_members_N(self):
        """Test that Members can be shared an arbitrary number of times between
        many submodules and internal data structures."""
        def populate_module(m,x):
            m.x=x
            m.lx=[x]
            m.llx=[[x],[x]]
            m.ltx=[(x,)]
            m.ldx=[{'x':x}]
            m.tx=(x,)
            m.tlx=([x],)
            m.ttx=((x,),)
            m.tdx=({'x':x},)
            m.dx={'x':x}
            m.dlx={'x':[x]}
            m.dtx={'x':(x,)}
            m.ddx={'x':{'x':x}}

        def get_element(i):
            return [i.x,i.lx[0],i.tx[0],i.dx['x'],i.llx[0][0], i.llx[1][0], i.ltx[0][0], i.ldx[0]['x'], i.tlx[0][0], i.tlx[0][0], i.tdx[0]['x'], i.dlx['x'][0], i.dtx['x'][0], i.ddx['x']['x']]
        m1=Module()
        m2=Module()
        m3=Module()
        m4=Module()
        x=T.dscalar()
        populate_module(m1,x)
        populate_module(m2,(x))
        populate_module(m4,(x))
        #m1.x and m2.x should not be shared as their is no hierarchi link between them.
        inst1=m1.make()
        inst2=m2.make()
        m1.m2=m2
        m2.m3=m3
        m3.m4=m4
        #m1.x and m2.x should be shared as their is a hierarchi link between them.
        inst3=m1.make()
        inst1.x=1
        inst2.x=2
        inst3.x=3
        for f in get_element(inst1):
            assert f==1
        for f in get_element(inst2):
            assert f==2
        for f in get_element(inst3)+get_element(inst3.m2)+get_element(inst3.m2.m3.m4):
            assert f==3

        inst3.m2.x=4
        for f in get_element(inst3)+get_element(inst3.m2)+get_element(inst3.m2.m3.m4):
            assert f==4

    def test_shared_method(self):
        """Test that under a variety of tricky conditions, the shared-ness of Variables and Methods
        is respected.
        Fred: the test create different method event if they are shared. What do we want?
        """

        m1=Module()
        m1.x=T.dscalar()
        x=T.dscalar()
        fy=Method(x,x*2)
        fz=Method([],m1.x*2)
        m1.y=fy
        m1.z=fz
        m1.ly=[fy]
        m1.lz=[fz]
        m1.lly=[[fy]]
        m1.llz=[[fz]]
        m1.ty=(fy,)
        m1.tz=(fz,)
        m1.tty=((fy,),)
        m1.ttz=((fz,),)
        m1.dy={'y':fy}
        m1.dz={'z':fz}

        inst=m1.make()
        inst.x=1
        assert inst.y(2)==4
        assert inst.z()==2
        assert inst.ly[0](2)==4
        assert inst.lz[0]()==2
        assert inst.ty[0](2)==4
        assert inst.tz[0]()==2
        assert inst.dy['y'](2)==4
        assert inst.dz['z']()==2
        assert inst.lly[0][0](2)==4
        assert inst.llz[0][0]()==2
        assert inst.tty[0][0](2)==4
        assert inst.ttz[0][0]()==2
        assert isinstance(inst.z,theano.compile.function_module.Function)
        assert isinstance(inst.lz[0],theano.compile.function_module.Function)
        assert isinstance(inst.llz[0][0],theano.compile.function_module.Function)
        assert isinstance(inst.tz[0],theano.compile.function_module.Function)
        assert isinstance(inst.dz['z'],theano.compile.function_module.Function)
        assert isinstance(inst.ttz[0][0],theano.compile.function_module.Function)
        assert isinstance(inst.y,theano.compile.function_module.Function)
        assert isinstance(inst.ly[0],theano.compile.function_module.Function)
        assert isinstance(inst.lly[0][0],theano.compile.function_module.Function)
        assert isinstance(inst.ty[0],theano.compile.function_module.Function)
        assert isinstance(inst.dy['y'],theano.compile.function_module.Function)
        assert isinstance(inst.tty[0][0],theano.compile.function_module.Function)


        assert m1.y is m1.ly[0]
        assert inst.y is inst.ly[0]
        assert inst.y is inst.lly[0][0]
        assert inst.y is inst.ty[0]
        assert inst.y is inst.tty[0][0]
        assert inst.y is inst.dy['y']

    def test_member_method_inputs(self):
        """Test that module Members can be named as Method inputs, in which case the function will
        *not* use the storage allocated for the Module's version of that Member.

        """

        # test that explicit Method inputs don't use shared storage
        M = Module()
        M.x = T.dscalar()
        M.y = T.dscalar()
        M.f = Method([M.x], M.x + M.y)
        M.g = Method([M.y], M.x - M.y)
        m = M.make()
        m.y = 77
        assert m.f(23) == 100
        assert m.x is None
        m.x = 1000
        assert m.g(23) == 977
        assert m.y == 77
        assert m.x == 1000

    def test_member_input_flags(self):
        """Test that we can manipulate the mutable, strict, etc. flags (see SymbolicInput) of
        Method inputs"""

        if config.mode == 'FAST_COMPILE':
            return

        M = Module()
        M.x = T.dvector()
        M.y = T.dvector()
        xval= numpy.asarray([0, 0.5])
        M.f = Method([io.In(M.x,
            mutable=True,
            update=(M.x - M.y),
            value=xval)], M.x + M.y)
        m = M.make()
        m.y = numpy.asarray([1, 2])

        assert numpy.all(m.f(xval) == [1, 2.5])
        assert numpy.all(xval == [-1, -1.5])

    def test_member_constant(self):
        """Test that module Members of Constant work correctly.
        As Variable with more optimization?"""
        M = Module()
        x = T.dscalar()
        M.y = T.constant(40)
        M.f = Method([x], x + 2 * M.y)
        m = M.make()
        try:
            m.y = 77 #fail?
        except Exception:
            pass
        assert m.y == 40
        assert m.f(20) == 100

    def test_raise_NotImplemented(self):
        c=Component()
        self.assertRaises(NotImplementedError, c.allocate,"")
        self.assertRaises(NotImplementedError, c.build,"","")
        self.assertRaises(NotImplementedError, c.pretty)
        c=Composite()
        self.assertRaises(NotImplementedError, c.components)
        self.assertRaises(NotImplementedError, c.components_map)
        self.assertRaises(NotImplementedError, c.get,"n")
        self.assertRaises(NotImplementedError, c.set,"n",1)


    def test_wrappable_as_tensor(self):
        M = Module()
        M.a = [1,2,3]
        M.make()
        m = M.make()
        #print m.a
        #print m.a[0], type(m.a[0]), m.a[0] == 1
        #print list(m.a)
        assert list(m.a) == [1,2,3]
        assert m.a is not M.a
        try:
            m.a = [4, 5, 6]
            assert False
        except Exception, e:
            if exc_message(e).startswith("Cannot set readonly"):
                pass
            else:
                raise

        try:
            m.a[0] = 4
            assert False
        except Exception, e:
            if exc_message(e).startswith("Cannot set readonly"):
                pass
            else:
                raise
Ejemplo n.º 20
0
def test_badoptimization_opt_err():
    # This variant of test_badoptimization() replace the working code
    # with a new apply node that will raise an error.
    @gof.local_optimizer([theano.tensor.add])
    def insert_bigger_b_add(node):
        if node.op == theano.tensor.add:
            inputs = list(node.inputs)
            if inputs[-1].owner is None:
                inputs[-1] = theano.tensor.concatenate(
                    (inputs[-1], inputs[-1]))
                return [node.op(*inputs)]
        return False

    @gof.local_optimizer([theano.tensor.add])
    def insert_bad_dtype(node):
        if node.op == theano.tensor.add:
            inputs = list(node.inputs)
            if inputs[-1].owner is None:

                return [node.outputs[0].astype('float32')]
        return False

    edb = gof.EquilibriumDB()
    edb.register('insert_bigger_b_add', insert_bigger_b_add, 'all')
    opt = edb.query('+all')
    edb2 = gof.EquilibriumDB()
    edb2.register('insert_bad_dtype', insert_bad_dtype, 'all')
    opt2 = edb2.query('+all')

    a = theano.tensor.dvector()
    b = theano.tensor.dvector()

    f = theano.function([a, b], a + b, mode=debugmode.DebugMode(optimizer=opt))
    try:
        f(
            [1.0, 2.0, 3.0],
            [2, 3, 4],
        )
    except ValueError as e:
        assert 'insert_bigger_b_add' in exc_message(e)
    else:
        assert False

    # Test that opt that do an illegal change still get the error from gof.
    try:
        with theano.change_flags(on_opt_error='raise'):
            f2 = theano.function([a, b],
                                 a + b,
                                 mode=debugmode.DebugMode(
                                     optimizer=opt2, stability_patience=1))
        f2(
            [1.0, 2.0, 3.0],
            [2, 3, 4],
        )
    except theano.gof.toolbox.BadOptimization as e:
        assert 'insert_bad_dtype' in str(e)
        # Test that we can reraise the error with an extended message
        try:
            new_e = e.__class__("TTT" + str(e))
            exc_type, exc_value, exc_trace = sys.exc_info()
            exc_value = new_e
            reraise(e.__class__, exc_value, exc_trace)
        except theano.gof.toolbox.BadOptimization as e:
            pass
        else:
            assert False
    else:
        assert False
Ejemplo n.º 21
0
def test_badoptimization_opt_err():
    # This variant of test_badoptimization() replace the working code
    # with a new apply node that will raise an error.
    @gof.local_optimizer([theano.tensor.add])
    def insert_bigger_b_add(node):
        if node.op == theano.tensor.add:
            inputs = list(node.inputs)
            if inputs[-1].owner is None:
                inputs[-1] = theano.tensor.concatenate((inputs[-1],
                                                        inputs[-1]))
                return [node.op(*inputs)]
        return False

    @gof.local_optimizer([theano.tensor.add])
    def insert_bad_dtype(node):
        if node.op == theano.tensor.add:
            inputs = list(node.inputs)
            if inputs[-1].owner is None:

                return [node.outputs[0].astype('float32')]
        return False
    edb = gof.EquilibriumDB()
    edb.register('insert_bigger_b_add', insert_bigger_b_add, 'all')
    opt = edb.query('+all')
    edb2 = gof.EquilibriumDB()
    edb2.register('insert_bad_dtype', insert_bad_dtype, 'all')
    opt2 = edb2.query('+all')

    a = theano.tensor.dvector()
    b = theano.tensor.dvector()

    f = theano.function([a, b], a + b,
                        mode=debugmode.DebugMode(optimizer=opt))
    try:
        f([1.0, 2.0, 3.0], [2, 3, 4],)
    except ValueError as e:
        assert 'insert_bigger_b_add' in exc_message(e)
    else:
        assert False

    # Test that opt that do an illegal change still get the error from gof.
    try:
        with theano.change_flags(on_opt_error='raise'):
            f2 = theano.function([a, b], a + b,
                                 mode=debugmode.DebugMode(optimizer=opt2,
                                                          stability_patience=1))
        f2([1.0, 2.0, 3.0], [2, 3, 4],)
    except theano.gof.toolbox.BadOptimization as e:
        assert 'insert_bad_dtype' in str(e)
        # Test that we can reraise the error with an extended message
        try:
            new_e = e.__class__("TTT" + str(e))
            exc_type, exc_value, exc_trace = sys.exc_info()
            exc_value = new_e
            reraise(e.__class__, exc_value, exc_trace)
        except theano.gof.toolbox.BadOptimization as e:
            pass
        else:
            assert False
    else:
        assert False
        try:
            db.register('c', Opt())  # name taken
            self.fail()
        except ValueError, e:
            if exc_message(e).startswith("The name"):
                pass
            else:
                raise
        except Exception:
            self.fail()

        try:
            db.register('z', Opt())  # name collides with tag
            self.fail()
        except ValueError, e:
            if exc_message(e).startswith("The name"):
                pass
            else:
                raise
        except Exception:
            self.fail()

        try:
            db.register('u', Opt(), 'b')  # name new but tag collides with name
            self.fail()
        except ValueError, e:
            if exc_message(e).startswith("The tag"):
                pass
            else:
                raise
        except Exception: