def main(_):
    data_path = 'data/new-dataset-cornell-length10-filter1-vocabSize40000.pkl'
    word2id, id2word, trainingSamples = load_dataset(data_path)
    hparam = Config()
    hparam.is_training = False

    with tf.Session() as sess:

        model = Seq2SeqModel(hparam, word2id)
        ckpt = tf.train.get_checkpoint_state(hparam.save_path)
        if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path):
            print("Restoring model parameters from %s." %
                  ckpt.model_checkpoint_path)
            model.saver.restore(sess, ckpt.model_checkpoint_path)
        else:
            print("Creating model with fresh parameters.")
            sess.run(model.init)

        sys.stdout.write("> ")
        sys.stdout.flush()
        sentence = sys.stdin.readline()
        while sentence:
            batch = sentence_preprocess(sentence, word2id)
            outputs = model.infer_session(sess, batch)

            predicted_ids = outputs["predicted_ids"]
            out_sents = [id2word[idx] for idx in predicted_ids[0][0].tolist()]
            print(" ".join(out_sents))
            print("> ", "")
            sys.stdout.flush()
            sentence = sys.stdin.readline()
Example #2
0
def main(_):
    hparam = Config()
    dataloader = IMDB(hparam.batch_size, hparam.seq_len, hparam.max_vocab_size,
                      hparam.index_from)
    hparam.vocab_size = dataloader.vocab_size
    model = RNNTextClassification(hparam)

    with tf.Session() as sess:

        model.saver.restore(sess, hparam.save_path + "/model.ckpt")
        x_batch_test, y_batch_test = dataloader.testX[:5], dataloader.testY[:5]
        seq_len_test = np.array([list(x).index(0) + 1 for x in x_batch_test])

        alphas_test = sess.run(model.alphas,
                               feed_dict={
                                   model.inputs: x_batch_test,
                                   model.targets: y_batch_test,
                                   model.lengths: seq_len_test,
                                   model.keep_prob: 1.0
                               })

        word_list = []
        for i in range(5):
            words = list(map(dataloader.index_to_word.get, x_batch_test[i]))
            word_list.append(words)

        # Save visualization as HTML
        with open("visualization.html", "w") as html_file:
            for i in range(5):
                words = word_list[i]
                alphas_values = alphas_test[i]
                for word, alpha in zip(words,
                                       alphas_values / alphas_values.max()):
                    if word == "<sos>":
                        continue
                    elif word == "<pad>":
                        break
                    html_file.write(
                        '<font style="background: rgba(255, 255, 0, %f)">%s</font>\n'
                        % (alpha, word))
                html_file.write("</br></br>")
def prep_config(dataset, size, channel, local=''):

    config = Config.from_file(f'configs/{dataset}_config_ae.py')

    if local:
        global DATADIR
        config.LOGDIR = local
        if 'stl10' in dataset:
            config.dataset['folder'] = DATADIR
        else:
            config.dataset['folder'] = os.path.join(DATADIR, dataset)
        config.validationset['folder'] = config.dataset['folder']

    config.model['channels'][-1] = channel
    config.LOGDIR += f'{size}/{channel}/ae'
    config.superfluous_strides = int(log2(size / min(config.sizes)))
    return config
Example #4
0
        modification = getattr(config, modification)
        trainer = modification(trainer)

    # validation
    validation_loader = DataLoader(config.DATASET(**config.validationset),
                                   **config.dataloader)
    sample = next(iter(validation_loader))
    sample = [s[0:config.n_samples] for s in sample]

    # setup monitoring
    trainer.register_event_handler(events.EACH_EPOCH,
                                   trainer,
                                   name='sample',
                                   sample=sample)
    trainer.register_event_handler(events.EACH_EPOCH,
                                   trainer.validate,
                                   dataloader=validation_loader)
    trainer.register_event_handler(events.EACH_EPOCH,
                                   EventSave(),
                                   interval=config.save_interval)

    # train classifier
    trainer.train(n_epochs=config.n_epochs, resume=True)


if __name__ == '__main__':

    config_file = sys.argv[1]
    config = Config.from_file(config_file)
    run_training(config)
            config.dataset['folder'] = DATADIR
        else:
            config.dataset['folder'] = os.path.join(DATADIR, dataset)
        config.validationset['folder'] = config.dataset['folder']

    config.model['channels'][-1] = channel
    config.LOGDIR += f'{size}/{channel}/ae'
    config.superfluous_strides = int(log2(size / min(config.sizes)))
    return config


DATADIR = ''  # directory containing the
LOCAL = os.getcwd() + '/'
DATASETS = ['pokemon', 'stl10', 'celeba']
DATASET = DATASETS[2]
SIZES = [(6, [48])
         ]  # used to run experiments on ly for a certain shape instead of all
SIZES = SIZES or Config.from_file(
    f'configs/{DATASET}_config_ae.py').sizes.items()

if __name__ == '__main__':

    for size, channels in SIZES:
        for channel in channels:

            config = prep_config(DATASET, size, channel, LOCAL)
            print('running', size, channel)
            config.save('temp_config.py', False)
            config = Config.from_file('temp_config.py')
            run_training(config)
from trainer import Trainer, events, Config
from trainer.handlers import EventSave
from examples.dummy_components import MNIST
from torch.utils.data import DataLoader

config = Config.from_file('dummy_config.py')
trainer = Trainer.from_config(config, altered=True)  # altered is set as a test to see if Config.dump works
sample = next(iter(trainer.dataloader))
validation_loader = DataLoader(MNIST('validation.pt'))

# register handlers for events (see in trainer.events and trainer.handlers)
trainer.register_event_handler(events.EACH_STEP, trainer, name='inference', interval=10, sample=sample)
trainer.register_event_handler(events.EACH_EPOCH, trainer.validate, dataloader=validation_loader)
trainer.register_event_handler(events.EACH_EPOCH, EventSave())

trainer.monitor(name='model.activation')  # register monitoring for parts of the trainer. Return values will be stored

trainer.train(n_steps=4)
Example #7
0
from trainer import Trainer, events, Config
from trainer.handlers import EventSave
from optimizer import OptimizerCollection
import torch as pt
from functools import partial
from itertools import chain

pt.cuda.set_device(0)

print('initializing trainer...', end='')
trainer = Trainer.from_config_file('config.py', False)

config = Config.from_file('config.py')

gen_optimizer = config.OPTIMIZER(
    chain(trainer.model.generator['hn'].parameters(),
          trainer.model.generator['ln'].parameters()), **config.optimizer)
disc_optimizer = config.OPTIMIZER(trainer.model.discriminator.parameters(),
                                  **config.optimizer)
optimizers = []
for optimizer in (disc_optimizer, gen_optimizer):
    if hasattr(config, 'APEX'):
        optimizer = config.APEX(optimizer, **config.apex)
    optimizers.append(optimizer)
trainer.optimizer = OptimizerCollection(*optimizers)
trainer.backward_pass = trainer.optimizer.backward

sample = next(iter(trainer.dataloader))[:4]


def sample_inference(trainer, part, sample, ind, *args, **kwargs):