Beispiel #1
0
def test_multitask_dbn_mnist(learning_rate=0.01, training_epochs=10, batch_size=20,
                             finetune_lr=0.05, finetune_epoch=10, output_folder=None, dropout=0.2,
                             model_structure=[500, 100], isPCD=0, k=1, plot_weight=False):
    """
        test_dbn_mnist(output_folder='/home/eric/Desktop/dbn_plots',
                   training_epochs=10,
                   model_structure=[500, 100],
                   finetune_epoch=10)
    """
    assert output_folder is not None

    from dbn_variants import MultiTask_DBN as DBN
    #################################
    #     Data Constructing         #
    #################################

    from sklearn.datasets import fetch_mldata
    from xylearn.utils.data_util import get_train_test
    from xylearn.utils.data_normalization import rescale


    dataset = fetch_mldata('MNIST original')

    data = get_train_test(rescale(dataset.data), dataset.target, useGPU=1, shuffle=True)

    train_x, train_y = data['train']
    test_x, test_y = data['test']

    num_of_classes = max(train_y.eval()) - min(train_y.eval()) + 1

    # man-made task label for testing
    n_outs = [num_of_classes, num_of_classes, num_of_classes]

    n_vis = train_x.get_value(borrow=True).shape[1]
    n_train_batches = train_x.get_value(borrow=True).shape[0] / batch_size

    print '... building the model'

    dbn = DBN(n_ins=n_vis, n_outs=n_outs, dropout=dropout,
              hidden_layers_sizes=model_structure, isPCD=isPCD)

    print '... getting the pretraining functions'
    pretrain_fns = dbn.get_pretrain_fns(train_set_x=train_x,
                                        batch_size=batch_size,
                                        k=k)
    #################################
    #     Training the DBN          #
    #################################
    if not os.path.isdir(output_folder):
        os.makedirs(output_folder)
    os.chdir(output_folder)

    plotting_time = 0.
    start_time = time.clock()
    import PIL.Image
    from visualizer import tile_raster_images

    # go through training epochs
    for epoch in xrange(training_epochs):

        # go through the training set
        mean_cost = []
        for i in xrange(dbn.n_layers):
            # go through pretraining epochs
            for batch_index in xrange(n_train_batches):
                # for each batch, we extract the gibbs chain
                new_cost = pretrain_fns[i](index=batch_index, lr=learning_rate)
                mean_cost += [new_cost]

            if plot_weight:
                # plot all weights
                weight = dbn.project_back(dbn.sigmoid_layers[i].W.T,
                                          from_layer_i=i)
                # Construct image from the weight matrix
                image = PIL.Image.fromarray(tile_raster_images(
                    X=weight,
                    img_shape=(dbn.input_length, dbn.input_height), tile_shape=(10, 10),
                    tile_spacing=(1, 1)))
                image.save('pretrain_epoch_%i_layer_%i.png' % (epoch, i))

        print 'Training epoch %d, cost is ' % epoch, numpy.mean(mean_cost)

    end_time = time.clock()
    pretraining_time = end_time - start_time
    print ('Training took %f minutes' % (pretraining_time / 60.))

    ########################
    # FINETUNING THE MODEL #
    ########################
    # get the training, validation and testing function for the model
    print '... getting the finetuning functions'

    # man-made labels, list of labels
    train_ys = [train_y, train_y, train_y]
    finetune_fn = dbn.get_finetune_fn(train_x, train_ys, batch_size)

    print '... finetunning the model'

    test_score = 0.

    start_time = time.clock()
    # go through the each batch
    for epoch in xrange(finetune_epoch):
        ## Pre-train layer-wise
        c = []
        for batch_index in xrange(n_train_batches):
            c.append(finetune_fn(index=batch_index, lr=finetune_lr))

        if plot_weight:
            # plot all weights
            weight = dbn.project_back(dbn.sigmoid_layers[-1].W.T,
                                      from_layer_i=dbn.n_layers - 1)
            # Construct image from the weight matrix
            image = PIL.Image.fromarray(tile_raster_images(
                X=weight,
                img_shape=(dbn.input_length, dbn.input_height), tile_shape=(10, 10),
                tile_spacing=(1, 1)))
            image.save('finetune_epoch_%i.png' % (epoch))

        print 'Fine-tuning epoch %i, model cost ' % epoch,
        print numpy.mean(c)
        test_score = numpy.mean(c)

    end_time = time.clock()
    print(('Optimization complete with best test performance %f %%') %
          (100. - test_score * 100.))
    print >> sys.stderr, ('The fine tuning code for file ' +
                          os.path.split(__file__)[1] +
                          ' ran for %.2fm' % ((end_time - start_time)
                                              / 60.))

    ########################
    #   Test THE MODEL     #
    ########################
    task_id = 0
    print '\nTest complete with error rate ' + str(dbn.get_error_rate(test_x, test_y, task_id=task_id)) + \
          ' for task_' + str(task_id)