You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

eval.py 2.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Evaluation api."""
  16. import argparse
  17. import pickle
  18. from mindspore.common import dtype as mstype
  19. from config import TransformerConfig
  20. from src.transformer import infer, infer_ppl
  21. from src.utils import Dictionary
  22. from src.utils import get_score
  23. parser = argparse.ArgumentParser(description='Evaluation MASS.')
  24. parser.add_argument("--config", type=str, required=True,
  25. help="Model config json file path.")
  26. parser.add_argument("--vocab", type=str, required=True,
  27. help="Vocabulary to use.")
  28. parser.add_argument("--output", type=str, required=True,
  29. help="Result file path.")
  30. parser.add_argument("--metric", type=str, default='rouge',
  31. help='Set eval method.')
  32. def get_config(config):
  33. config = TransformerConfig.from_json_file(config)
  34. config.compute_type = mstype.float16
  35. config.dtype = mstype.float32
  36. return config
  37. if __name__ == '__main__':
  38. args, _ = parser.parse_known_args()
  39. vocab = Dictionary.load_from_persisted_dict(args.vocab)
  40. _config = get_config(args.config)
  41. if args.metric == 'rouge':
  42. result = infer(_config)
  43. else:
  44. result = infer_ppl(_config)
  45. with open(args.output, "wb") as f:
  46. pickle.dump(result, f, 1)
  47. # get score by given metric
  48. score = get_score(result, vocab, metric=args.metric)
  49. print(score)