예제 #1
0
    def testRecurrences(self):
        import Mariana.recurrence as MREC
        import Mariana.reshaping as MRES

        ls = MS.GradientDescent(lr=0.1)
        cost = MC.NegativeLogLikelihood()

        # for clas in [MREC.RecurrentDense, MREC.LSTM, MREC.GRU] :
        for clas in [MREC.RecurrentDense]:
            inp = ML.Input((None, 3), 'inp')
            r = clas(2, onlyReturnFinal=True, name="rec")
            reshape = MRES.Reshape((-1, 2), name="reshape")
            o = ML.SoftmaxClassifier(
                2,
                cost=cost,
                learningScenari=[MS.GradientDescent(lr=0.5)],
                name="out")
            net = inp > r > reshape > o
            net.init()
            inputs = [[[1, 1], [1, 0], [1, 1]], [[1, 0], [0, 1], [1, 0]]]

            oldWih = r.getP("W_in_to_hid").getValue()
            oldWhh = r.getP("W_hid_to_hid").getValue()
            for x in xrange(1, 100):
                net["out"].train({
                    "inp.inputs": inputs,
                    "out.targets": [1, 1, 1]
                })["out.drive.train"]
            self.assertTrue(
                oldWih.mean() != r.getP("W_in_to_hid").getValue().mean())
            self.assertTrue(
                oldWhh.mean() != r.getP("W_hid_to_hid").getValue().mean())
예제 #2
0
파일: tests.py 프로젝트: Solertis/Mariana
    def test_save_load_pickle(self) :
        import os
        import Mariana.network as MN

        ls = MS.GradientDescent(lr = 0.1)
        cost = MC.NegativeLogLikelihood()

        i = ML.Input(2, 'inp')
        h = Hidden_layerRef(i, 10, activation = MA.ReLU(), name = "Hidden_0.500705866892")
        o = ML.SoftmaxClassifier(2, learningScenario = ls, costObject = cost, name = "out")

        mlp = i > h > o
        
        self.xor_ins = numpy.array(self.xor_ins)
        self.xor_outs = numpy.array(self.xor_outs)
        for i in xrange(1000) :
            mlp.train(o, inp = self.xor_ins, targets = self.xor_outs )

        mlp.save("test_save")
        mlp2 = MN.loadModel("test_save.mar.mdl.pkl")
        
        o = mlp.outputs.values()[0]
        
        v1 = mlp.propagate( o.name, inp = self.xor_ins )["outputs"]
        v2 = mlp2.propagate( o.name, inp = self.xor_ins )["outputs"]
        self.assertEqual(numpy.sum(v1), numpy.sum(v2))
        self.assertEqual(mlp["Hidden_0.500705866892"].otherLayer.name, mlp2["Hidden_0.500705866892"].otherLayer.name)
        
        os.remove('test_save.mar.mdl.pkl')
예제 #3
0
    def test_concatenation(self):
        ls = MS.GradientDescent(lr=0.1)
        cost = MC.NegativeLogLikelihood()

        inp = ML.Input(2, 'inp')
        h1 = ML.Hidden(5, activation=MA.Tanh(), name="h1")
        h2 = ML.Hidden(5, activation=MA.Tanh(), name="h2")
        o = ML.SoftmaxClassifier(nbClasses=2,
                                 cost=cost,
                                 learningScenari=[ls],
                                 name="out")

        inp > h1
        inp > h2
        c = ML.C([h1, h2], name="concat")
        mlp = c > o
        mlp.init()

        self.assertEqual(c.getIntrinsicShape()[0],
                         h1.getIntrinsicShape()[0] + h2.getIntrinsicShape()[0])
        for i in xrange(10000):
            ii = i % len(self.xor_ins)
            miniBatch = [self.xor_ins[ii]]
            targets = [self.xor_outs[ii]]
            mlp["out"].train({
                "inp.inputs": miniBatch,
                "out.targets": targets
            })["out.drive.train"]

        for i in xrange(len(self.xor_ins)):
            self.assertEqual(
                mlp["out"].predict["test"]({
                    "inp.inputs": [self.xor_ins[i]]
                })["out.predict.test"], self.xor_outs[i])
예제 #4
0
    def test_batch_norm(self):
        import theano, numpy

        def batchnorm(W, b, data):
            return numpy.asarray(
                W * ((data - numpy.mean(data)) / numpy.std(data)) + b,
                dtype=theano.config.floatX)

        data = numpy.random.randn(1, 100).astype(theano.config.floatX)
        batch = MD.BatchNormalization(testMu=0, testSigma=1)
        inp = ML.Input(100, name='inp', decorators=[batch])

        model = inp.network
        model.init()

        m1 = numpy.mean(model["inp"].propagate["train"]({
            "inp.inputs": data
        })["inp.propagate.train"])
        m2 = numpy.mean(
            batchnorm(
                batch.getP("gamma").getValue(),
                batch.getP("beta").getValue(), data))

        epsilon = 1e-6
        self.assertTrue((m1 - m2) < epsilon)
예제 #5
0
    def test_save_load_64h(self):
        import os
        import Mariana.network as MN

        ls = MS.GradientDescent(lr=0.1)
        cost = MC.NegativeLogLikelihood()

        i = ML.Input(2, 'inp')
        o = ML.SoftmaxClassifier(nbClasses=2,
                                 cost=cost,
                                 learningScenari=[ls],
                                 name="out")

        prev = i
        for i in xrange(64):
            h = ML.Hidden(size=10, activation=MA.ReLU(), name="Hidden_%s" % i)
            prev > h
            prev = h

        mlp = prev > o
        mlp.init()
        mlp.save("test_save")

        mlp2 = MN.loadModel("test_save.mar")
        mlp2.init()

        v1 = mlp["out"].propagate["test"]({
            "inp.inputs": self.xor_ins
        })["out.propagate.test"]
        v2 = mlp2["out"].propagate["test"]({
            "inp.inputs": self.xor_ins
        })["out.propagate.test"]
        self.assertTrue((v1 == v2).all())
        os.remove('test_save.mar')
예제 #6
0
    def __init__(self, ls, cost):
        maxPool = MCONV.MaxPooling2D(2, 2)

        #The input channeler will take regular layers and arrange them into several channels
        i = ML.Input(28 * 28, name='inp')
        ichan = MCONV.InputChanneler(28, 28, name='inpChan')

        c1 = MCONV.Convolution2D(nbFilters=1,
                                 filterHeight=5,
                                 filterWidth=5,
                                 activation=MA.Tanh(),
                                 pooler=maxPool,
                                 name="conv1")

        c2 = MCONV.Convolution2D(nbFilters=1,
                                 filterHeight=5,
                                 filterWidth=5,
                                 activation=MA.Tanh(),
                                 pooler=maxPool,
                                 name="conv2")

        f = MCONV.Flatten(name="flat")
        h = ML.Hidden(5,
                      activation=MA.Tanh(),
                      decorators=[],
                      regularizations=[],
                      name="hid")
        o = ML.SoftmaxClassifier(10,
                                 decorators=[],
                                 learningScenario=ls,
                                 costObject=cost,
                                 name="out",
                                 regularizations=[])

        self.model = i > ichan > c1 > c2 > f > h > o
예제 #7
0
    def test_multiinputs(self):
        ls = MS.GradientDescent(lr=0.1)

        inpA = ML.Embedding(2, 2, 2, name="IA")
        inpB = ML.Input(2, name="IB")
        inpNexus = ML.Composite(name="InputNexus")

        h1 = ML.Hidden(32,
                       activation=MA.ReLU(),
                       decorators=[],
                       regularizations=[],
                       name="Fully-connected1")

        o = ML.Regression(2,
                          decorators=[],
                          activation=MA.ReLU(),
                          learningScenario=ls,
                          costObject=MC.CrossEntropy(),
                          name="Out",
                          regularizations=[])

        inpA > inpNexus
        inpB > inpNexus
        m = inpNexus > h1 > o
        m.init()
예제 #8
0
파일: tests.py 프로젝트: Solertis/Mariana
    def test_ae(self) :

        data = []
        for i in xrange(8) :
            zeros = numpy.zeros(8)
            zeros[i] = 1
            data.append(zeros)

        ls = MS.GradientDescent(lr = 0.1)
        cost = MC.MeanSquaredError()

        i = ML.Input(8, name = 'inp')
        h = ML.Hidden(3, activation = MA.ReLU(), initializations=[MI.SmallUniformWeights(), MI.ZeroBias()], name = "hid")
        o = ML.Autoencode(targetLayerName = "inp", activation = MA.ReLU(), initializations=[MI.SmallUniformWeights(), MI.ZeroBias()], learningScenario = ls, costObject = cost, name = "out" )

        ae = i > h > o

        miniBatchSize = 1
        for e in xrange(2000) :
            for i in xrange(0, len(data), miniBatchSize) :
                ae.train(o, inp = data[i:i+miniBatchSize])

        res = ae.propagate(o, inp = data)["outputs"]
        for i in xrange(len(res)) :
            self.assertEqual( numpy.argmax(data[i]), numpy.argmax(res[i]))
예제 #9
0
파일: tests.py 프로젝트: Solertis/Mariana
        def getModel(inpSize, filterWidth) :
            ls = MS.GradientDescent(lr = 0.5)
            cost = MC.NegativeLogLikelihood()
            
            pooler = MCONV.MaxPooling2D(1, 2)

            i = ML.Input(inpSize, name = 'inp')
            ichan = MCONV.InputChanneler(1, inpSize, name = 'inpChan')
            
            c1 = MCONV.Convolution2D( 
                nbFilters = 5,
                filterHeight = 1,
                filterWidth = filterWidth,
                activation = MA.ReLU(),
                pooler = pooler,
                name = "conv1"
            )

            c2 = MCONV.Convolution2D( 
                nbFilters = 10,
                filterHeight = 1,
                filterWidth = filterWidth,
                activation = MA.ReLU(),
                pooler = pooler,
                name = "conv2"
            )

            f = MCONV.Flatten(name = "flat")
            h = ML.Hidden(5, activation = MA.ReLU(), decorators = [], regularizations = [ ], name = "hid" )
            o = ML.SoftmaxClassifier(2, decorators = [], learningScenario = ls, costObject = cost, name = "out", regularizations = [ ] )
            
            model = i > ichan > c1 > c2 > f > h > o
            return model
예제 #10
0
    def test_ae(self):

        data = []
        for i in xrange(8):
            zeros = numpy.zeros(8)
            zeros[i] = 1
            data.append(zeros)

        ls = MS.GradientDescent(lr=0.1)
        cost = MC.MeanSquaredError()

        i = ML.Input(8, name='inp')
        h = ML.Hidden(3, activation=MA.ReLU(), name="hid")
        o = ML.Regression(8,
                          activation=MA.ReLU(),
                          learningScenario=ls,
                          costObject=cost,
                          name="out")

        ae = i > h > o

        miniBatchSize = 2
        for e in xrange(2000):
            for i in xrange(0, len(data), miniBatchSize):
                ae.train(o,
                         inp=data[i:i + miniBatchSize],
                         targets=data[i:i + miniBatchSize])

        res = ae.propagate(o, inp=data)["outputs"]
        for i in xrange(len(res)):
            self.assertEqual(numpy.argmax(data[i]), numpy.argmax(res[i]))
예제 #11
0
    def test_composite(self):
        ls = MS.GradientDescent(lr=0.1)
        cost = MC.NegativeLogLikelihood()

        inp = ML.Input(2, 'inp')
        h1 = ML.Hidden(5, activation=MA.Tanh(), name="h1")
        h2 = ML.Hidden(5, activation=MA.Tanh(), name="h2")
        o = ML.SoftmaxClassifier(2,
                                 learningScenario=ls,
                                 costObject=cost,
                                 name="out")
        c = ML.Composite(name="Comp")

        inp > h1 > c
        inp > h2 > c
        mlp = c > o

        for i in xrange(10000):
            ii = i % len(self.xor_ins)
            mlp.train(o, inp=[self.xor_ins[ii]], targets=[self.xor_outs[ii]])

        self.assertEqual(mlp.predict(o, inp=[self.xor_ins[0]])["class"], 0)
        self.assertEqual(mlp.predict(o, inp=[self.xor_ins[1]])["class"], 1)
        self.assertEqual(mlp.predict(o, inp=[self.xor_ins[2]])["class"], 1)
        self.assertEqual(mlp.predict(o, inp=[self.xor_ins[3]])["class"], 0)
예제 #12
0
def ae1(data):
    '''Using a regression layer. This layer needs an explicit target'''

    miniBatchSize = 2

    ls = MS.GradientDescent(lr=0.1)
    cost = MC.MeanSquaredError()

    i = ML.Input(8, name='inp')
    h = ML.Hidden(3,
                  activation=MA.ReLU(),
                  initializations=[MI.SmallUniformWeights(),
                                   MI.ZeroBias()],
                  name="hid")
    o = ML.Regression(
        8,
        activation=MA.ReLU(),
        initializations=[MI.SmallUniformWeights(),
                         MI.ZeroBias()],
        learningScenario=ls,
        costObject=cost,
        name="out")

    ae = i > h > o

    for e in xrange(1000):
        for i in xrange(0, len(data), miniBatchSize):
            ae.train(o,
                     inp=data[i:i + miniBatchSize],
                     targets=data[i:i + miniBatchSize])

    return ae, o
예제 #13
0
def ae2(data):
    """This one uses an Autoencode layer. This layer is a part of the graph and does not need a specific traget"""

    miniBatchSize = 1

    ls = MS.GradientDescent(lr=0.1)
    cost = MC.MeanSquaredError()

    i = ML.Input(8, name='inp')
    h = ML.Hidden(3,
                  activation=MA.ReLU(),
                  initializations=[MI.SmallUniformWeights(),
                                   MI.ZeroBias()],
                  name="hid")
    o = ML.Autoencode(
        i.name,
        activation=MA.ReLU(),
        initializations=[MI.SmallUniformWeights(),
                         MI.ZeroBias()],
        learningScenario=ls,
        costObject=cost,
        name="out")

    ae = i > h > o
    # ae.init()
    # o.train.printGraph()
    for e in xrange(2000):
        for i in xrange(0, len(data), miniBatchSize):
            ae.train(o, inp=data[i:i + miniBatchSize])

    return ae, o
예제 #14
0
        def getModel(inpSize, filterWidth):
            ls = MS.GradientDescent(lr=0.5)
            cost = MC.NegativeLogLikelihood()

            i = ML.Input((1, 1, inpSize), name='inp')

            c1 = MCONV.Convolution2D(numFilters=5,
                                     filterHeight=1,
                                     filterWidth=filterWidth,
                                     activation=MA.ReLU(),
                                     name="conv1")

            pool1 = MSAMP.MaxPooling2D(poolHeight=1, poolWidth=2, name="pool1")

            c2 = MCONV.Convolution2D(numFilters=10,
                                     filterHeight=1,
                                     filterWidth=filterWidth,
                                     activation=MA.ReLU(),
                                     name="conv2")

            pool2 = MSAMP.MaxPooling2D(poolHeight=1, poolWidth=2, name="pool2")

            h = ML.Hidden(5, activation=MA.ReLU(), name="hid")
            o = ML.SoftmaxClassifier(nbClasses=2,
                                     cost=cost,
                                     learningScenari=[ls],
                                     name="out")

            model = i > c1 > pool1 > c2 > pool2 > h > o
            model.init()
            return model
예제 #15
0
    def test_optimizer_override(self):

        ls = MS.GradientDescent(lr=0.5)
        cost = MC.NegativeLogLikelihood()

        inp = ML.Input(1, 'inp')
        h = ML.Hidden(5,
                      activation=MA.Tanh(),
                      learningScenari=[MS.Fixed("b")],
                      name="h")
        o = ML.SoftmaxClassifier(
            2,
            learningScenari=[MS.GradientDescent(lr=0.5),
                             MS.Fixed("W")],
            cost=cost,
            name="out")
        net = inp > h > o
        net.init()

        ow = o.getP('W').getValue()
        ob = o.getP('b').getValue()
        hw = h.getP('W').getValue()
        hb = h.getP('b').getValue()
        for x in xrange(1, 10):
            net["out"].train({
                "inp.inputs": [[1]],
                "out.targets": [1]
            })["out.drive.train"]

        self.assertTrue(sum(ow[0]) == sum(o.getP('W').getValue()[0]))
        self.assertTrue(sum(ob) != sum(o.getP('b').getValue()))
        self.assertTrue(sum(hb) == sum(h.getP('b').getValue()))
        self.assertTrue(sum(hw[0]) != sum(h.getP('W').getValue()[0]))
예제 #16
0
    def test_embedding(self):
        """the first 3 and the last 3 should be diametrically opposed"""
        data = [[0], [1], [2], [3], [4], [5]]
        targets = [0, 0, 0, 1, 1, 1]

        ls = MS.GradientDescent(lr=0.5)
        cost = MC.NegativeLogLikelihood()

        inp = ML.Input(1, 'inp')
        emb = ML.Embedding(nbDimensions=2,
                           dictSize=len(data),
                           learningScenari=[ls],
                           name="emb")
        o = ML.SoftmaxClassifier(2,
                                 learningScenari=[MS.Fixed()],
                                 cost=cost,
                                 name="out")
        net = inp > emb > o
        net.init()

        miniBatchSize = 2
        for i in xrange(2000):
            for i in xrange(0, len(data), miniBatchSize):
                net["out"].train({
                    "inp.inputs": data[i:i + miniBatchSize],
                    "out.targets": targets[i:i + miniBatchSize]
                })["out.drive.train"]

        embeddings = emb.getP("embeddings").getValue()
        for i in xrange(0, len(data) / 2):
            v = numpy.dot(embeddings[i], embeddings[i + len(data) / 2])
            self.assertTrue(v < -1)
def Perceptron(ls, cost):
    i = ML.Input(28 * 28, name='inp')
    o = ML.SoftmaxClassifier(10,
                             learningScenario=ls,
                             costObject=cost,
                             name="out",
                             regularizations=[MR.L1(0), MR.L2(0)])

    return i > o
예제 #18
0
    def test_merge(self):
        ls = MS.GradientDescent(lr=0.1)
        cost = MC.NegativeLogLikelihood()

        inp1 = ML.Input(1, 'inp1')
        inp2 = ML.Input(1, 'inp2')
        merge = ML.M((inp1 + inp2) / 3 * 10 - 1, name="merge")

        inp1 > merge
        mdl = inp2 > merge
        mdl.init()

        self.assertEqual(merge.getIntrinsicShape(), inp1.getIntrinsicShape())
        v = mdl["merge"].propagate["test"]({
            "inp1.inputs": [[1]],
            "inp2.inputs": [[8]]
        })["merge.propagate.test"]
        self.assertEqual(v, 29)
예제 #19
0
	def test_mask(self) :
		import theano, numpy
		
		inp = ML.Input(100, 'inp', decorators=[MD.Mask(mask = numpy.zeros(100))])
		model = inp.network

		data = numpy.random.randn(1, 100).astype(theano.config.floatX)
		out = model.propagate(inp, inp=data)["outputs"]
		
		self.assertEqual(sum(out[0]), 0)
예제 #20
0
    def test_mask(self):
        import theano, numpy

        inp = ML.Input(100, 'inp', decorators=[MD.Mask(mask=numpy.zeros(100))])
        model = inp.network
        model.init()

        data = numpy.random.randn(1, 100).astype(theano.config.floatX)
        out = model["inp"].propagate["train"]({
            "inp.inputs": data
        })["inp.propagate.train"]

        self.assertEqual(sum(out[0]), 0)
예제 #21
0
def getMLP(self, nbInputs=2, nbClasses=2):
    ls = MS.GradientDescent(lr=0.1)
    cost = MC.NegativeLogLikelihood()

    i = ML.Input(nbInputs, 'inp')
    h = ML.Hidden(size=6, activation=MA.ReLU(), name="Hidden_0.500705866892")
    o = ML.SoftmaxClassifier(nbClasses=nbClasses,
                             cost=cost,
                             learningScenari=[ls],
                             name="out")

    mlp = i > h > o
    mlp.init()
    return mlp
예제 #22
0
    def test_dropout(self):
        import theano, numpy

        inp = ML.Input(100,
                       'inp',
                       decorators=[MD.BinomialDropout(dropoutRatio=0.8)])
        model = inp.network
        model.init()

        data = numpy.random.randn(1, 100).astype(theano.config.floatX) + 1
        out = model["inp"].propagate["train"]({
            "inp.inputs": data
        })["inp.propagate.train"]

        self.assertTrue(sum(out[0] != 0) < sum(data[0] != 0))
예제 #23
0
    def test_ae_reg(self):
        powerOf2 = 3
        nbUnits = 2**powerOf2

        data = []
        for i in xrange(nbUnits):
            zeros = numpy.zeros(nbUnits)
            zeros[i] = 1
            data.append(zeros)

        ls = MS.GradientDescent(lr=0.1)
        cost = MC.MeanSquaredError()

        i = ML.Input(nbUnits, name='inp')
        h = ML.Hidden(powerOf2,
                      activation=MA.ReLU(),
                      initializations=[
                          MI.Uniform('W', small=True),
                          MI.SingleValue('b', 0)
                      ],
                      name="hid")
        o = ML.Regression(nbUnits,
                          activation=MA.ReLU(),
                          initializations=[
                              MI.Uniform('W', small=True),
                              MI.SingleValue('b', 0)
                          ],
                          learningScenari=[ls],
                          cost=cost,
                          name="out")

        ae = i > h > o
        ae.init()

        miniBatchSize = 1
        for e in xrange(2000):
            for i in xrange(0, len(data), miniBatchSize):
                miniBatch = data[i:i + miniBatchSize]
                ae["out"].train({
                    "inp.inputs": miniBatch,
                    "out.targets": miniBatch
                })["out.drive.train"]

        res = ae["out"].propagate["test"]({
            "inp.inputs": data
        })["out.propagate.test"]
        for i in xrange(len(res)):
            self.assertEqual(numpy.argmax(data[i]), numpy.argmax(res[i]))
예제 #24
0
	def test_batch_norm(self) :
		import theano, numpy
		
		def batchnorm(W, b, data) :
			return numpy.asarray( W * ( (data-numpy.mean(data)) / numpy.std(data) ) + b, dtype= theano.config.floatX)

		data = numpy.random.randn(1, 100).astype(theano.config.floatX)
		
		inp = ML.Input(100, 'inp', decorators=[MD.BatchNormalization()])
		
		model = inp.network
		m1 = numpy.mean( model.propagate(inp, inp=data)["outputs"])
		m2 = numpy.mean( batchnorm(inp.batchnorm_W.get_value(), inp.batchnorm_b.get_value(), data) )

		epsilon = 1e-6
		self.assertTrue ( (m1 - m2) < epsilon )
예제 #25
0
파일: tests.py 프로젝트: Solertis/Mariana
    def trainMLP_xor(self) :
        ls = MS.GradientDescent(lr = 0.1)
        cost = MC.NegativeLogLikelihood()

        i = ML.Input(2, 'inp')
        h = ML.Hidden(10, activation = MA.ReLU(), name = "Hidden_0.500705866892")
        o = ML.SoftmaxClassifier(2, learningScenario = ls, costObject = cost, name = "out")

        mlp = i > h > o
        
        self.xor_ins = numpy.array(self.xor_ins)
        self.xor_outs = numpy.array(self.xor_outs)
        for i in xrange(1000) :
            mlp.train(o, inp = self.xor_ins, targets = self.xor_outs )

        return mlp
예제 #26
0
def MLP(ls, cost):

    i = ML.Input(28 * 28, name='inp')
    h = ML.Hidden(500,
                  activation=MA.Tanh(),
                  decorators=[MD.GlorotTanhInit()],
                  regularizations=[MR.L1(0), MR.L2(0.0001)],
                  name="hid")
    o = ML.SoftmaxClassifier(10,
                             decorators=[MD.ZerosInit()],
                             learningScenario=ls,
                             costObject=cost,
                             name="out",
                             regularizations=[MR.L1(0),
                                              MR.L2(0.0001)])

    mlp = i > h > o

    return mlp
예제 #27
0
파일: tests.py 프로젝트: craftsliu/Mariana
    def trainMLP_xor(self):
        ls = MS.GradientDescent(lr=0.1)
        cost = MC.NegativeLogLikelihood()

        i = ML.Input(2, 'inp')
        h = ML.Hidden(4,
                      activation=MA.Tanh(),
                      decorators=[dec.GlorotTanhInit()],
                      regularizations=[MR.L1(0), MR.L2(0)])
        o = ML.SoftmaxClassifier(2,
                                 learningScenario=ls,
                                 costObject=cost,
                                 name="out")

        mlp = i > h > o

        self.xor_ins = numpy.array(self.xor_ins)
        self.xor_outs = numpy.array(self.xor_outs)
        for i in xrange(1000):
            ii = i % len(self.xor_ins)
            mlp.train(o, inp=[self.xor_ins[ii]], targets=[self.xor_outs[ii]])

        return mlp
예제 #28
0
    def test_multiout_fctmixin(self):

        i = ML.Input(1, name='inp')
        o1 = ML.Autoencode(targetLayer=i,
                           activation=MA.Tanh(),
                           learningScenari=[MS.GradientDescent(lr=0.1)],
                           cost=MC.MeanSquaredError(),
                           name="out1")
        o2 = ML.Regression(1,
                           activation=MA.Tanh(),
                           learningScenari=[MS.GradientDescent(lr=0.2)],
                           cost=MC.MeanSquaredError(),
                           name="out2")

        i > o1
        ae = i > o2
        ae.init()

        preOut1 = ae["out1"].test({"inp.inputs": [[1]]})["out1.drive.test"]
        preOut2 = ae["out2"].test({
            "inp.inputs": [[1]],
            "out2.targets": [[1]]
        })["out2.drive.test"]
        ae["out1"].train({"inp.inputs": [[1]]})["out1.drive.train"]
        self.assertTrue(
            preOut1 > ae["out1"].test({"inp.inputs": [[1]]})["out1.drive.test"]
        )
        self.assertTrue(preOut2 == ae["out2"].test({
            "inp.inputs": [[1]],
            "out2.targets": [[1]]
        })["out2.drive.test"])

        preOut1 = ae["out1"].test({"inp.inputs": [[1]]})["out1.drive.test"]
        preOut2 = ae["out2"].test({
            "inp.inputs": [[1]],
            "out2.targets": [[1]]
        })["out2.drive.test"]
        ae["out2"].train({
            "inp.inputs": [[1]],
            "out2.targets": [[1]]
        })["out2.drive.train"]
        self.assertTrue(preOut1 == ae["out1"].test({"inp.inputs": [[1]]})
                        ["out1.drive.test"])
        self.assertTrue(preOut2 > ae["out2"].test({
            "inp.inputs": [[1]],
            "out2.targets": [[1]]
        })["out2.drive.test"])

        preOut1 = ae["out1"].test({"inp.inputs": [[1]]})["out1.drive.test"]
        preOut2 = ae["out2"].test({
            "inp.inputs": [[1]],
            "out2.targets": [[1]]
        })["out2.drive.test"]
        fct = ae["out1"].train + ae["out2"].train + ae["inp"].propagate["train"]
        ret = fct({"inp.inputs": [[1]], "out2.targets": [[1]]})
        self.assertEqual(len(ret), 3)
        self.assertTrue(
            preOut1 > ae["out1"].test({"inp.inputs": [[1]]})["out1.drive.test"]
        )
        self.assertTrue(preOut2 > ae["out2"].test({
            "inp.inputs": [[1]],
            "out2.targets": [[1]]
        })["out2.drive.test"])
예제 #29
0
* automatically saves the best model for each set (train, test, validation)
* automatically saves the model if the training halts because of an error or if the process is killed
* saves a log if the process dies unexpectedly
* training results and hyper parameters values are recorded to a file
* allows you to define custom stop criteria
* training info is printed at each epoch, including best scores and at which epoch they were achieved

"""

if __name__ == "__main__":

	# Let's define the network
	ls = MS.GradientDescent(lr=0.01)
	cost = MC.NegativeLogLikelihood()

	i = ML.Input(28 * 28, name='inp')
	h = ML.Hidden(500, activation=MA.Tanh(), decorators=[MD.GlorotTanhInit()], regularizations=[MR.L1(0), MR.L2(0.0001)], name="hid")
	o = ML.SoftmaxClassifier(10, learningScenario=ls, costObject=cost, name="out", regularizations=[MR.L1(0), MR.L2(0.0001)])

	mlp = i > h > o

	mlp.saveDOT("mnist_mlp")
	mlp.saveHTML("mnist_mlp")
	# And then map sets to the inputs and outputs of our network
	train_set, validation_set, test_set = load_mnist()

	trainData = MDM.Series(images=train_set[0], numbers=train_set[1])
	trainMaps = MDM.DatasetMapper()
	trainMaps.mapInput(i, trainData.images)
	trainMaps.mapOutput(o, trainData.numbers)
예제 #30
0
        elif i%3 == 0 :
            data[i][start:start+patternSize] = patternC2
            targets.append(2)
        else :
            targets.append(0)

    targets = numpy.asarray(targets, dtype=theano.config.floatX)
    return data, targets

if __name__ == "__main__" :
    examples, targets = makeDataset(300, 100, 10)

    ls = MS.GradientDescent(lr = 0.01)
    cost = MC.NegativeLogLikelihood()

    i = ML.Input(100, 'inp')
    h1 = ML.Hidden(50, activation = MA.ReLU(), decorators = [MD.BatchNormalization()])
    h2 = ML.Hidden(2, activation = MA.Softmax())
    o = ML.SoftmaxClassifier(3, learningScenario = ls, costObject = cost, name = "out")

    mlp = i > h1 > h2 > o

    for k in xrange(100) :
        for example, target in zip(examples, targets) :
            mlp.train(o, inp=[example], targets=[target])
    
    nbErr = 0
    for example, target in zip(examples, targets) :
        if target != mlp.classify(o, inp=[example])["class"] :
            nbErr += 1