本文整理匯總了Python中opts.py方法的典型用法代碼示例。如果您正苦於以下問題:Python opts.py方法的具體用法?Python opts.py怎麽用?Python opts.py使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類opts
的用法示例。
在下文中一共展示了opts.py方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: evaluate
# 需要導入模塊: import opts [as 別名]
# 或者: from opts import py [as 別名]
def evaluate(best_val_checkpoint_path):
# python translate.py -src data/multi30k/test2016.en.atok -output pred.txt \
# -replace_unk -tgt=data/multi30k/test2016.de.atok -report_bleu -gpu 2
# -model saves/2018-02-09-enc:Rev-dec:Rev-et:RevGRU-dt:RevGRU-h:300-el:1-dl:1-em:300-atn:general-cxt:slice_emb-sl:20-ef1:0.875-ef2:0.875-df1:0.875-df2:0.875/best_checkpoint.pt
base_dir = os.path.dirname(best_val_checkpoint_path)
if '600' in best_val_checkpoint_path:
test_output = subprocess.run(['python', 'translate.py', '-src', 'data/en-de/IWSLT16.TED.tst2014.en-de.en.tok.low',
'-output', os.path.join(base_dir, 'test_pred.txt'), '-replace_unk', '-tgt', 'data/en-de/IWSLT16.TED.tst2014.en-de.de.tok.low',
'-report_bleu', '-gpu', str(opt.gpuid[0]), '-model', best_val_checkpoint_path], stdout=subprocess.PIPE)
test_output_string = test_output.stdout.decode('utf-8')
print(test_output_string)
# Also save the whole stdout string for reference
with open(os.path.join(base_dir, 'test_stdout.txt'), 'w') as f:
f.write('{}\n'.format(test_output_string))
val_output = subprocess.run(['python', 'translate.py', '-src', 'data/en-de/IWSLT16.TED.tst2013.en-de.en.tok.low',
'-output', os.path.join(base_dir, 'val_pred.txt'), '-replace_unk', '-tgt', 'data/en-de/IWSLT16.TED.tst2013.en-de.de.tok.low',
'-report_bleu', '-gpu', str(opt.gpuid[0]), '-model', best_val_checkpoint_path], stdout=subprocess.PIPE)
val_output_string = val_output.stdout.decode('utf-8')
print(val_output_string)
else:
test_output = subprocess.run(['python', 'translate.py', '-src', 'data/multi30k/test2016.en.tok.low',
'-output', os.path.join(base_dir, 'test_pred.txt'), '-replace_unk', '-tgt', 'data/multi30k/test2016.de.tok.low',
'-report_bleu', '-gpu', str(opt.gpuid[0]), '-model', best_val_checkpoint_path], stdout=subprocess.PIPE)
test_output_string = test_output.stdout.decode('utf-8')
print(test_output_string)
# Also save the whole stdout string for reference
with open(os.path.join(base_dir, 'test_stdout.txt'), 'w') as f:
f.write('{}\n'.format(test_output_string))
val_output = subprocess.run(['python', 'translate.py', '-src', 'data/multi30k/val.en.tok.low',
'-output', os.path.join(base_dir, 'val_pred.txt'), '-replace_unk', '-tgt', 'data/multi30k/val.de.tok.low',
'-report_bleu', '-gpu', str(opt.gpuid[0]), '-model', best_val_checkpoint_path], stdout=subprocess.PIPE)
val_output_string = val_output.stdout.decode('utf-8')
print(val_output_string)
# Also save the whole stdout string for reference
with open(os.path.join(base_dir, 'val_stdout.txt'), 'w') as f:
f.write('{}\n'.format(val_output_string))
val_bleu = extract_bleu_score(val_output_string)
test_bleu = extract_bleu_score(test_output_string)
with open(os.path.join(base_dir, 'result.txt'), 'w') as f:
f.write('{} {}\n'.format(val_bleu, test_bleu))
print('Val BLEU: {} | Test BLEU: {}'.format(val_bleu, test_bleu))
示例2: main
# 需要導入模塊: import opts [as 別名]
# 或者: from opts import py [as 別名]
def main():
# Load train and validate data.
train_dataset = load_dataset("train")
valid_dataset = load_dataset("valid")
print(' * maximum batch size: %d' % opt.batch_size)
# Load checkpoint if we resume from a previous training.
if opt.train_from:
print('Loading checkpoint from %s' % opt.train_from)
checkpoint = torch.load(opt.train_from,
map_location=lambda storage, loc: storage)
model_opt = checkpoint['opt']
# I don't like reassigning attributes of opt: it's not clear.
opt.start_epoch = checkpoint['epoch'] + 1
else:
checkpoint = None
model_opt = opt
# Load fields generated from preprocess phase.
fields = load_fields(train_dataset, valid_dataset, checkpoint)
# Report src/tgt features.
collect_report_features(fields)
# Build model.
model = build_model(model_opt, opt, fields, checkpoint)
tally_parameters(model)
check_save_model_path()
# Build optimizer.
optim = build_optim(model, checkpoint)
# load embeddings
# NOTE you need to comment/uncomment the following section to use word embeddings!!!!!!
# NOTE DO NOT USE THOSE WORD EMBED OPTIONS IN opts.py because they do not work!!!!!!
fields['src'].vocab.load_vectors(wv_type='glove.42B', wv_dim=300)
fields['tgt'].vocab.load_vectors(wv_type='glove.42B', wv_dim=300)
model.encoder.embeddings.word_lut.weight.data.copy_(fields['src'].vocab.vectors.cuda())
model.decoder.embeddings.word_lut.weight.data.copy_(fields['tgt'].vocab.vectors.cuda())
model.encoder.embeddings.word_lut.weight.requires_grad = False
model.decoder.embeddings.word_lut.weight.requires_grad = False
# Do training.
train_model(model, train_dataset, valid_dataset, fields, optim, model_opt)