Exemplo n.º 1
0
def train_SdA(training_epochs=15,
              train_lr=0.001,
              data_file=None,
              skip_col=2,
              batch_size=1,
              random_seed_1=89677,
              random_seed_2=123,
              net_structure=[1000, 1000, 1000],
              corruption_levels=[.1, .2, .3],
              output_file=None,
              net_file=None):

    logging.basicConfig(filename=output_file.replace('activity_SdA.txt',
                                                     'SdA.log'),
                        level=logging.INFO)
    logging.info('Training the dataset:' + data_file)
    logging.info('The structure of the networks:' + str(net_structure))
    logging.info('Training epoches:' + str(training_epochs) + '\t' +
                 'Batch size:' + str(batch_size) + '\t' + 'Learning rate:' +
                 str(train_lr) + '\t' + 'Corruption levels:' +
                 str(corruption_levels) + '\n' + 'Random seed for training:' +
                 str(random_seed_1) + '\t' +
                 'Ramdom seed for permuting sample order:' +
                 str(random_seed_2))

    datasets = PCLfile(data_file, skip_col)
    train_set_x, sample_id = datasets.get_permuted_sample(
        seed=random_seed_2)  #Permute the order of samples using random_seed_2
    print '... finish reading the data'

    train_set_x = theano.shared(train_set_x, borrow=True)

    # compute number of minibatches for training
    train_size = train_set_x.get_value(borrow=True).shape[0]
    n_train_batches = train_size / batch_size

    # numpy random generator
    numpy_rng = numpy.random.RandomState(random_seed_1)

    # the number of input nodes
    input_node = len(datasets.id_list)

    print '... building the model'
    # construct the stacked denoising autoencoder class
    sda = SdA(numpy_rng=numpy_rng,
              n_ins=input_node,
              hidden_layers_sizes=net_structure)

    #########################
    # TRAINING THE MODEL #
    #########################
    print '... getting the training functions'
    training_fns = sda.training_functions(train_set_x=train_set_x,
                                          batch_size=batch_size)

    print '... training the model'
    start_time = time.clock()
    ## Train layer-wise
    corruption_levels = corruption_levels
    for i in xrange(sda.n_layers):
        # go through training epochs
        for epoch in xrange(training_epochs):
            # go through the training set
            c = []
            for batch_index in xrange(n_train_batches):
                c.append(training_fns[i](index=batch_index,
                                         corruption=corruption_levels[i],
                                         lr=train_lr))
            print 'Training layer %i, epoch %d, cost ' % (i, epoch),
            print numpy.mean(c)
            logging.info('Training layer %i, epoch %d, cost %f ' %
                         (i, epoch, numpy.mean(c)))

    end_time = time.clock()

    logging.info('The training code for file ' + os.path.split(__file__)[1] +
                 ' ran for %.2fm' % ((end_time - start_time) / 60.))
    print '... training finished.'

    ##############################################################
    # Return the final activity value and raw activity value
    # for each node of each input sample
    ##############################################################
    output_fh = open(output_file, 'w')
    raw_output_fh = open(output_file.replace('activity', 'rawActivity'), 'w')
    each_layer_output = sda.return_activity(train_set_x=train_set_x)
    each_layer_raw_output = sda.return_raw_activity(train_set_x=train_set_x)
    for i in xrange(sda.n_layers):
        output_fh.write('layer %i \n' % (i + 1))
        raw_output_fh.write('layer %i \n' % (i + 1))
        for train_sample in xrange(train_size):
            node_activation = each_layer_output[i](train_sample)
            node_raw_activation = each_layer_raw_output[i](train_sample)
            output_fh.write(sample_id[train_sample] + '\t')
            raw_output_fh.write(sample_id[train_sample] + '\t')
            numpy.savetxt(output_fh,
                          node_activation,
                          fmt='%.8f',
                          delimiter='\t')
            numpy.savetxt(raw_output_fh,
                          node_raw_activation,
                          fmt='%.8f',
                          delimiter='\t')

    ##############################################################
    # Return weight matrix and bias vectors of the final network #
    ##############################################################
    net_file = open(net_file, 'w')
    weight_output, bias_output, bias_prime_output = sda.return_network()
    for i in xrange(len(weight_output)):
        net_file.write('layer %i \n' % (i + 1))
        net_file.write('weight matrix \n')
        numpy.savetxt(net_file, weight_output[i], fmt='%.8f', delimiter='\t')
        net_file.write('hidden bias vector \n')
        numpy.savetxt(net_file, bias_output[i], fmt='%.8f', delimiter='\t')
        net_file.write('visible bias vector \n')
        numpy.savetxt(net_file,
                      bias_prime_output[i],
                      fmt='%.8f',
                      delimiter='\t')
Exemplo n.º 2
0
def train_SdA(training_epochs=15, train_lr=0.001, data_file = None, skip_col = 2,
              batch_size=1, random_seed_1 = 89677, random_seed_2 = 123,net_structure = [1000,1000,1000], 
              corruption_levels = [.1, .2, .3], output_file = None, net_file = None):


    logging.basicConfig(filename = output_file.replace('activity_SdA.txt', 'SdA.log'), level= logging.INFO)
    logging.info('Training the dataset:' + data_file)
    logging.info('The structure of the networks:'+ str(net_structure))
    logging.info('Training epoches:'+str(training_epochs)+'\t'+'Batch size:'+str(batch_size)+'\t'+'Learning rate:'+str(train_lr)+'\t'+'Corruption levels:'+str(corruption_levels)+'\n'
        +'Random seed for training:'+str(random_seed_1)+'\t'+ 'Ramdom seed for permuting sample order:'+str(random_seed_2))
    
    datasets = PCLfile(data_file, skip_col)    
    train_set_x, sample_id = datasets.get_permuted_sample(seed = random_seed_2)#Permute the order of samples using random_seed_2
    print '... finish reading the data'

    train_set_x = theano.shared(train_set_x,borrow=True)

    # compute number of minibatches for training
    train_size = train_set_x.get_value(borrow=True).shape[0]
    n_train_batches = train_size / batch_size

    # numpy random generator
    numpy_rng = numpy.random.RandomState(random_seed_1)

    # the number of input nodes
    input_node = len(datasets.id_list)

    print '... building the model'
    # construct the stacked denoising autoencoder class
    sda = SdA(numpy_rng=numpy_rng, n_ins= input_node,
              hidden_layers_sizes= net_structure)

    #########################
    # TRAINING THE MODEL #
    #########################
    print '... getting the training functions'
    training_fns = sda.training_functions(train_set_x=train_set_x,
                                          batch_size=batch_size)

    print '... training the model'
    start_time = time.clock()
    ## Train layer-wise
    corruption_levels = corruption_levels
    for i in xrange(sda.n_layers):
        # go through training epochs
        for epoch in xrange(training_epochs):
            # go through the training set
            c = []
            for batch_index in xrange(n_train_batches):
                c.append(training_fns[i](index=batch_index,
                         corruption=corruption_levels[i],
                         lr=train_lr))
            print 'Training layer %i, epoch %d, cost ' % (i, epoch),
            print numpy.mean(c)
            logging.info('Training layer %i, epoch %d, cost %f ' % (i, epoch, numpy.mean(c) ))

    end_time = time.clock()

    logging.info('The training code for file ' + os.path.split(__file__)[1] + ' ran for %.2fm' % ((end_time - start_time) / 60.))
    print '... training finished.'

    ##############################################################
    # Return the final activity value and raw activity value
    # for each node of each input sample 
    ##############################################################
    output_fh = open(output_file,'w')
    raw_output_fh = open(output_file.replace('activity','rawActivity'),'w')
    each_layer_output = sda.return_activity(train_set_x=train_set_x)
    each_layer_raw_output = sda.return_raw_activity(train_set_x=train_set_x)
    for i in xrange(sda.n_layers):
        output_fh.write('layer %i \n' %(i+1))
        raw_output_fh.write('layer %i \n' %(i+1))
        for train_sample in xrange(train_size):
            node_activation = each_layer_output[i](train_sample)
            node_raw_activation = each_layer_raw_output[i](train_sample)
            output_fh.write(sample_id[train_sample]+'\t')
            raw_output_fh.write(sample_id[train_sample]+'\t')
            numpy.savetxt(output_fh, node_activation, fmt= '%.8f', delimiter= '\t') 
            numpy.savetxt(raw_output_fh, node_raw_activation, fmt= '%.8f', delimiter= '\t') 


    ##############################################################
    # Return weight matrix and bias vectors of the final network #
    ##############################################################
    net_file = open(net_file,'w')
    weight_output, bias_output, bias_prime_output = sda.return_network()
    for i in xrange(len(weight_output)):
        net_file.write('layer %i \n' %(i+1))
        net_file.write('weight matrix \n')
        numpy.savetxt(net_file, weight_output[i], fmt= '%.8f', delimiter = '\t') 
        net_file.write('hidden bias vector \n')
        numpy.savetxt(net_file, bias_output[i], fmt= '%.8f', delimiter = '\t')
        net_file.write('visible bias vector \n')
        numpy.savetxt(net_file, bias_prime_output[i], fmt= '%.8f', delimiter = '\t')