예제 #1
0
def main():
    dataloader = IMDB()
    params = {
        'vocab_size': len(dataloader.word2idx),
        'word2idx': dataloader.word2idx,
        'idx2word': dataloader.idx2word,
    }
    model = VRAE(params)

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

    print("Loading trained model ...")
    tf.train.Saver().restore(sess, './saved/vrae.ckpt')

    # lowercase, no punctuation, please
    model.customized_reconstruct(
        sess, 'i love this firm and it is beyond my expectation')
    model.customized_reconstruct(
        sess, 'i want to watch this movie again because it is so interesting')
    model.customized_reconstruct(
        sess, 'the time taken to develop the characters is quite long')
    model.customized_reconstruct(
        sess, 'is there any point to make a bad movie like this')
    model.customized_reconstruct(
        sess, 'sorry but there is no point to watch this movie again')
    model.customized_reconstruct(
        sess, 'to be honest this movie is not worth my time and money')
예제 #2
0
def main():
    dataloader = IMDB()
    model = Model(dataloader.params)

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

    n_batch = len(dataloader.enc_inp) // args.batch_size

    if not os.path.isfile(model.model_path + '.meta'):
        for epoch in range(args.stage_1_num_epochs):
            dataloader.update_word_dropout()
            print("Word Dropout")
            dataloader.shuffle()
            print("Data Shuffled")
            print()
            model.prior_inference(sess)
            model.post_inference(sess,
                                 'i love this film it is so good to watch')
            for i, (enc_inp, dec_inp, dec_out,
                    _) in enumerate(dataloader.next_batch()):

                log = model.train_vae_session(sess, enc_inp, dec_inp, dec_out)
                if i % args.stage_1_display_step == 0:
                    print("Step %d | Train VAE | [%d/%d] | [%d/%d]" %
                          (log['step'], epoch + 1, args.stage_1_num_epochs, i,
                           n_batch),
                          end='')
                    print(" | nll_loss:%.1f | kl_w:%.2f | kl_loss:%.2f" %
                          (log['nll_loss'], log['kl_w'], log['kl_loss']))
                    print()
        save_path = model.saver.save(sess, model.model_path)
        print("Model saved in file: %s" % save_path)
    else:
        print("Stage 1 Completed")
예제 #3
0
파일: train.py 프로젝트: codeaudit/finch
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)
예제 #4
0
파일: train.py 프로젝트: mieitza/finch
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)
예제 #5
0
def main():
    dataloader = IMDB()
    model = Model(dataloader.params)

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

    n_batch = len(dataloader.enc_inp) // args.batch_size
    
    print("Loading trained model ...")
    model.saver.restore(sess, model.model_path)

    for epoch in range(args.stage_2_num_epochs):
        dataloader.update_word_dropout()
        print("Word Dropout")
        dataloader.shuffle()
        print("Data Shuffled")
        print()
        for i, (enc_inp, dec_inp, dec_out, labels) in enumerate(dataloader.next_batch()):

            log = model.train_discriminator_session(sess, enc_inp, dec_inp, dec_out, labels)
            if i % args.stage_2_display_step == 0:
                print("------------")
                print("Step %d | Train Discriminator | [%d/%d] | [%d/%d]" % (
                    log['step'], epoch+1, args.stage_2_num_epochs, i, n_batch))
                print("\t| clf_loss:%.2f | clf_acc:%.2f | L_u: %.2f" % (
                    log['clf_loss'], log['clf_acc'], log['L_u']))
                print()
            
            log = model.train_encoder_session(sess, enc_inp, dec_inp, dec_out)
            if i % args.stage_2_display_step == 0:
                print("Step %d | Train Encoder | [%d/%d] | [%d/%d]" % (
                    log['step'], epoch+1, args.stage_2_num_epochs, i, n_batch))
                print("\t| seq_loss:%.1f | kl_w:%.2f | kl_loss:%.2f" % (
                    log['nll_loss'], log['kl_w'], log['kl_loss']))
                print()
            
            log = model.train_generator_session(sess, enc_inp, dec_inp, dec_out)
            if i % args.stage_2_display_step == 0:
                print("Step %d | Train Generator | [%d/%d] | [%d/%d]" % (
                    log['step'], epoch+1, args.stage_2_num_epochs, i, n_batch))
                print("\t| seq_loss:%.1f | kl_w:%.2f | kl_loss:%.2f" % (
                    log['nll_loss'], log['kl_w'], log['kl_loss']))
                print("\t| temperature:%.2f | l_attr_z:%.2f | l_attr_c:%.2f" % (
                    log['temperature'], log['l_attr_z'], log['l_attr_c']))
                print("------------")
            
            if i % (5 * args.stage_2_display_step) == 0:
                model.post_inference(sess, 'i love this film it is so good to watch')
                model.post_inference(sess, 'this movie is horrible and waste my time')
        save_path = model.saver.save(sess, './saved_temp/model.ckpt')
        print("Model saved in file: %s" % save_path)
예제 #6
0
파일: test.py 프로젝트: zwjyyc/finch
def main():
    dataloader = IMDB()
    model = Model(dataloader.params)

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

    print("Loading trained model ...")
    model.saver.restore(sess, './saved_temp/model.ckpt')

    # lowercase, no punctuation, please
    model.post_inference(sess, "i love this film it is one of the best")
    model.post_inference(sess, "this film is awful and the acting is bad")
    model.post_inference(
        sess, "i hate this boring movie and there is no point to watch")
예제 #7
0
파일: test.py 프로젝트: mieitza/finch
def main():
    dataloader = IMDB()
    model = VRAE(dataloader.word2idx)

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

    print("Loading trained model ...")
    model.saver.restore(sess, model.model_path)

    # lowercase, no punctuation, please
    model.customized_reconstruct(sess,
                                 'i love this firm it is one of the best')
    model.customized_reconstruct(
        sess, 'i want to see this movie it seems interesting')
예제 #8
0
파일: train.py 프로젝트: xzhren/VAE2Seq
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)
예제 #9
0
파일: train.py 프로젝트: zhangjiekui/finch
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)
예제 #10
0
# CONSTANT
VOCAB_SIZE = 5000
MAX_SEQ_LEN = 100
SEED = 1234

gpu = tf.config.experimental.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(gpu[0], True)  # gpu name: /GPU:0

model = RNN(MAX_SEQ_LEN, VOCAB_SIZE)
model.summary()
model.compile(loss='binary_crossentropy',
              optimizer=RMSprop(),
              metrics=['accuracy'])

imdb = IMDB(MAX_SEQ_LEN, VOCAB_SIZE)

if __name__ == '__main__':
    # people can find their devices using tf.config.list_physical_devices("GPU") # "CPU"
    with tf.device("/GPU:0"):
        # Model Training
        model.fit(
            imdb.X_train,
            imdb.Y_train,
            batch_size=512,
            epochs=10,
            validation_split=0.2,
            callbacks=[EarlyStopping(patience=2, verbose=1)],
        )

        # Run model on test set