Exemplo n.º 1
0
def run():

    import sys, getopt, os
    load_from = None

    opts, args = getopt.getopt(sys.argv[1:], "l:")
    for opt, val in opts:
        if opt == "-l": load_from = val

    bg = QuestionsBatchGenerator(args[0], 1000)

    trainig_set_size = bg.training_samples()

    validation_data = bg.validateSet()
    print "Validation set created"
    #print validation_data

    model = createModel(bg.rowSize, 2)
    if load_from: model.load_weights(load_from)

    for i in range(100):
        pair_x, pair_truth = bg.loadPair(i)
        q1, q2, dup_ = pair_truth
        print "Question 1: ", q1
        print "Question 2: ", q2

        y = model.predict(pair_x)[0]
        dup = y[1] > 0.5

        correct = dup == dup_

        print "Same: %s   model: %.3f (%s)" % (dup_, y[1], "CORRECT"
                                               if correct else "incorrect")
        print
Exemplo n.º 2
0
def run():

    import sys, getopt, os
    save_to = None
    load_from = None

    batch_size = 60
    nworkers = 2
    verbose = 1

    opts, args = getopt.getopt(sys.argv[1:], "s:l:n:qw:")
    for opt, val in opts:
        if opt == "-s": save_to = val
        if opt == "-l": load_from = val
        if opt == "-w":
            load_from = val
            save_to = val
        if opt == "-n": nworkers = int(val)
        if opt == "-q": verbose = 0

    os.system("rm logs/*")

    bg = QuestionsBatchGenerator(args[0], 1000)

    print "Row size=", bg.rowSize

    trainig_set_size = bg.training_samples()

    validation_data = bg.validateSet()
    print "Validation set created"
    #print validation_data

    model = createModel(bg.rowSize, 2)

    if load_from:
        model.load_weights(load_from, by_name=True)
        print
        print "model weights loaded from %s" % (load_from, )
        print

    tb = TensorBoard(write_images=False, histogram_freq=1.0)
    callbacks = [tb]
    if save_to:
        callbacks.append(
            ModelCheckpoint(filepath=save_to,
                            verbose=1,
                            save_best_only=True,
                            save_weights_only=True,
                            monitor="val_loss"))

    model.fit_generator(bg.batches_guargded(batch_size),
                        int(trainig_set_size / batch_size / 20),
                        epochs=1000,
                        verbose=verbose,
                        workers=nworkers,
                        callbacks=callbacks,
                        validation_data=validation_data)
Exemplo n.º 3
0
def run():

    import sys, getopt, os
    save_to = None
    load_from = None

    batch_size = 30

    opts, args = getopt.getopt(sys.argv[1:], "s:l:")
    for opt, val in opts:
        if opt == "-s": save_to = val
        if opt == "-l": load_from = val

    os.system("rm logs/*")

    bg = QuestionsBatchGenerator(args[0], 1000)

    trainig_set_size = bg.training_samples()

    validation_data = bg.validateSet()
    print "Validation set created"
    #print validation_data

    model = createModel(bg.rowSize, 2)

    tb = TensorBoard(write_images=False, histogram_freq=1.0)
    callbacks = [tb]
    if save_to:
        callbacks.append(
            ModelCheckpoint(filepath=save_to,
                            verbose=1,
                            save_best_only=True,
                            save_weights_only=True,
                            monitor="val_loss"))

    model.fit_generator(bg.batches_guargded(batch_size),
                        int(trainig_set_size / batch_size / 100),
                        epochs=1000,
                        workers=4,
                        callbacks=callbacks,
                        validation_data=validation_data)