Exemple #1
0
def main():
    dataloader = IMDB()
    model = VRAE(dataloader.word2idx, dataloader.idx2word)

    sess = tf.Session()
    sess.run(tf.global_variables_initializer())

    for epoch in range(args.num_epoch):
        dataloader.update_word_dropout()
        print("\nWord Dropout")
        dataloader.shuffle()
        print("Data Shuffled", end='\n\n')
        for i, (enc_inp, dec_inp, dec_out) in enumerate(dataloader.next_batch()):
            log = model.train_session(sess, enc_inp, dec_inp, dec_out)
            if i % args.display_loss_step == 0:
                print("Step %d | [%d/%d] | [%d/%d]" % (log['step'], epoch+1, args.num_epoch, i, len(dataloader.enc_inp)//args.batch_size), end='')
                print(" | nll_loss:%.1f | kl_w:%.3f | kl_loss:%.2f \n" % (log['nll_loss'], log['kl_w'], log['kl_loss']))
        
        model.reconstruct(sess, enc_inp[-1], dec_inp[-1])
        #model.generate(sess)
        model.customized_reconstruct(sess, 'i love this film and i think it is one of the best films')
        model.customized_reconstruct(sess, 'this movie is a waste of time and there is no point to watch it')
        
        save_path = model.saver.save(sess, model.model_path)
        print("Model saved in file: %s" % save_path)
Exemple #2
0
def main():
    dataloader = IMDB()
    model = VRAE(dataloader.word2idx)

    sess = tf.Session()
    sess.run(tf.global_variables_initializer())

    for epoch in range(args.num_epoch):
        dataloader.update_word_dropout()
        dataloader.shuffle()
        print("\nData Shuffled", end='\n\n')
        for i, (seq, seq_dropped,
                seq_len) in enumerate(dataloader.next_batch()):
            log = model.train_session(sess, seq, seq_dropped, seq_len)
            if i % args.display_loss_step == 0:
                print("Step %d | [%d/%d] | [%d/%d]" %
                      (log['step'], epoch + 1, args.num_epoch, i,
                       len(dataloader._X) // args.batch_size),
                      end='')
                """
                if args.mutinfo_loss:
                    print("\n\tnll_loss:%.1f | kl_w:%.3f | kl_loss:%.2f | temper:%.2f | mutinfo_loss:%.2f \n" % (
                        log['nll_loss'], log['kl_w'], log['kl_loss'], log['temperature'], log['mutinfo_loss']))
                else:
                """
                print(" | nll_loss:%.1f | kl_w:%.3f | kl_loss:%.2f \n" %
                      (log['nll_loss'], log['kl_w'], log['kl_loss']))
        model.reconstruct(sess, seq[-1], seq_dropped[-1])
        model.generate(sess)

        save_path = model.saver.save(sess, model.model_path)
        print("Model saved in file: %s" % save_path)
Exemple #3
0
def main():
    dataloader = IMDB()
    params = {
        'vocab_size': len(dataloader.word2idx),
        'word2idx': dataloader.word2idx,
        'idx2word': dataloader.idx2word,
    }
    print('Vocab Size:', params['vocab_size'])
    model = VRAE(params)
    saver = tf.train.Saver()

    config = tf.ConfigProto(allow_soft_placement=True)
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)
    sess.run(tf.global_variables_initializer())

    for epoch in range(args.num_epoch):
        dataloader.update_word_dropout()
        print("\nWord Dropout")
        dataloader.shuffle()
        print("Data Shuffled", end='\n\n')
        for i, (enc_inp, dec_inp,
                dec_out) in enumerate(dataloader.next_batch()):
            log = model.train_session(sess, enc_inp, dec_inp, dec_out)
            if i % args.display_loss_step == 0:
                print("Step %d | [%d/%d] | [%d/%d]" %
                      (log['step'], epoch + 1, args.num_epoch, i,
                       len(dataloader.enc_inp) // args.batch_size),
                      end='')
                print(" | nll_loss:%.1f | kl_w:%.3f | kl_loss:%.2f \n" %
                      (log['nll_loss'], log['kl_w'], log['kl_loss']))

        model.generate(sess)
        model.reconstruct(sess, enc_inp[-1], dec_inp[-1])
        model.customized_reconstruct(
            sess, 'i love this film and i think it is one of the best films')
        model.customized_reconstruct(
            sess,
            'this movie is a waste of time and there is no point to watch it')

        save_path = saver.save(sess, './saved/vrae.ckpt')
        print("Model saved in file: %s" % save_path)
Exemple #4
0
def main():
    dataloader = IMDB()
    model = VRAE(dataloader.word2idx)

    sess = tf.Session()
    sess.run(tf.global_variables_initializer())

    for epoch in range(args.num_epoch):
        dataloader.update_word_dropout()
        dataloader.shuffle()
        print("\nData Shuffled", end='\n\n')
        for i, (seq, seq_dropped, seq_len) in enumerate(dataloader.next_batch()):
            log = model.train_session(sess, seq, seq_dropped, seq_len)
            if i % args.display_loss_step == 0:
                bar = '[%d/%d] | [%d/%d] | nll_loss: %.1f | kl_w: %.3f | kl_loss: %.1f'
                vars = (epoch+1, args.num_epoch, i+1, len(dataloader._X)//args.batch_size, log['nll_loss'],
                        log['kl_w'], log['kl_loss'])
                print(bar % vars)
            if i % args.display_text_step == 0:
                model.reconstruct(sess, seq[-1], seq_dropped[-1])
                model.generate(sess)
Exemple #5
0
def main():
    os.environ["CUDA_VISIBLE_DEVICES"] = "1"

    args.max_len = 100
    args.batch_size = 64
    args.max_dec_len = 100
    args.display_info_step = 1000
    args.isPointer = False
    args.vocab_limit = 35000
    train_data_len = 209940
    args.diff_input = True
    print(args)

    dataloader = IWSLT(batch_size=args.batch_size, vocab_limit=args.vocab_limit, max_input_len=args.max_len, max_output_len=args.max_dec_len)
    params = {
        'vocab_size_encoder': len(dataloader.idx2token),
        'vocab_size': len(dataloader.word2idx),
        'word2idx': dataloader.word2idx,
        'idx2word': dataloader.idx2word,
        'idx2token': dataloader.idx2token}
    print('Vocab Size:', params['vocab_size'])

    model = VRAE(params)
    saver = tf.train.Saver()
    exp_path = "./saved/iwslt_seq2seq/"
    model_name = "seq2seq.ckpt"

    config = tf.ConfigProto(allow_soft_placement=True)
    config.gpu_options.allow_growth=True
    sess = tf.Session(config=config)
    sess.run(tf.global_variables_initializer())

    EPOCH_STEPS = (train_data_len-1)//args.batch_size+1

    summary_writer = tf.summary.FileWriter(exp_path, sess.graph)
    restore_path = tf.train.latest_checkpoint(exp_path)
    if restore_path:
        saver.restore(sess, restore_path)
        last_train_step = int(restore_path.split("-")[-1]) % EPOCH_STEPS
        print("Model restore from file: %s, last train step: %d" % (restore_path, last_train_step))
    # summary_writer = tf.summary.FileWriter(exp_path)
    # saver.restore(sess, exp_path+model_name)

    for epoch in range(args.num_epoch):
        # dataloader.update_word_dropout()
        # print("\nWord Dropout")
        # dataloader.shuffle()
        # print("Data Shuffled", end='\n\n')
        batcher = dataloader.load_data()

        step = -1
        while True:
            try:
                # enc_inp, dec_inp_full, dec_out = next(dataloader.data_loader)
                (x_enc_inp, x_dec_inp_full, x_dec_out, y_enc_inp, y_dec_inp_full, y_dec_out), x_enc_inp_oovs, data_oovs, _ = next(batcher)
                # enc_inp, dec_inp_full, dec_out = dataloader.next_batch()
                enc_inp, dec_inp_full, dec_out = x_enc_inp, y_dec_inp_full, y_dec_out
                dec_inp = dataloader.update_word_dropout(dec_inp_full)
                step += 1
            except StopIteration:
                print("there are no more examples")
                break
            # print(step, "enc_inp.shape:", enc_inp.shape)
            # print(step, "dec_inp_full.shape:", dec_inp_full.shape)
            # print(step, "dec_out.shape:", dec_out.shape)

            log = model.train_session(sess, enc_inp, dec_inp, dec_out)

            # get the summaries and iteration number so we can write summaries to tensorboard
            summaries, train_step = log['summaries'], log['step']
            summary_writer.add_summary(summaries, train_step) # write the summaries
            if train_step % 100 == 0: # flush the summary writer every so often
                summary_writer.flush()

            if step % args.display_loss_step == 0:
                print("Step %d | [%d/%d] | [%d/%d]" % (log['step'], epoch+1, args.num_epoch, step, train_data_len//args.batch_size), end='')
                print(" | loss:%.3f" % (log['loss']))
        
            if step % args.display_info_step == 0:
                model.reconstruct(sess, enc_inp[-1], dec_out[-1])
                save_path = saver.save(sess, exp_path+model_name, global_step=train_step)
                print("Model saved in file: %s" % save_path)

        model.reconstruct(sess, enc_inp[-1], dec_out[-1])
        save_path = saver.save(sess, exp_path+model_name, global_step=train_step)
        print("Model saved in file: %s" % save_path)