Beispiel #1
0
def main(_):
    if FLAGS.model == 'dmn':
        word2vec = load_glove(FLAGS.glove_size)
        words = WordTable(word2vec, FLAGS.glove_size)
        from models.old.dmn import DMN

    elif FLAGS.model == 'dmn+':
        words = WordTable()
        from models.new.dmn_plus import DMN

    elif FLAGS.model == 'mc_dmn+':
        word2vec = load_glove(FLAGS.glove_size)
        words = WordTable(word2vec, FLAGS.glove_size)
        from models.new.mc_dmn_plus import DMN

    elif FLAGS.model == 'dmn_embed':
        words = WordTable()
        from models.old.dmn_embedding import DMN
    else:
        print('Unknown model type: %s' % FLAGS.model)
        return

    # Read data
    train = read_babi(FLAGS.data_dir, FLAGS.task, 'train', FLAGS.batch_size,
                      words)
    test = read_babi(FLAGS.data_dir, FLAGS.task, 'test', FLAGS.batch_size,
                     words)
    val = train.split_dataset(FLAGS.val_ratio)

    FLAGS.max_sent_size, FLAGS.max_ques_size, FLAGS.max_fact_count = get_max_sizes(
        train, test, val)
    print('Word count: %d, Max sentence len : %d' %
          (words.vocab_size, FLAGS.max_sent_size))

    # Modify save dir
    FLAGS.save_dir += '/task_%d/' % FLAGS.task
    if not os.path.exists(FLAGS.save_dir):
        os.makedirs(FLAGS.save_dir)  #, exist_ok=True)

    with tf.Session() as sess:
        model = DMN(FLAGS, words)
        sess.run(tf.initialize_all_variables())

        if FLAGS.test:
            model.load(sess)
            model.eval(sess, test, name='Test')
        else:
            if FLAGS.load: model.load(sess)
            model.train(sess, train, val)
Beispiel #2
0
def main(_):
    train_ds, test_ds = read_data.read_babi(FLAGS.batch_size, FLAGS.data_dir,
                                            FLAGS.task)
    train_ds, val_ds = read_data.split_val(train_ds, FLAGS.val_ratio)
    train_ds.name, val_ds.name, test_ds.name = 'train', 'val', 'test'
    FLAGS.vocab_size = test_ds.vocab_size
    FLAGS.max_sent_size, FLAGS.max_ques_size = read_data.get_max_sizes(
        train_ds, val_ds, test_ds)
    # FIXME : adhoc for now!
    FLAGS.max_sent_size = max(FLAGS.max_sent_size, FLAGS.max_ques_size)
    FLAGS.train_num_batches = train_ds.num_batches
    FLAGS.val_num_batches = val_ds.num_batches
    FLAGS.test_num_batches = test_ds.num_batches
    if not os.path.exists(FLAGS.save_dir):
        os.mkdir(FLAGS.save_dir)

    if FLAGS.linear_start:
        FLAGS.num_epochs = FLAGS.ls_num_epochs
        FLAGS.init_lr = FLAGS.ls_init_lr

    if FLAGS.draft:
        FLAGS.num_layers = 1
        FLAGS.num_epochs = 1
        FLAGS.eval_period = 1
        FLAGS.ls_duration = 1
        FLAGS.train_num_batches = 1
        FLAGS.test_num_batches = 1
        FLAGS.save_period = 1

    pprint(FLAGS.__flags)

    graph = tf.Graph()
    model = N2NModel(graph, FLAGS)
    with tf.Session(graph=graph) as sess:
        sess.run(tf.initialize_all_variables())
        if FLAGS.train:
            writer = tf.train.SummaryWriter(FLAGS.log_dir, sess.graph)
            if FLAGS.load:
                model.load(sess)
            model.train(sess, writer, train_ds, val_ds)
        else:
            model.load(sess)
            model.eval(sess, test_ds)
Beispiel #3
0
def main(_):
    train_ds, test_ds = read_data.read_babi(FLAGS.batch_size, FLAGS.data_dir, FLAGS.task)
    train_ds, val_ds = read_data.split_val(train_ds, FLAGS.val_ratio)
    train_ds.name, val_ds.name, test_ds.name = 'train', 'val', 'test'
    FLAGS.vocab_size = test_ds.vocab_size
    FLAGS.max_sent_size, FLAGS.max_ques_size = read_data.get_max_sizes(train_ds, val_ds, test_ds)
    # FIXME : adhoc for now!
    FLAGS.max_sent_size = max(FLAGS.max_sent_size, FLAGS.max_ques_size)
    FLAGS.train_num_batches = train_ds.num_batches
    FLAGS.val_num_batches = val_ds.num_batches
    FLAGS.test_num_batches = test_ds.num_batches
    if not os.path.exists(FLAGS.save_dir):
        os.mkdir(FLAGS.save_dir)

    if FLAGS.linear_start:
        FLAGS.num_epochs = FLAGS.ls_num_epochs
        FLAGS.init_lr = FLAGS.ls_init_lr

    if FLAGS.draft:
        FLAGS.num_layers = 1
        FLAGS.num_epochs = 1
        FLAGS.eval_period = 1
        FLAGS.ls_duration = 1
        FLAGS.train_num_batches = 1
        FLAGS.test_num_batches = 1
        FLAGS.save_period = 1

    pprint(FLAGS.__flags)

    graph = tf.Graph()
    model = N2NModel(graph, FLAGS)
    with tf.Session(graph=graph) as sess:
        sess.run(tf.initialize_all_variables())
        if FLAGS.train:
            writer = tf.train.SummaryWriter(FLAGS.log_dir, sess.graph)
            if FLAGS.load:
                model.load(sess)
            model.train(sess, writer, train_ds, val_ds)
        else:
            model.load(sess)
            model.eval(sess, test_ds)
Beispiel #4
0
def main(_):
    if FLAGS.model == 'dmn':
        word2vec = load_glove(FLAGS.glove_size)
        words = WordTable(word2vec, FLAGS.glove_size)
        from models.old.dmn import DMN

    elif FLAGS.model == 'dmn+':
        words = WordTable()
        from models.new.dmn_plus import DMN

    elif FLAGS.model == 'dmn_embed':
        words = WordTable()
        from models.old.dmn_embedding import DMN
    else:
        print('Unknown model type: %s' % FLAGS.model)
        return

    # Read data
    train = read_babi(FLAGS.data_dir, FLAGS.task, 'train', FLAGS.batch_size, words)
    test = read_babi(FLAGS.data_dir, FLAGS.task, 'test', FLAGS.batch_size, words)
    val = train.split_dataset(FLAGS.val_ratio)

    FLAGS.max_sent_size, FLAGS.max_ques_size, FLAGS.max_fact_count = get_max_sizes(train, test, val)
    print('Word count: %d, Max sentence len : %d' % (words.vocab_size, FLAGS.max_sent_size))

    # Modify save dir
    FLAGS.save_dir += '/task_%d/' % FLAGS.task
    if not os.path.exists(FLAGS.save_dir):
        os.makedirs(FLAGS.save_dir, exist_ok=True)

    with tf.Session() as sess:
        model = DMN(FLAGS, words)
        sess.run(tf.initialize_all_variables())

        if FLAGS.test:
            model.load(sess)
            model.eval(sess, test, name='Test')
        else:
            if FLAGS.load: model.load(sess)
            model.train(sess, train, val)