Пример #1
0
def load_best(experiment, parts):
    log_data = parse(log(experiment))
    best_step, _ = max_key(scores(log_data, parts))
    parameters = load_parameters(experiment)

    MODEL_CLASS = load_model(parameters['MODEL_PREFIX'])
    TOKENIZER = MODEL_CLASS[0].from_pretrained(parameters['MODEL_NAME'])
    CONFIG = MODEL_CLASS[1].from_pretrained(parameters['MODEL_NAME'],
                                            num_labels=3)
    MODEL = MODEL_CLASS[2].from_pretrained(parameters['MODEL_NAME'],
                                           config=CONFIG)
    MODEL.load_state_dict(
        torch.load('logs/{}/model_{}.torch'.format(experiment, best_step),
                   map_location='cpu'))

    return best_step, log_data[best_step], MODEL, TOKENIZER
Пример #2
0
    with open(output_dir + '/parameters.json', 'w+') as config_file:
        json.dump({
            'MODEL_PREFIX': MODEL_PREFIX,
            'MODEL_NAME': MODEL_NAME,
            'DATASET': DATASET,
            'LANGS': LANGS,
            'TRAIN_BATCH_SIZE': TRAIN_BATCH_SIZE,
            'ACCUMULATION_STEPS': ACCUMULATION_STEPS,
            'LEARN_RATE': LEARN_RATE,
            'EPOCHS': EPOCHS,
            'WARMUP_STEPS': WARMUP_STEPS,
            'SEQUENCE_LENGTH': SEQUENCE_LENGTH,
        }, config_file, sort_keys=True, indent=4, separators=(',', ': '))

# Load and initialize model
MODEL_CLASS = load_model(MODEL_PREFIX)
TOKENIZER = MODEL_CLASS[0].from_pretrained(MODEL_NAME)
CONFIG = MODEL_CLASS[1].from_pretrained(MODEL_NAME, num_labels=3)
MODEL = MODEL_CLASS[2].from_pretrained(MODEL_NAME, config=CONFIG)

# Load training data
train_dataset = dataset(
    tokenize(chain(*(load_semeval(DATASET, 'train', lang) for lang in LANGS)), TOKENIZER, SEQUENCE_LENGTH))
train_sampler = RandomSampler(train_dataset)
train_dataset = DataLoader(train_dataset, sampler=train_sampler, batch_size=TRAIN_BATCH_SIZE, drop_last=True)

# Run Training
training(
  train_dataset,
  val_datasets(TOKENIZER, SEQUENCE_LENGTH),
  MODEL,