from plearn.pyplearn import * ##### # Definitions of default values. # # If you provide "foo=bar" on the command line, then the variable # args.foo will have the value bar. Otherwise, it will have the value # defined here. class args( plargs ): # Path to the dataset data = 'HOME:data/mnist_all.amat' # The number of examples in the training/validation/testing sets train_size = 50000 valid_size = 10000 test_size = 10000 # The dimension of the input input_size = 784 # Number of classes n_classes = 10 # Learning rate during the contrastive divergence phase cd_learning_rate = 0.01 # Learning rate during the fine-tuning (gradient descent) phase grad_learning_rate = 0.1 # Number of times we loop over the training set, # for the training of each RBM n_epochs_cd = 1 # Number of times we loop over the training set during the fine-tuning phase n_epochs_grad = 0 # Output debug information report_progress = True # Number of hidden layers, and number of units on them # You can set it from the command line by "n_hidden=500,500,2000" n_hidden = [ 500, 500, 2000 ] ###### # Definition of some useful values, from the provided variables # Number of units, including input layer n_units = [ args.input_size ] + args.n_hidden # Convert the numbers of epochs into numbers of samples n_samples_cd = args.n_epochs_cd * args.train_size n_samples_grad = args.n_epochs_grad * args.train_size # DeepBeliefNet.training_schedule is a vector containing, for each # layer, the number of examples that should be seen during the # training of that layer. # In addition to that, the last element is the number of samples we see # during the fine-tuning phase. # Here, we restrain ourselves to the case where we use the same number # of examples for each RBM. # training_schedule = [ n_samples_cd_step, ..., n_samples_cd, n_samples_grad ] training_schedule = [ n_samples_cd ] * (len(n_units)-1) + [ n_samples_grad ] # The total number of examples to see (needed by PLearner) nstages = sum( training_schedule ) # The original dataset dataset = pl.AutoVMatrix( filename = args.data, inputsize = args.input_size, targetsize = 1, weightsize = 0 ) # Build the RBMLayers from n_units # layers = [ # pl.RBMBinomialLayer( size = n_units[0] ), # pl.RBMBinomialLayer( size = n_units[1] ), # .... # ] layers = [ pl.RBMBinomialLayer( size = i ) for i in n_units ] # Build the RBMConnections that connect the RBMLayers connections = [ pl.RBMMatrixConnection( down_size = i, up_size = j ) for i,j in zip( n_units[:-1], n_units[1:] ) ] # Build the learner from the above specifications learner = pl.DeepBeliefNet( # Hyperparameters cd_learning_rate = args.cd_learning_rate, grad_learning_rate = args.grad_learning_rate, training_schedule = training_schedule, nstages = nstages, # Architecture n_classes = args.n_classes, use_classification_cost = 1, layers = layers, connections = connections, # Other forget_when_training_set_changes = 0, report_progress = args.report_progress ) # A simple splitter # The first split will be used during the training, the other will only # be used to compute testing errors (no hyper-parameter selection is # done). # Repeating the first split allows to retest on the training set after # the training (the training error is not always useful or correct). end_train = args.train_size end_valid = end_train + args.valid_size end_test = end_valid + args.test_size splitter = pl.FractionSplitter( splits = TMat( 1, 3, [ (0, end_train), (end_train, end_valid), (end_valid, end_test) ] ) ) # All the files produced by the experiment will be stored in this directory # You can use the values of the variables defined in the script # Here, we save the directories of all experiments into a common subdirectory # args.expdir ensures the unicity of the directory, with a timestamp expdir = 'expdir/example' \ + '_cd_learning_rate=' + str(args.cd_learning_rate) \ + '_grad_learning_rate=' + str(args.grad_learning_rate) \ + '_n_epochs_cd=' + str(args.n_epochs_cd) \ + '_n_epochs_grad=' + str(args.n_epochs_grad) \ + '_' + args.expdir # The tester, that will perform the experiment tester = pl.PTester( # The objects defined above learner = learner, dataset = dataset, splitter = splitter, # The statistics we want to keep statnames = [ 'E[train.E[NLL]]', 'E[train.E[class_error]]', 'E[test1.E[NLL]]', 'E[test1.E[class_error]]', 'E[test2.E[NLL]]', 'E[test2.E[class_error]]' ], # Where to save the data provide_learner_expdir = 1, expdir = expdir, # What to save save_initial_tester = 0, save_test_costs = 0, save_test_outputs = 0, save_learners = 1 ) def main(): return tester