Exemple #1
0
 def run(self):
     """[read the arguments passed to check if is to train model, to run preprocess or run both]
     """
     config_json = self._open_config()
     my_parser = argparse.ArgumentParser(
         description='Model to classify if is speech or music')
     my_parser.add_argument('-m',
                            '--model',
                            required=False,
                            action='store_true')
     my_parser.add_argument('-d',
                            '--data',
                            required=False,
                            action='store_true')
     my_parser.add_argument('-y',
                            '--youtube',
                            required=False,
                            action='store_true')
     args = my_parser.parse_args()
     if (args.data):
         preProcess = PreProcess(config_json)
         preProcess.run()
     if (args.model):
         mlpClassifier = MlpClassifier(config_json)
         mlpClassifier.run()
     if (args.youtube):
         preProcessYoutube = PreProcessYoutube(config_json)
         preProcessYoutube.run()
Exemple #2
0
    def run(self,
            num_kernels=[25, 25],
            kernel_sizes=[(11, 11), (5, 5)],
            batch_size=256,
            epochs=100000,
            optimizer='RMSprop'):

        optimizerData = {}
        optimizerData['learning_rate'] = 0.0005 / 2
        optimizerData['rho'] = 0.9
        optimizerData['epsilon'] = 1e-2
        optimizerData['momentum'] = 0.9

        print '... Loading data'

        # load in and process data
        if data_size == 'large':
            preProcess = PreProcess()
            data = preProcess.run()
        elif data_size == 'medium':
            preProcess = Medium()
            data = preProcess.run()
        elif data_size == 'small':
            preProcess = Small()
            data = preProcess.run()
        else:
            print 'data_size must be small, medium or large.'
            exit()
        train_set_x, train_set_y = data[0], data[3]
        valid_set_x, valid_set_y = data[1], data[4]
        test_set_x, test_set_y = data[2], data[5]

        train_set_x = theano.tensor._shared(train_set_x, borrow=True)
        valid_set_x = theano.tensor._shared(valid_set_x, borrow=True)
        train_set_y = theano.tensor._shared(train_set_y, borrow=True)
        valid_set_y = theano.tensor._shared(valid_set_y, borrow=True)
        test_set_x = theano.tensor._shared(test_set_x, borrow=True)
        test_set_y = theano.tensor._shared(test_set_y, borrow=True)

        print '... Initializing network'

        # training parameters
        self.n_sports = 500

        # print error if batch size is to large
        if valid_set_y.get_value(borrow=True).size < batch_size:
            print 'Error: Batch size is larger than size of validation set.'

        # compute batch sizes for train/test/validation
        n_train_batches = train_set_x.get_value(borrow=True).shape[0]
        n_valid_batches = valid_set_x.get_value(borrow=True).shape[0]
        n_test_batches = test_set_x.get_value(borrow=True).shape[0]
        n_train_batches /= batch_size
        n_valid_batches /= batch_size
        n_test_batches /= batch_size

        # symbolic variables
        x = T.matrix('x')  # input image data
        y = T.ivector('y')  # input label data

        self.model(batch_size, num_kernels, kernel_sizes, x, y)

        # Initialize parameters and functions
        cost = self.layer3.negative_log_likelihood(y)  # Cost function
        params = self.params  # List of parameters
        grads = T.grad(cost, params)  # Gradient
        index = T.lscalar()  # Index

        # Intialize optimizer
        updates = self.init_optimizer(optimizer, cost, params, optimizerData)

        # Train function
        train_model = theano.function(
            [index],
            cost,
            updates=updates,
            givens={
                x: train_set_x[index * batch_size:(index + 1) * batch_size],
                y: train_set_y[index * batch_size:(index + 1) * batch_size]
            })

        # Validation function
        validate_model = theano.function(
            [index],
            self.layer3.errors(y),
            givens={
                x: valid_set_x[index * batch_size:(index + 1) * batch_size],
                y: valid_set_y[index * batch_size:(index + 1) * batch_size]
            })

        # Test function
        test_model = theano.function(
            [index],
            self.layer3.errors(y),
            givens={
                x: test_set_x[index * batch_size:(index + 1) * batch_size],
                y: test_set_y[index * batch_size:(index + 1) * batch_size]
            })

        def solve():
            costs = []
            for i in xrange(n_train_batches):
                costs.append(train_model(i))
            return costs

        def shuffle(train_set_x, train_set_y):
            #print train_set_x.get_value(borrow=True).shape[0]
            rand = np.random.permutation(
                range(train_set_x.get_value(borrow=True).shape[0]))
            train_set_x.set_value(train_set_x.get_value(borrow=True)[rand],
                                  borrow=True)
            train_set_y.set_value(train_set_y.get_value(borrow=True)[rand],
                                  borrow=True)
            return train_set_x, train_set_y, train_model

        # Solver
        try:
            print '... Solving'
            start_time = time.time()
            for epoch in range(epochs):
                t1 = time.time()
                train_set_x, train_set_y, train_model = shuffle(
                    train_set_x, train_set_y)
                costs = solve()
                validation_losses = [
                    validate_model(i) for i in xrange(n_valid_batches)
                ]
                t2 = time.time()
                print "Epoch {}    NLL {:.2}    %err in validation set {:.1%}    Time (epoch/total) {:.2}/{:.2} mins".format(
                    epoch + 1, np.mean(costs), np.mean(validation_losses),
                    (t2 - t1) / 60., (t2 - start_time) / 60.)
                # f = open('workfile' , 'r+')
                # f.write("Epoch {}    NLL {:.2}    %err in validation set {:.1%}    Time (epoch/total) {:.2}/{:.2} mins".format(epoch + 1, np.mean(costs), np.mean(validation_losses),(t2-t1)/60.,(t2-start_time)/60.))
                # f.close()
                with open("workfile_batch_c", "a") as myfile:
                    myfile.write(
                        "Epoch {}    NLL {:.2}    %err in validation set {:.1%}    Time (epoch/total) {:.2}/{:.2} mins \n"
                        .format(epoch + 1, np.mean(costs),
                                np.mean(validation_losses), (t2 - t1) / 60.,
                                (t2 - start_time) / 60.))
                if epoch % 10 == 0:

                    test_errors = [
                        test_model(i) for i in range(n_test_batches)
                    ]
                    print "test errors: {:.1%}".format(np.mean(test_errors))
                    with open("workfile_batch_c2", "a") as myfile:
                        myfile.write("test errors: {:.1%}\n".format(
                            np.mean(test_errors)))

        except KeyboardInterrupt:
            print '... Exiting solver'
        # Evaluate performance

        predict = theano.function(
            inputs=[index],
            outputs=self.layer3.prediction(),
            givens={
                x: test_set_x[index * batch_size:(index + 1) * batch_size]
            })

        test_errors = [test_model(i) for i in range(n_test_batches)]
        print "test errors: {:.1%}".format(np.mean(test_errors))

        pred = [predict(i) for i in range(n_test_batches)]
        print pred[0].shape
    def run(self,
            num_kernels  = [125,125],
            kernel_sizes = [(11, 11), (5, 5)],
            batch_size   = 50,
            epochs       = 100000,
            optimizer    = 'RMSprop'):
            
            
        optimizerData = {}
        optimizerData['learning_rate'] = 0.0005/2
        optimizerData['rho']           = 0.9
        optimizerData['epsilon']       = 1e-2
        optimizerData['momentum']      = 0.9
        
        print '... Loading data'
        
        # load in and process data
        if data_size == 'large':
            preProcess              = PreProcess()
            data                    = preProcess.run()
        elif data_size == 'medium':
            preProcess              = Medium()
            data                    = preProcess.run()
        elif data_size == 'small':
            preProcess             = Small()
            data                    = preProcess.run()   
        else:
            print 'data_size must be small, medium or large.'
            exit()
        train_set_x,train_set_y = data[0],data[3]
        valid_set_x,valid_set_y = data[1],data[4]
        test_set_x,test_set_y   = data[2],data[5]

        train_set_x = theano.tensor._shared(train_set_x,borrow=True)
        valid_set_x = theano.tensor._shared(valid_set_x,borrow=True)
        train_set_y = theano.tensor._shared(train_set_y,borrow=True)
        valid_set_y = theano.tensor._shared(valid_set_y,borrow=True)
        test_set_x  = theano.tensor._shared(test_set_x,borrow=True)
        test_set_y  = theano.tensor._shared(test_set_y,borrow=True)
        
        print '... Initializing network'
       
        # training parameters
        self.n_sports = 500
    
        # print error if batch size is to large
        if valid_set_y.get_value(borrow=True).size<batch_size:
            print 'Error: Batch size is larger than size of validation set.'

        # compute batch sizes for train/test/validation
        n_train_batches  = train_set_x.get_value(borrow=True).shape[0]
        n_valid_batches  = valid_set_x.get_value(borrow=True).shape[0]
        n_test_batches   = test_set_x.get_value(borrow=True).shape[0]
        n_train_batches /= batch_size
        n_valid_batches /= batch_size
        n_test_batches  /= batch_size

        # symbolic variables
        x = T.matrix('x')  # input image data
        y = T.ivector('y')  # input label data
        
        self.model(batch_size, num_kernels, kernel_sizes, x, y)

        # Initialize parameters and functions
        cost   = self.layer3.negative_log_likelihood(y)        # Cost function
        params = self.params                                   # List of parameters
        grads  = T.grad(cost, params)                          # Gradient
        index  = T.lscalar()                                   # Index
        
        # Intialize optimizer
        updates = self.init_optimizer(optimizer, cost, params, optimizerData)

        # Train function
        train_model = theano.function(
                        [index],
                        cost,
                        updates = updates,
                        givens  = {
                                        x: train_set_x[index * batch_size: (index + 1) * batch_size], 
                                        y: train_set_y[index * batch_size: (index + 1) * batch_size] 
                        }
                    )      
            

        # Validation function
        validate_model = theano.function(
                         [index],
                         self.layer3.errors(y),
                         givens = {
                                  x: valid_set_x[index * batch_size: (index + 1) * batch_size],
                                  y: valid_set_y[index * batch_size: (index + 1) * batch_size]
                }
            )

        # Test function
        test_model = theano.function(
             [index],
             self.layer3.errors(y),
             givens = {
                      x: test_set_x[index * batch_size: (index + 1) * batch_size],
                      y: test_set_y[index * batch_size: (index + 1) * batch_size]
            }
        )

        predict = theano.function(inputs = [index],
                            outputs = self.layer3.prediction(),
                            givens = {
                            x: test_set_x[index*batch_size: (index+1)*batch_size]
            }
        )

        

        def solve():
            costs = []
            for i in xrange(n_train_batches):
                costs.append(train_model(i))
            return costs

        def shuffle(train_set_x,train_set_y):
            #print train_set_x.get_value(borrow=True).shape[0]
            rand = np.random.permutation(range(train_set_x.get_value(borrow=True).shape[0]))
            train_set_x.set_value(train_set_x.get_value(borrow=True)[rand],borrow=True)
            train_set_y.set_value(train_set_y.get_value(borrow=True)[rand],borrow=True)
            return train_set_x,train_set_y,train_model


        print np.savetxt('y_vec_LARGE.txt',test_set_y.get_value(borrow=True),delimiter=',')
        length = test_set_y.get_value(borrow=True).shape[0]
        print length
        print 'saved'
        try:
            print '... Solving'
            start_time = time.time()    
            for epoch in range(epochs):
                t1 = time.time()
                train_set_x,train_set_y,train_model = shuffle(train_set_x,train_set_y) 
                costs                   = solve()
                validation_losses       = [validate_model(i) for i in xrange(n_valid_batches)]
                t2 = time.time()
                print "Epoch {}    NLL {:.2}    %err in validation set {:.1%}    Time (epoch/total) {:.2}/{:.2} mins".format(epoch + 1, np.mean(costs), np.mean(validation_losses),(t2-t1)/60.,(t2-start_time)/60.)
                # f = open('workfile' , 'r+')
                # f.write("Epoch {}    NLL {:.2}    %err in validation set {:.1%}    Time (epoch/total) {:.2}/{:.2} mins".format(epoch + 1, np.mean(costs), np.mean(validation_losses),(t2-t1)/60.,(t2-start_time)/60.))
                # f.close()
                with open("workfile_BIG31", "a") as myfile:
                     myfile.write("Epoch {}    NLL {:.2}    %err in validation set {:.1%}    Time (epoch/total) {:.2}/{:.2} mins \n".format(epoch + 1, np.mean(costs), np.mean(validation_losses),(t2-t1)/60.,(t2-start_time)/60.))
                if epoch%1== 0:
                    predictions = np.array([predict(i) for i in range(n_test_batches)])
                    print predictions[0].shape
                    print predictions.shape
                    predictions = predictions.reshape((length-length%batch_size),500) 
                    with file('workfile_BIG33', 'w') as outfile:
                        # I'm writing a header here just for the sake of readability
                        # Any line starting with "#" will be ignored by numpy.loadtxt
                        outfile.write('# Array shape: {0}\n'.format(len(predictions)))

                        # Iterating through a ndimensional array produces slices along
                        # the last axis. This is equivalent to data[i,:,:] in this case
                        for data_slice in predictions:

                            # The formatting string indicates that I'm writing out
                            # the values in left-justified columns 7 characters in width
                            # with 2 decimal places.  
                            np.savetxt(outfile, data_slice, fmt='%-7.2f')

                            # Writing out a break to indicate different slices...
                            #outfile.write('\n')
                

                    test_errors = [test_model(i) for i in range(n_test_batches)]
                    print "test errors: {:.1%}".format(np.mean(test_errors))
                    with open("workfile_BIG32", "a") as myfile:
                        myfile.write("test errors: {:.1%}\n".format(np.mean(test_errors)))

        except KeyboardInterrupt:
            print '... Exiting solver'
        # Evaluate performance 




        test_errors = [test_model(i) for i in range(n_test_batches)]
        print "test errors: {:.1%}".format(np.mean(test_errors))

        pred = [predict(i) for i in range(n_test_batches)]
        print pred[0].shape
Exemple #4
0
    def run(self,
            num_kernels=[15, 15],
            kernel_sizes=[(11, 11), (5, 5)],
            batch_size=50,
            epochs=20,
            optimizer='RMSprop'):

        optimizerData = {}
        optimizerData['learning_rate'] = 0.001
        optimizerData['rho'] = 0.9
        optimizerData['epsilon'] = 1e-4
        optimizerData['momentum'] = 0.9

        print '... Loading data'

        # load in and process data
        preProcess = PreProcess()
        data = preProcess.run()
        train_set_x, train_set_y = data[0], data[3]
        valid_set_x, valid_set_y = data[1], data[4]
        test_set_x, test_set_y = data[2], data[5]

        print train_set_x.eval().shape

        print '... Initializing network'

        # training parameters
        self.n_sports = np.max(train_set_y.eval()) + 1

        # print error if batch size is to large
        if valid_set_y.eval().size < batch_size:
            print 'Error: Batch size is larger than size of validation set.'

        # compute batch sizes for train/test/validation
        n_train_batches = train_set_x.get_value(borrow=True).shape[0]
        n_test_batches = test_set_x.get_value(borrow=True).shape[0]
        n_valid_batches = valid_set_x.get_value(borrow=True).shape[0]
        n_train_batches /= batch_size
        n_test_batches /= batch_size
        n_valid_batches /= batch_size

        # symbolic variables
        x = T.matrix('x')  # input image data
        y = T.ivector('y')  # input label data

        self.model(batch_size, num_kernels, kernel_sizes, x, y)

        # Initialize parameters and functions
        cost = self.layer3.negative_log_likelihood(y)  # Cost function
        params = self.params  # List of parameters
        grads = T.grad(cost, params)  # Gradient
        index = T.lscalar()  # Index

        # Intialize optimizer
        updates = self.init_optimizer(optimizer, cost, params, optimizerData)

        # Training model
        train_model = theano.function(
            [index],
            cost,
            updates=updates,
            givens={
                x: train_set_x[index * batch_size:(index + 1) * batch_size],
                y: train_set_y[index * batch_size:(index + 1) * batch_size]
            })

        # Validation function
        validate_model = theano.function(
            [index],
            self.layer3.errors(y),
            givens={
                x: valid_set_x[index * batch_size:(index + 1) * batch_size],
                y: valid_set_y[index * batch_size:(index + 1) * batch_size]
            })

        # Test function
        test_model = theano.function(
            [index],
            self.layer3.errors(y),
            givens={
                x: test_set_x[index * batch_size:(index + 1) * batch_size],
                y: test_set_y[index * batch_size:(index + 1) * batch_size]
            })

        def solve():
            costs = []
            for i in xrange(n_train_batches):
                costs.append(train_model(i))

                #if i % 1000 ==0:
                #    print i
            return costs

        # Solver
        try:
            print '... Solving'
            start_time = time.time()
            for epoch in range(epochs):
                t1 = time.time()
                costs = solve()
                validation_losses = [
                    validate_model(i) for i in xrange(n_valid_batches)
                ]
                t2 = time.time()
                print "Epoch {}    NLL {:.2}    %err in validation set {:.1%}    Time (epoch/total) {:.2}/{:.2} mins".format(
                    epoch + 1, np.mean(costs), np.mean(validation_losses),
                    (t2 - t1) / 60., (t2 - start_time) / 60.)
        except KeyboardInterrupt:
            print '... Exiting solver'
        # Evaluate performance
        test_errors = [test_model(i) for i in range(n_test_batches)]
        print "test errors: {:.1%}".format(np.mean(test_errors))