Ejemplo n.º 1
0
def run(model_path, test_path, train_path, settings, batch_size, buffer_size,
        device, model_info, full, confusion):

    model = BaseModel.load(model_path).to(device)
    if model_info:
        print(model)

    if hasattr(model, '_settings'):  # new models should all have _settings
        settings = model._settings
    elif settings:
        with utils.shutup():
            settings = settings_from_file(settings)
    else:
        with utils.shutup():
            settings = load_default_settings()

    # overwrite defaults
    settings.batch_size = batch_size
    settings.buffer_size = buffer_size
    settings.device = device

    trainset = None
    if train_path:
        trainset = Dataset(settings, Reader(settings, train_path),
                           model.label_encoder)

    testset = Dataset(settings, Reader(settings, *test_path),
                      model.label_encoder)

    for task in model.evaluate(testset, trainset).values():
        task.print_summary(full=full, confusion_matrix=confusion)
Ejemplo n.º 2
0
 def setUp(self):
     settings = settings_from_file(testpath)
     settings['batch_size'] = 1
     reader = Reader(settings, settings.input_path)
     label_encoder = MultiLabelEncoder.from_settings(settings)
     insts = label_encoder.fit(line for _, line in reader.readsents())
     self.insts = insts
     self.num_batches = insts // settings.batch_size
     self.data = Dataset(settings, reader, label_encoder)
Ejemplo n.º 3
0
 def setUp(self):
     settings = settings_from_file(testpath)
     settings.tasks = [{
         "name": "lemma",
         "level": "char",
         "decoder": "attentional",
         "settings": {
             "preprocessor": "edit_tree_tuples",
             "target": "lemma"
         }
     }]
     self.settings = settings
Ejemplo n.º 4
0
 def setUp(self):
     settings = settings_from_file(testpath)
     settings.tasks = [{
         "name": "lemma",
         "level": "token",
         "decoder": "linear",
         "settings": {
             "preprocessor": "edit_trees",
             "target": "lemma"
         }
     }]
     self.settings = settings
Ejemplo n.º 5
0
 def setUp(self):
     settings = settings_from_file(testpath)
     settings.tasks = [{
         "name": "lemma",
         "level": "char",
         "decoder": "linear",
         "settings": {
             "preprocessor": "greedy_scripts",
             "target": "lemma"
         }
     }]
     settings.char_eos = False
     settings.char_bos = False
     settings.char_max_size = 10000
     self.settings = settings
Ejemplo n.º 6
0
    def test_batch_level(self):
        settings = settings_from_file(testpath)
        settings['batch_size'] = 20
        reader = Reader(settings, settings.input_path)
        label_encoder = MultiLabelEncoder.from_settings(settings)
        label_encoder.fit(line for _, line in reader.readsents())
        data = Dataset(settings, reader, label_encoder)

        pre_batches = 0
        for batch in data.batch_generator():
            pre_batches += 1

        self.assertAlmostEqual(pre_batches, self.insts // 20, delta=delta)

        devset = data.get_dev_split(self.insts, split=0.05)

        post_batches = 0
        for batch in data.batch_generator():
            post_batches += 1

        self.assertAlmostEqual(pre_batches * 0.95, post_batches, delta=delta)
Ejemplo n.º 7
0
 def setUp(self):
     settings = settings_from_file(testpath)
     reader = Reader(settings, settings.input_path)
     label_encoder = MultiLabelEncoder.from_settings(settings)
     label_encoder.fit_reader(reader)
     self.data = Dataset(settings, reader, label_encoder)
Ejemplo n.º 8
0
import uuid
import torch
import os
import unittest

from pie.models import SimpleModel
from pie.data import MultiLabelEncoder, Reader, Dataset
from pie.settings import settings_from_file

testpath = os.path.join(os.path.dirname(__file__), 'testconfig.json')
settings = settings_from_file(testpath)
label_encoder = MultiLabelEncoder.from_settings(settings)
reader = Reader(settings, settings.input_path)
label_encoder.fit_reader(reader)
dataset = Dataset(settings, label_encoder, reader)


class TestModelSerialization(unittest.TestCase):
    def setUp(self):
        emb_dim, hidden_size, num_layers = 64, 100, 1
        self.model = SimpleModel(label_encoder, emb_dim, emb_dim, hidden_size,
                                 num_layers)

    def test_serialization(self):
        model = self.model
        fid = '/tmp/{}'.format(str(uuid.uuid1()))
        model.save(fid)
        model2 = SimpleModel.load(fid)
        os.remove('{}.tar'.format(fid))
        self.assertEqual(model.label_encoder, model2.label_encoder)
Ejemplo n.º 9
0
    fname = os.path.join(settings.modelpath, settings.modelname)
    # infix
    targets = [t['name'] for t in settings.tasks if t.get('schedule', {}).get('target')]
    timestamp = datetime.now().strftime("%Y_%m_%d-%H_%M_%S")
    infix = '-'.join(['+'.join(targets), timestamp])

    return fname, infix


if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('config_path', nargs='?', default='config.json')
    args = parser.parse_args()

    settings = settings_from_file(args.config_path)

    # datasets
    reader = Reader(settings, settings.input_path)
    tasks = reader.check_tasks(expected=None)
    label_encoder = MultiLabelEncoder.from_settings(settings, tasks=tasks)
    if settings.verbose:
        print("::: Available tasks :::")
        print()
        for task in tasks:
            print("- {}".format(task))
        print()

    # fit
    start = time.time()
    if settings.verbose:
Ejemplo n.º 10
0
        cembs = (1 - alpha).unsqueeze(-1).expand_as(cembs) * cembs

        return wembs + cembs


def EmbeddingConcat():
    def func(wemb, cemb):
        return torch.cat([wemb, cemb], dim=-1)
    return func


if __name__ == '__main__':
    from pie.settings import settings_from_file
    from pie.data import Dataset

    settings = settings_from_file('./config.json')
    data = Dataset(settings)
    ((word, wlen), (char, clen)), tasks = next(data.batch_generator())
    print("lemma", tasks['lemma'][0].size(), tasks['lemma'][1])
    print("char", char.size(), clen)
    print("word", word.size(), wlen)

    emb_dim = 20
    wemb = nn.Embedding(len(data.label_encoder.word), emb_dim)
    cemb = RNNEmbedding(len(data.label_encoder.char), emb_dim)
    cnncemb = CNNEmbedding(len(data.label_encoder.char), emb_dim)

    mixer = EmbeddingMixer(20)
    w, (c, _) = wemb(word), cemb(char, clen, wlen)
    output = mixer(w, c)
Ejemplo n.º 11
0
 def setUp(self):
     settings = settings_from_file(testpath)
     reader = Reader(settings, settings.input_path)
     label_encoder = MultiLabelEncoder.from_settings(settings)
     label_encoder.fit(line for _, line in reader.readsents())
     self.data = Dataset(settings, reader, label_encoder)
Ejemplo n.º 12
0
def run(config_path):
    now = datetime.now()
    seed = now.hour * 10000 + now.minute * 100 + now.second
    print("Using seed:", seed)
    random.seed(seed)
    numpy.random.seed(seed)
    torch.manual_seed(seed)
    if torch.cuda.is_available():
        torch.cuda.manual_seed(seed)

    settings = settings_from_file(config_path)

    # check settings
    # - check at least and at most one target
    has_target = False
    for task in settings.tasks:
        if len(settings.tasks) == 1:
            task['target'] = True
        if task.get('target', False):
            if has_target:
                raise ValueError("Got more than one target task")
            has_target = True
    if not has_target:
        raise ValueError("Needs at least one target task")

    # datasets
    reader = Reader(settings, settings.input_path)
    tasks = reader.check_tasks(expected=None)
    if settings.verbose:
        print("::: Available tasks :::")
        print()
        for task in tasks:
            print("- {}".format(task))
        print()

    # label encoder
    label_encoder = MultiLabelEncoder.from_settings(settings, tasks=tasks)
    if settings.verbose:
        print("::: Fitting data :::")
        print()
    label_encoder.fit_reader(reader)

    if settings.verbose:
        print()
        print("::: Vocabulary :::")
        print()
        types = '{}/{}={:.2f}'.format(*label_encoder.word.get_type_stats())
        tokens = '{}/{}={:.2f}'.format(*label_encoder.word.get_token_stats())
        print("- {:<15} types={:<10} tokens={:<10}".format(
            "word", types, tokens))
        types = '{}/{}={:.2f}'.format(*label_encoder.char.get_type_stats())
        tokens = '{}/{}={:.2f}'.format(*label_encoder.char.get_token_stats())
        print("- {:<15} types={:<10} tokens={:<10}".format(
            "char", types, tokens))
        print()
        print("::: Tasks :::")
        print()
        for task, le in label_encoder.tasks.items():
            print("- {:<15} target={:<6} level={:<6} vocab={:<6}".format(
                task, le.target, le.level, len(le)))
        print()

    trainset = Dataset(settings, reader, label_encoder)

    devset = None
    if settings.dev_path:
        devset = Dataset(settings, Reader(settings, settings.dev_path),
                         label_encoder)
    else:
        logging.warning("No devset: cannot monitor/optimize training")

    # model
    model = SimpleModel(label_encoder,
                        settings.tasks,
                        settings.wemb_dim,
                        settings.cemb_dim,
                        settings.hidden_size,
                        settings.num_layers,
                        dropout=settings.dropout,
                        cell=settings.cell,
                        cemb_type=settings.cemb_type,
                        cemb_layers=settings.cemb_layers,
                        custom_cemb_cell=settings.custom_cemb_cell,
                        linear_layers=settings.linear_layers,
                        scorer=settings.scorer,
                        word_dropout=settings.word_dropout,
                        lm_shared_softmax=settings.lm_shared_softmax,
                        include_lm=settings.include_lm)

    # pretrain(/load pretrained) embeddings
    if model.wemb is not None:
        if settings.pretrain_embeddings:
            print("Pretraining word embeddings")
            wemb_reader = Reader(settings, settings.input_path,
                                 settings.dev_path, settings.test_path)
            weight = get_pretrained_embeddings(wemb_reader,
                                               label_encoder,
                                               size=settings.wemb_dim,
                                               window=5,
                                               negative=5,
                                               min_count=1)
            model.wemb.weight.data = torch.tensor(weight, dtype=torch.float32)

        elif settings.load_pretrained_embeddings:
            print("Loading pretrained embeddings")
            if not os.path.isfile(settings.load_pretrained_embeddings):
                print("Couldn't find pretrained eembeddings in: {}".format(
                    settings.load_pretrained_embeddings))
            initialization.init_pretrained_embeddings(
                settings.load_pretrained_embeddings, label_encoder.word,
                model.wemb)

    # load pretrained weights
    if settings.load_pretrained_encoder:
        model.init_from_encoder(
            pie.Encoder.load(settings.load_pretrained_encoder))

    # freeze embeddings
    if settings.freeze_embeddings:
        model.wemb.weight.requires_grad = False

    model.to(settings.device)

    print("::: Model :::")
    print()
    print(model)
    print()
    print("::: Model parameters :::")
    print()
    trainable = sum(p.nelement() for p in model.parameters()
                    if p.requires_grad)
    total = sum(p.nelement() for p in model.parameters())
    print("{}/{} trainable/total".format(trainable, total))
    print()

    # training
    print("Starting training")

    running_time = time.time()
    trainer = Trainer(settings, model, trainset, reader.get_nsents())
    scores = None
    try:
        scores = trainer.train_epochs(settings.epochs, devset=devset)
    except KeyboardInterrupt:
        print("Stopping training")
    finally:
        model.eval()
    running_time = time.time() - running_time

    if settings.test_path:
        print("Evaluating model on test set")
        testset = Dataset(settings, Reader(settings, settings.test_path),
                          label_encoder)
        for task in model.evaluate(testset, trainset).values():
            task.print_summary()

    # save model
    fpath, infix = get_fname_infix(settings)
    if not settings.run_test:
        fpath = model.save(fpath, infix=infix, settings=settings)
        print("Saved best model to: [{}]".format(fpath))

    if devset is not None and not settings.run_test:
        scorers = model.evaluate(devset, trainset)
        scores = []
        for task in sorted(scorers):
            scorer = scorers[task]
            result = scorer.get_scores()
            for acc in result:
                scores.append('{}:{:.6f}'.format(task,
                                                 result[acc]['accuracy']))
                scores.append('{}-support:{}'.format(task,
                                                     result[acc]['support']))
        path = '{}.results.{}.csv'.format(settings.modelname,
                                          '-'.join(get_targets(settings)))
        with open(path, 'a') as f:
            line = [infix, str(seed), str(running_time)]
            line += scores
            f.write('{}\n'.format('\t'.join(line)))

    print("Bye!")
Ejemplo n.º 13
0
    parser.add_argument('--batch_size', type=int, default=500)
    parser.add_argument('--buffer_size', type=int, default=100000)
    parser.add_argument('--device', default='cpu')
    parser.add_argument('--model_info', action='store_true')
    parser.add_argument('--full', action='store_true')
    args = parser.parse_args()

    model = BaseModel.load(args.model_path).to(args.device)
    if args.model_info:
        print(model)

    if hasattr(model, '_settings'):  # new models should all have _settings
        settings = model._settings
    elif args.settings:
        with utils.shutup():
            settings = settings_from_file(args.settings)
    else:
        with utils.shutup():
            settings = load_default_settings()

    # overwrite defaults
    settings.batch_size = args.batch_size
    settings.buffer_size = args.buffer_size
    settings.device = args.device

    reader = Reader(settings, *args.test_path)
    dataset = Dataset(settings, reader, model.label_encoder)
    dataset = device_wrapper(list(dataset.batch_generator()), args.device)

    for task in model.evaluate(dataset).values():
        task.print_summary(full=args.full)
Ejemplo n.º 14
0
    from pie import utils
    with utils.shutup():        # avoid pattern warning
        from gensim.models import Word2Vec

    word2vec = Word2Vec(reader.get_token_iterator(), **kwargs)
    weight = np.zeros((len(label_encoder.word), word2vec.wv.vector_size))

    found = 0
    for w, idx in label_encoder.word.table.items():
        try:
            weight[idx] = word2vec.wv[w]
            found += 1
        except KeyError:  # reserved symbols are not in training sentences
            pass

    print("A total of {}/{} word embeddings were pretrained"
          .format(found, len(label_encoder.word)))

    return weight


if __name__ == '__main__':
    from pie.data import Reader, MultiLabelEncoder
    from pie.settings import settings_from_file

    settings = settings_from_file("config.json")
    reader = Reader(settings, settings.input_path)
    le = MultiLabelEncoder.from_settings(settings)
    le.fit_reader(reader)
    get_pretrained_embeddings(reader, le, min_count=1)