Пример #1
0
            log_to_file(log_file_name, epoch, avg_train_loss, train_accuracy, dev_accuracy)

            if patience == train_hyperparams['PATIENCE']:
                print 'out of patience after {} epochs'.format(epoch)
                train_progress_bar.finish()
                break
            # finished epoch
            train_progress_bar.update(epoch)
                
        print 'finished training.'
        
        ti = SoftAttention(pc, model_hyperparams, best_model_path)
        dev_accuracy, dev_results = ti.evaluate(dev_data.iter(), int(arguments['--beam']))
        print 'Best dev accuracy: {}'.format(dev_accuracy)
        write_param_file(output_file_path, dict(model_hyperparams.items()+train_hyperparams.items()))
        write_pred_file(output_file_path, dev_results)
        write_eval_file(output_file_path, best_dev_accuracy, dev_path)

    elif arguments['test']:
        print '=========EVALUATION ONLY:========='
        # requires test path, model path of pretrained path and results path where to write the results to
        assert arguments['--test_path']!=None

        print 'Loading data...'
        test_path = check_path(arguments['--test_path'], '--test_path')
        data_set = SoftDataSet
        input_format = [int(col) for col in arguments['--input_format'].split(',')]
        test_data = data_set.from_file(test_path,input_format, arguments['--lowercase'])
        print 'Test data has {} examples'.format(test_data.length)

        print 'Checking if any special symbols in data...'
		    arguments["--lm_paths"].split(','),\
		    [int(o) for o in arguments["--lm_orders"].split(',')]\
		)):
        lm_model_folder =  check_path(path, 'LM_MODEL_FOLDER_{}'.format(i), is_data_path=False)
        if lm_type=="srilm_char":
            print '...Loading lm model {} from path {}'.format(i,lm_model_folder)
            lm_model =  SRILM_char_lm_loader(lm_model_folder, order)
        elif lm_type=="srilm_morph":
            lm_model = SRILM_morpheme_lm_loader(lm_model_folder,order)
            assert arguments['--morph_vocab'] != None
            lm_model.vocab = Vocab.from_file(check_path(arguments['--morph_vocab'], 'morph_vocab', is_data_path=False))
        else:
            print "WARNING -- Could not load language model. Unknown type",lm_type,". Use 'srilm_char' or 'srilm_morph'"
        lm_models.append(lm_model)
    lm_number  = len(lm_models)

    output_file_path = os.path.join(model_folder,arguments['--pred_path'])

    # save best dev model parameters and predictions
    print 'Evaluating on test..'
    t = time.clock()
    if arguments['--indices']:
        indices = [int(ind) for ind in arguments['--indices'].split(',')]
        accuracy, test_results = evaluate_syncbeam(test_data.iter(indices), nmt_models, lm_models, weights, int(arguments['--beam']), int(arguments['--output_format']), verbose=arguments['--verbose'], exclude_eow=arguments['--exclude_eow'])
    else:
        accuracy, test_results = evaluate_syncbeam(test_data.iter(), nmt_models, lm_models, weights, int(arguments['--beam']), int(arguments['--output_format']), verbose=arguments['--verbose'], exclude_eow=arguments['--exclude_eow'])
    print 'Time: {}'.format(time.clock()-t)
    print 'accuracy: {}'.format(accuracy)
    write_pred_file(output_file_path, test_results, int(arguments['--output_format']))
    write_eval_file(output_file_path, accuracy, test_path)