Esempio n. 1
0
def validation_err(vl_start_index=0, vl_limit=None):
    
    global if_load_trained_model
    global validate_model
    global validate_results

    if if_load_trained_model == 0:
        load_trained_model()
    
    valid_set = tdtf.read_data_patch_to_ndarray(valid_dataset_route, vl_start_index, vl_limit)
    print valid_set[1]
    valid_set_x, valid_set_y = valid_set
    validation_loss = validate_model(valid_set_x, valid_set_y)
    validation_pred_y = validate_results(valid_set_x)
    print validation_pred_y
    label = [0, 0]
    right = [0, 0]
    for i in range(len(validation_pred_y)):
        y_pred = validation_pred_y[i]
        y = valid_set_y[i]
        right[y] += 1
        if y != y_pred:
            if y == 0:
                label[1] += 1
            else:
                label[0] += 1 

    t_num = len(validation_pred_y)
    right_num = t_num - label[0] - label[1]
    print "total %d, 0:1=%d:%d, right %d , wrong label to bg %d , label to 0 %d " % (t_num, right[0], right[1], right_num, label[1], label[0])
    print('validation error %f %%' % \
        (validation_loss * 100.))
Esempio n. 2
0
def validation_err(vl_start_index=0, vl_limit=None):

    global if_load_trained_model
    global validate_model
    global validate_results

    if if_load_trained_model == 0:
        load_trained_model()

    valid_set = tdtf.read_data_patch_to_ndarray(valid_dataset_route,
                                                vl_start_index, vl_limit)
    print valid_set[1]
    valid_set_x, valid_set_y = valid_set
    validation_loss = validate_model(valid_set_x, valid_set_y)
    validation_pred_y = validate_results(valid_set_x)
    print validation_pred_y
    label = [0, 0]
    right = [0, 0]
    for i in range(len(validation_pred_y)):
        y_pred = validation_pred_y[i]
        y = valid_set_y[i]
        right[y] += 1
        if y != y_pred:
            if y == 0:
                label[1] += 1
            else:
                label[0] += 1

    t_num = len(validation_pred_y)
    right_num = t_num - label[0] - label[1]
    print "total %d, 0:1=%d:%d, right %d , wrong label to bg %d , label to 0 %d " % (
        t_num, right[0], right[1], right_num, label[1], label[0])
    print('validation error %f %%' % \
        (validation_loss * 100.))
Esempio n. 3
0
File: lenet.py Progetto: hphp/Kaggle
def train_by_lenet5(tr_start_index, tr_limit, vl_start_index, vl_limit, output_filename="tmp.file", learning_rate=0.13, n_epochs=5000):

    global train_dataset_route
    global valid_dataset_route

    output_file = open(output_filename, 'w')

    print train_dataset_route, type(train_dataset_route)
    """
    :type learning_rate: float
    :param learning_rate: learning rate used (factor for the stochastic
                          gradient)

    :type n_epochs: int
    :param n_epochs: maximal number of epochs to run the optimizer

    :type nkerns: list of ints
    :param nkerns: number of kernels on each layer
    """

    train_set = tdtf.read_data_patch_to_ndarray(train_dataset_route, tr_start_index, tr_limit)
    datasets = load_data.shared_dataset(train_set)
    train_set_x, train_set_y = datasets

    valid_set = tdtf.read_data_patch_to_ndarray(valid_dataset_route, vl_start_index, vl_limit)
    print valid_set[1]
    datasets = load_data.shared_dataset(valid_set)
    valid_set_x, valid_set_y = datasets

    # compute number of minibatches for training, validation and testing
    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_train_batches /= batch_size
    n_valid_batches /= batch_size

    # allocate symbolic variables for the data

    ######################
    # BUILD ACTUAL MODEL #
    ######################
    print '... building the model'

    # the cost we minimize during training is the NLL of the model
    cost = layer3.negative_log_likelihood(y)

    # create a function to compute the mistakes that are made by the model
    validate_model = theano.function([index], 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]})

    # create a list of all model parameters to be fit by gradient descent
    params = layer3.params + layer2.params + layer1.params + layer0.params

    # create a list of gradients for all model parameters
    grads = T.grad(cost, params)

    # train_model is a function that updates the model parameters by
    # SGD Since this model has many parameters, it would be tedious to
    # manually create an update rule for each model parameter. We thus
    # create the updates list by automatically looping over all
    # (params[i],grads[i]) pairs.
    updates = []
    for param_i, grad_i in zip(params, grads):
        updates.append((param_i, param_i - learning_rate * grad_i))

    train_model = theano.function([index], [cost, layer3.errors(y), layer3.params[0][0][0]], 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]})

    ###############
    # TRAIN MODEL #
    ###############
    print '... training'
    # early-stopping parameters
    patience = 50000 # look as this many examples regardless
    patience_increase = 2  # wait this much longer when a new best is
                           # found
    improvement_threshold = 0.995  # a relative improvement of this much is
                                   # considered significant
    validation_frequency = min(n_train_batches, patience / 2)
                                  # go through this many
                                  # minibatche before checking the network
                                  # on the validation set; in this case we
                                  # check every epoch

    best_params = None
    best_validation_loss = numpy.inf
    best_iter = 0
    test_score = 0.
    start_time = time.clock()

    epoch = 0
    done_looping = False

    min_train_cost = 10000
    decreasing_num = 0

    last_train_err = 1
    last_train_cost = 1

    while (epoch < n_epochs) and (not done_looping):
        epoch = epoch + 1
        for minibatch_index in xrange(n_train_batches):

            iter = (epoch - 1) * n_train_batches + minibatch_index

            if iter % 100 == 0:
                print 'training @ iter = ', iter , ' patience = ' , patience
            cost_ij, train_err, par = train_model(minibatch_index)
            
            decreasing_rate = (last_train_err - train_err) / (last_train_err) * 100.
            last_train_err = train_err
            if last_train_err == 0:
                last_train_err += 0.0000001
            c_d_rate = (last_train_cost - cost_ij) / (last_train_cost) * 100.
            last_train_cost = cost_ij 
            print >> output_file, ('epoch %i, minibatch %i/%i, train_cost %f , train_error %.2f %%, decreasing rate %f %%, cost_decreasing rate %f %%, W00 ' % \
                (epoch, minibatch_index + 1, n_train_batches,
                cost_ij,
                train_err* 100.
                ,decreasing_rate
                ,c_d_rate))

            #print layer1.params[0:1][0][0:3]
            #print layer2.params[0:1][0][0:3]
            if cost_ij < min_train_cost:
                decreasing_num = 0
                min_train_cost = cost_ij
                layer0_state = layer0.__getstate__()
                layer1_state = layer1.__getstate__()
                layer2_state = layer2.__getstate__()
                layer3_state = layer3.__getstate__()
                trained_model_list = [layer0_state, layer1_state, layer2_state, layer3_state]
                trained_model_array = numpy.asarray(trained_model_list)
                classifier_file = open(train_model_route, 'w')
                cPickle.dump([1,2,3], classifier_file, protocol=2)
                numpy.save(classifier_file, trained_model_array)
                classifier_file.close()
            else:
                print "decreasing"
                decreasing_num += 1
                if decreasing_num > 100:
                    done_looping = True
                    break
            if (iter + 1) % validation_frequency == 0:

                # compute zero-one loss on validation set
                validation_losses = [validate_model(i) for i
                                     in xrange(n_valid_batches)]
                this_validation_loss = numpy.mean(validation_losses)
                print >> output_file, ('epoch %i, minibatch %i/%i, validation error %f %%' % \
                      (epoch, minibatch_index + 1, n_train_batches, \
                       this_validation_loss * 100.))

                # if we got the best validation score until now
                if this_validation_loss < best_validation_loss:

                    #improve patience if loss improvement is good enough
                    if this_validation_loss < best_validation_loss *  \
                       improvement_threshold:
                        patience = max(patience, iter * patience_increase)

                    # save best validation score and iteration number
                    best_validation_loss = this_validation_loss
                    best_iter = iter
            if patience <= iter:
                done_looping = True
                print patience , iter
                break

    end_time = time.clock()
    print >> sys.stderr, ('The code for file ' +
                          os.path.split(__file__)[1] +
                          ' ran for %.2fm' % ((end_time - start_time) / 60.))
    print >> output_file, ('Optimization complete.')
    print >> output_file, ('Best validation score of %f %% obtained at iteration %i,'\
          'with test performance %f %%' %
          (best_validation_loss * 100., best_iter + 1, test_score * 100.))
          
    output_file.close()
Esempio n. 4
0
def train_by_lenet5(tr_start_index,
                    tr_limit,
                    vl_start_index,
                    vl_limit,
                    output_filename="tmp.file",
                    learning_rate=0.13,
                    n_epochs=5000):

    global train_dataset_route
    global valid_dataset_route

    output_file = open(output_filename, 'w')

    print train_dataset_route, type(train_dataset_route)
    """
    :type learning_rate: float
    :param learning_rate: learning rate used (factor for the stochastic
                          gradient)

    :type n_epochs: int
    :param n_epochs: maximal number of epochs to run the optimizer

    :type nkerns: list of ints
    :param nkerns: number of kernels on each layer
    """

    train_set = tdtf.read_data_patch_to_ndarray(train_dataset_route,
                                                tr_start_index, tr_limit)
    datasets = load_data.shared_dataset(train_set)
    train_set_x, train_set_y = datasets

    valid_set = tdtf.read_data_patch_to_ndarray(valid_dataset_route,
                                                vl_start_index, vl_limit)
    print valid_set[1]
    datasets = load_data.shared_dataset(valid_set)
    valid_set_x, valid_set_y = datasets

    # compute number of minibatches for training, validation and testing
    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_train_batches /= batch_size
    n_valid_batches /= batch_size

    # allocate symbolic variables for the data

    ######################
    # BUILD ACTUAL MODEL #
    ######################
    print '... building the model'

    # the cost we minimize during training is the NLL of the model
    cost = layer3.negative_log_likelihood(y)

    # create a function to compute the mistakes that are made by the model
    validate_model = theano.function(
        [index],
        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]
        })

    # create a list of all model parameters to be fit by gradient descent
    params = layer3.params + layer2.params + layer1.params + layer0.params

    # create a list of gradients for all model parameters
    grads = T.grad(cost, params)

    # train_model is a function that updates the model parameters by
    # SGD Since this model has many parameters, it would be tedious to
    # manually create an update rule for each model parameter. We thus
    # create the updates list by automatically looping over all
    # (params[i],grads[i]) pairs.
    updates = []
    for param_i, grad_i in zip(params, grads):
        updates.append((param_i, param_i - learning_rate * grad_i))

    train_model = theano.function(
        [index], [cost, layer3.errors(y), layer3.params[0][0][0]],
        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]
        })

    ###############
    # TRAIN MODEL #
    ###############
    print '... training'
    # early-stopping parameters
    patience = 50000  # look as this many examples regardless
    patience_increase = 2  # wait this much longer when a new best is
    # found
    improvement_threshold = 0.995  # a relative improvement of this much is
    # considered significant
    validation_frequency = min(n_train_batches, patience / 2)
    # go through this many
    # minibatche before checking the network
    # on the validation set; in this case we
    # check every epoch

    best_params = None
    best_validation_loss = numpy.inf
    best_iter = 0
    test_score = 0.
    start_time = time.clock()

    epoch = 0
    done_looping = False

    min_train_cost = 10000
    decreasing_num = 0

    last_train_err = 1
    last_train_cost = 1

    while (epoch < n_epochs) and (not done_looping):
        epoch = epoch + 1
        for minibatch_index in xrange(n_train_batches):

            iter = (epoch - 1) * n_train_batches + minibatch_index

            if iter % 100 == 0:
                print 'training @ iter = ', iter, ' patience = ', patience
            cost_ij, train_err, par = train_model(minibatch_index)

            decreasing_rate = (last_train_err -
                               train_err) / (last_train_err) * 100.
            last_train_err = train_err
            if last_train_err == 0:
                last_train_err += 0.0000001
            c_d_rate = (last_train_cost - cost_ij) / (last_train_cost) * 100.
            last_train_cost = cost_ij
            print >> output_file, ('epoch %i, minibatch %i/%i, train_cost %f , train_error %.2f %%, decreasing rate %f %%, cost_decreasing rate %f %%, W00 ' % \
                (epoch, minibatch_index + 1, n_train_batches,
                cost_ij,
                train_err* 100.
                ,decreasing_rate
                ,c_d_rate))

            #print layer1.params[0:1][0][0:3]
            #print layer2.params[0:1][0][0:3]
            if cost_ij < min_train_cost:
                decreasing_num = 0
                min_train_cost = cost_ij
                layer0_state = layer0.__getstate__()
                layer1_state = layer1.__getstate__()
                layer2_state = layer2.__getstate__()
                layer3_state = layer3.__getstate__()
                trained_model_list = [
                    layer0_state, layer1_state, layer2_state, layer3_state
                ]
                trained_model_array = numpy.asarray(trained_model_list)
                classifier_file = open(train_model_route, 'w')
                cPickle.dump([1, 2, 3], classifier_file, protocol=2)
                numpy.save(classifier_file, trained_model_array)
                classifier_file.close()
            else:
                print "decreasing"
                decreasing_num += 1
                if decreasing_num > 100:
                    done_looping = True
                    break
            if (iter + 1) % validation_frequency == 0:

                # compute zero-one loss on validation set
                validation_losses = [
                    validate_model(i) for i in xrange(n_valid_batches)
                ]
                this_validation_loss = numpy.mean(validation_losses)
                print >> output_file, ('epoch %i, minibatch %i/%i, validation error %f %%' % \
                      (epoch, minibatch_index + 1, n_train_batches, \
                       this_validation_loss * 100.))

                # if we got the best validation score until now
                if this_validation_loss < best_validation_loss:

                    #improve patience if loss improvement is good enough
                    if this_validation_loss < best_validation_loss *  \
                       improvement_threshold:
                        patience = max(patience, iter * patience_increase)

                    # save best validation score and iteration number
                    best_validation_loss = this_validation_loss
                    best_iter = iter
            if patience <= iter:
                done_looping = True
                print patience, iter
                break

    end_time = time.clock()
    print >> sys.stderr, ('The code for file ' + os.path.split(__file__)[1] +
                          ' ran for %.2fm' % ((end_time - start_time) / 60.))
    print >> output_file, ('Optimization complete.')
    print >> output_file, ('Best validation score of %f %% obtained at iteration %i,'\
          'with test performance %f %%' %
          (best_validation_loss * 100., best_iter + 1, test_score * 100.))

    output_file.close()
Esempio n. 5
0
def sgd_optimization_mnist(tr_start_index=1, tr_limit=5000, vl_start_index=1, vl_limit=5000,
                           learning_rate=0.015, n_epochs=5000
                           , output_filename="ls.out"):

    output_file = open(output_filename,'w')
    # allocate symbolic variables for the data
    index = T.lscalar()  # index to a [mini]batch
    x = T.matrix('x')  # the data is presented as rasterized images
    y = T.ivector('y')  # the labels are presented as 1D vector of
                               # [int] labels
    in_shape = layer0_input_shape[0] * layer0_input_shape[1]

    batch_size = tr_limit
    train_set = tdtf.read_data_patch_to_ndarray(train_dataset_route, tr_start_index, tr_limit)
    datasets = load_data.shared_dataset(train_set)
    train_set_x, train_set_y = datasets

    valid_set = tdtf.read_data_patch_to_ndarray(valid_dataset_route, vl_start_index, vl_limit)
    print valid_set[1]
    datasets = load_data.shared_dataset(valid_set)
    valid_set_x, valid_set_y = datasets

    # compute number of minibatches for training, validation and testing
    n_train_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size
    n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] / batch_size
    #n_test_batches = test_set_x.get_value(borrow=True).shape[0] / batch_size

    if not if_load_trained_model :
        trained_model_pkl = open(train_model_route, 'r')
        trained_model_state_list = cPickle.load(trained_model_pkl)
        trained_model_state_array = numpy.load(trained_model_pkl)
        classifier_state = trained_model_state_array[0]

        classifier = LogisticRegression(input=x, n_in=in_shape, n_out=layer0_output_shape
                                        , W=classifier_state[0], b=classifier_state[1])

    else:

        ######################
        # BUILD ACTUAL MODEL #
        ######################
        #print '... building the model'

        # construct the logistic regression class
        rng = numpy.random.RandomState(23555)
        W_bound=1
        tmp_W = theano.shared(numpy.asarray(
                rng.uniform(low=0, high=W_bound, size=(in_shape, layer0_output_shape)), dtype=theano.config.floatX),
                borrow=True)
        classifier = LogisticRegression(input=x, n_in=in_shape, n_out=layer0_output_shape)
                                    #,W=tmp_W)

    # the cost we minimize during training is the negative log likelihood of
    # the model in symbolic format
    cost = classifier.negative_log_likelihood(y)

    # compiling a Theano function that computes the mistakes that are made by
    # the model on a minibatch
    validate_model = theano.function(inputs=[index],
            outputs=classifier.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]})

    # compute the gradient of cost with respect to theta = (W,b)
    g_W = T.grad(cost=cost, wrt=classifier.W)
    g_b = T.grad(cost=cost, wrt=classifier.b)

    # specify how to update the parameters of the model as a list of
    # (variable, update expression) pairs.
    updates = [(classifier.W, classifier.W - learning_rate * g_W),
               (classifier.b, classifier.b - learning_rate * g_b)]

    # compiling a Theano function `train_model` that returns the cost, but in
    # the same time updates the parameter of the model based on the rules
    # defined in `updates`
    train_model = theano.function(inputs=[index], \
            outputs=[cost, classifier.errors(y)], \
            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]})

    ###############
    # TRAIN MODEL #
    ###############
    #print '... training the model'
    # early-stopping parameters
    patience = 50000  # look as this many examples regardless
    patience_increase = 2  # wait this much longer when a new best is
                                  # found
    improvement_threshold = 0.995  # a relative improvement of this much is
                                  # considered significant
    validation_frequency = min(n_train_batches, patience / 2)
                                  # go through this many
                                  # minibatche before checking the network
                                  # on the validation set; in this case we
                                  # check every epoch

    best_params = None
    best_validation_loss = numpy.inf
    test_score = 0.
    start_time = time.clock()
    best_train_loss = numpy.inf

    done_looping = False
    epoch = 0

    last_train_err = 1
    last_train_cost = 1

    while (epoch < n_epochs) and (not done_looping):
        epoch = epoch + 1
        for minibatch_index in xrange(n_train_batches):

            minibatch_avg_cost, train_err = train_model(minibatch_index)
            decreasing_rate = (last_train_err - train_err) / (last_train_err) * 100.
            last_train_err = train_err
            c_d_rate = (last_train_cost - minibatch_avg_cost) / (last_train_cost) * 100.
            last_train_cost = minibatch_avg_cost
            print >> output_file, ('epoch %i, minibatch %i/%i, train_cost %f , train_error %.2f %%, decreasing rate %f %%, cost_decreasing rate %f %%' % \
                (epoch, minibatch_index + 1, n_train_batches,
                minibatch_avg_cost,
                train_err* 100.
                ,decreasing_rate
                ,c_d_rate))

            if best_train_loss > train_err:
                best_train_loss = train_err

            # iteration number
            iter = (epoch - 1) * n_train_batches + minibatch_index

            if (iter + 1) % validation_frequency == 0:
                # compute zero-one loss on validation set
                validation_losses = [validate_model(i)
                                     for i in xrange(n_valid_batches)]
                this_validation_loss = numpy.mean(validation_losses)

                print >> output_file, ('epoch %i, minibatch %i/%i, validation error %f %%' % \
                    (epoch, minibatch_index + 1, n_train_batches,
                    this_validation_loss * 100.))

                # if we got the best validation score until now
                if this_validation_loss < best_validation_loss:
                    #improve patience if loss improvement is good enough
                    if this_validation_loss < best_validation_loss *  \
                       improvement_threshold:
                        patience = max(patience, iter * patience_increase)

                    best_validation_loss = this_validation_loss

                    # load trained_model to 
                    '''
                    layer_state = classifier.__getstate__()
                    trained_model_list = [layer_state]
                    trained_model_array = numpy.asarray(trained_model_list)
                    classifier_file = open(train_model_route, 'w')
                    cPickle.dump([1,2,3], classifier_file, protocol=2)
                    numpy.save(classifier_file, trained_model_array)
                    classifier_file.close()
                    '''
                    '''
                    test_losses = [test_model(i)
                                   for i in xrange(n_test_batches)]
                    test_score = numpy.mean(test_losses)
                    test_res = [test_results(i)
                                   for i in xrange(n_test_batches)]

                    print(('     epoch %i, minibatch %i/%i, test error of best'
                       ' model %f %%') %
                        (epoch, minibatch_index + 1, n_train_batches,
                         test_score * 100.))
                     '''

            if patience <= iter:
                done_looping = True
                break

    end_time = time.clock()
    print >> output_file, (('Optimization complete with best validation score of %f %%,'
           'with test performance %f %%'
           'with best train_performance %f %%') %
                 (best_validation_loss * 100., test_score * 100., best_train_loss * 100.))
    print >> output_file, 'The code run for %d epochs, with %f epochs/sec' % (
        epoch, 1. * epoch / (end_time - start_time))
    print >> sys.stderr, ('The code for file ' +
                          os.path.split(__file__)[1] +
                          ' ran for %.1fs' % ((end_time - start_time)))
    output_file.close()