Exemple #1
0
    def run(self):

        print "--- Loading data"
        self.jobman.save()
        if not os.path.exists("files"):
            os.mkdir("files")
        bs = self.jobman.state["bs"]

        params = self.cae_emotions.params.values() + self.cae_identity.params.values() + self.logrege.params.values()

        self.cost = self.init_costs()

        # Computing gradient of the sum of all costs
        # on all the parameters of the model
        # (except the convolutional part)

        self.updates = dict((p, p - self.jobman.state["lr"] * g) for p, g in zip(params, T.grad(self.cost, params)))

        k = T.iscalar()

        # Compiling optimisation theano function

        self.optimize = theano.function(
            [k],
            self.total_cost_list,
            updates=self.updates,
            givens={self.x: self.shared_x[k : k + 1], self.y: self.shared_y[k : k + 1]},
        )
        self.jobman.state["valid_emotion"] = 0
        self.jobman.state["valid_identity"] = 0
        self.jobman.state["valid_both"] = 0
        print "--- Training"
        sys.stdout.flush()
        bs = self.jobman.state["bs"]
        costs = []
        # self.evalreg()
        for i in range(self.jobman.state["epochs"]):
            costs = []

            # For each epoch we take 50% of labeled data
            # and 50% of unlabeled data shuffled in random
            # order.

            x, y = shuffle_array(
                self.data.get_train_unlabeled(i, shared_array=False), self.data.get_train_labeled(i, shared_array=False)
            )
            self.shared_x.set_value(x)
            self.shared_y.set_value(y)

            # We evaluate the representations using an SVM
            # with 'evalfreq' frequency.

            if (i + 1) % self.jobman.state["evalfreq"] == 0:
                he = self.cae_emotions.get_hidden(self.x)
                hi = self.cae_identity.get_hidden(self.x)
                self.svm(he, "emotion")
                self.svm(hi, "identity")
                self.svm(T.concatenate([he, hi], axis=1), "both")
                self.cae_emotions.save(i, "emot")
                self.cae_identity.save(i, "iden")
                self.logrege.save(i, "logreg")
                mi.view_data(self.cae_emotions.params["W"].get_value(borrow=True).T, "files/emot_" + str(i))
                mi.view_data(self.cae_identity.params["W"].get_value(borrow=True).T, "files/iden_" + str(i))
                self.jobman.save()

            total = self.shared_x.get_value(borrow=True).shape[0] / bs

            # We loop over the samples of the current split
            # to tune the parameters of the model.

            for j in range(total):
                costs.append(self.optimize(j))
                if j % self.jobman.state["freq"] == 0:
                    # self.evalreg()
                    print i, j, zip(self.total_cost_str, numpy.mean(numpy.array(costs), axis=0))
                    sys.stdout.flush()

            self.jobman.state["cft"] = i
        self.svm()
Exemple #2
0
    def run(self):

        print '--- Loading data'
        self.jobman.save()
        if not os.path.exists('files'):
            os.mkdir('files')
        bs = self.jobman.state['bs']

        params = self.cae.params.values() + self.logreg.params.values()

        cost = self.getcosts()

        # Computing gradient of the sum of all costs
        # on all the parameters of the model 
        # (except the convolutional part)

        updates = dict ( (p,p - self.jobman.state['lr']*g) for p,g in zip(params,T.grad(cost,params)) )
        
        k = T.iscalar()

        # Compiling optimisation theano function

        optimize = theano.function([k],self.costs,updates=updates,givens = { self.x:self.shared_x[k:k+1],
                                                                             self.y:self.shared_y[k:k+1] })
        self.jobman.state['valid_discriminant'] = 0
        self.jobman.state['valid_nuisance'] = 0 
        self.jobman.state['valid_both'] = 0
        print '--- Training'
        sys.stdout.flush()
        bs = self.jobman.state['bs']
        costs = []

        h = self.cae.get_hidden(self.x)
        h_d = h[:,:self.featsplit]
        h_o = h[:,self.featsplit:]

        for i in range(self.jobman.state['epochs']):
            costs = []

            # For each epoch we take 50% of labeled data
            # and 50% of unlabeled data shuffled in random
            # order.

            self.data.get_train_labeled(i)

            # We evaluate the representations using an SVM
            # with 'evalfreq' frequency.
            
            if (i+1)%self.jobman.state['evalfreq'] == 0:
                self.svm(h_d,'discriminant')
                self.svm(h_o,'nuisance')
                self.svm(h,'both')
                self.cae.save(i)
                self.logreg.save(i,'logreg')
                mi.view_data(self.cae.params['W'].get_value(borrow=True).T,'files/cae_'+str(i))
                self.jobman.save()

            total = self.shared_x.get_value(borrow=True).shape[0]/bs

            # We loop over the samples of the current split
            # to tune the parameters of the model.

            for j in range(total):
                costs.append(optimize(j))
                if j%self.jobman.state['freq'] == 0:
                    #self.evalreg()
                    print i, j,zip(self.costs_str,numpy.mean(numpy.array(costs),axis=0))
                    sys.stdout.flush()
            self.jobman.state['cft']=i
        self.svm(h_d,'discriminant')
        self.svm(h_o,'nuisance')
        self.svm(h,'both')
Exemple #3
0
    def run(self):

        print '--- Loading data'
        self.jobman.save()
        if not os.path.exists('files'):
            os.mkdir('files')
        bs = self.jobman.state['bs']

        params = self.cae_emotions.params.values(
        ) + self.cae_identity.params.values() + self.logrege.params.values()

        self.cost = self.init_costs()

        # Computing gradient of the sum of all costs
        # on all the parameters of the model
        # (except the convolutional part)

        self.updates = dict((p, p - self.jobman.state['lr'] * g)
                            for p, g in zip(params, T.grad(self.cost, params)))

        k = T.iscalar()

        # Compiling optimisation theano function

        self.optimize = theano.function([k],
                                        self.total_cost_list,
                                        updates=self.updates,
                                        givens={
                                            self.x: self.shared_x[k:k + 1],
                                            self.y: self.shared_y[k:k + 1]
                                        })
        self.jobman.state['valid_emotion'] = 0
        self.jobman.state['valid_identity'] = 0
        self.jobman.state['valid_both'] = 0
        print '--- Training'
        sys.stdout.flush()
        bs = self.jobman.state['bs']
        costs = []
        #self.evalreg()
        for i in range(self.jobman.state['epochs']):
            costs = []

            # For each epoch we take 50% of labeled data
            # and 50% of unlabeled data shuffled in random
            # order.

            x, y = shuffle_array(
                self.data.get_train_unlabeled(i, shared_array=False),
                self.data.get_train_labeled(i, shared_array=False))
            self.shared_x.set_value(x)
            self.shared_y.set_value(y)

            # We evaluate the representations using an SVM
            # with 'evalfreq' frequency.

            if (i + 1) % self.jobman.state['evalfreq'] == 0:
                he = self.cae_emotions.get_hidden(self.x)
                hi = self.cae_identity.get_hidden(self.x)
                self.svm(he, 'emotion')
                self.svm(hi, 'identity')
                self.svm(T.concatenate([he, hi], axis=1), 'both')
                self.cae_emotions.save(i, 'emot')
                self.cae_identity.save(i, 'iden')
                self.logrege.save(i, 'logreg')
                mi.view_data(
                    self.cae_emotions.params['W'].get_value(borrow=True).T,
                    'files/emot_' + str(i))
                mi.view_data(
                    self.cae_identity.params['W'].get_value(borrow=True).T,
                    'files/iden_' + str(i))
                self.jobman.save()

            total = self.shared_x.get_value(borrow=True).shape[0] / bs

            # We loop over the samples of the current split
            # to tune the parameters of the model.

            for j in range(total):
                costs.append(self.optimize(j))
                if j % self.jobman.state['freq'] == 0:
                    #self.evalreg()
                    print i, j, zip(self.total_cost_str,
                                    numpy.mean(numpy.array(costs), axis=0))
                    sys.stdout.flush()

            self.jobman.state['cft'] = i
        self.svm()
Exemple #4
0
    def run(self):

        print '--- Loading data'
        self.jobman.save()
        if not os.path.exists('files'):
            os.mkdir('files')
        bs = self.jobman.state['bs']

        params = self.cae.params.values() + self.logreg.params.values()

        cost = self.getcosts()

        # Computing gradient of the sum of all costs
        # on all the parameters of the model
        # (except the convolutional part)

        updates = dict((p, p - self.jobman.state['lr'] * g)
                       for p, g in zip(params, T.grad(cost, params)))

        k = T.iscalar()

        # Compiling optimisation theano function

        optimize = theano.function([k],
                                   self.costs,
                                   updates=updates,
                                   givens={
                                       self.x: self.shared_x[k:k + 1],
                                       self.y: self.shared_y[k:k + 1]
                                   })
        self.jobman.state['valid_discriminant'] = 0
        self.jobman.state['valid_nuisance'] = 0
        self.jobman.state['valid_both'] = 0
        print '--- Training'
        sys.stdout.flush()
        bs = self.jobman.state['bs']
        costs = []

        h = self.cae.get_hidden(self.x)
        h_d = h[:, :self.featsplit]
        h_o = h[:, self.featsplit:]

        for i in range(self.jobman.state['epochs']):
            costs = []

            # For each epoch we take 50% of labeled data
            # and 50% of unlabeled data shuffled in random
            # order.

            self.data.get_train_labeled(i)

            # We evaluate the representations using an SVM
            # with 'evalfreq' frequency.

            if (i + 1) % self.jobman.state['evalfreq'] == 0:
                self.svm(h_d, 'discriminant')
                self.svm(h_o, 'nuisance')
                self.svm(h, 'both')
                self.cae.save(i)
                self.logreg.save(i, 'logreg')
                mi.view_data(self.cae.params['W'].get_value(borrow=True).T,
                             'files/cae_' + str(i))
                self.jobman.save()

            total = self.shared_x.get_value(borrow=True).shape[0] / bs

            # We loop over the samples of the current split
            # to tune the parameters of the model.

            for j in range(total):
                costs.append(optimize(j))
                if j % self.jobman.state['freq'] == 0:
                    #self.evalreg()
                    print i, j, zip(self.costs_str,
                                    numpy.mean(numpy.array(costs), axis=0))
                    sys.stdout.flush()
            self.jobman.state['cft'] = i
        self.svm(h_d, 'discriminant')
        self.svm(h_o, 'nuisance')
        self.svm(h, 'both')