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.

cluener_evaluation.py 2.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. '''bert clue evaluation'''
  16. import json
  17. import numpy as np
  18. from evaluation_config import cfg
  19. import mindspore.common.dtype as mstype
  20. from mindspore.common.tensor import Tensor
  21. from CRF import postprocess
  22. import tokenization
  23. from sample_process import label_generation, process_one_example_p
  24. vocab_file = "./vocab.txt"
  25. tokenizer_ = tokenization.FullTokenizer(vocab_file=vocab_file)
  26. def process(model, text, sequence_length):
  27. """
  28. process text.
  29. """
  30. data = [text]
  31. features = []
  32. res = []
  33. ids = []
  34. for i in data:
  35. feature = process_one_example_p(tokenizer_, i, max_seq_len=sequence_length)
  36. features.append(feature)
  37. input_ids, input_mask, token_type_id = feature
  38. input_ids = Tensor(np.array(input_ids), mstype.int32)
  39. input_mask = Tensor(np.array(input_mask), mstype.int32)
  40. token_type_id = Tensor(np.array(token_type_id), mstype.int32)
  41. if cfg.use_crf:
  42. backpointers, best_tag_id = model.predict(input_ids, input_mask, token_type_id, Tensor(1))
  43. best_path = postprocess(backpointers, best_tag_id)
  44. logits = []
  45. for ele in best_path:
  46. logits.extend(ele)
  47. ids = logits
  48. else:
  49. logits = model.predict(input_ids, input_mask, token_type_id, Tensor(1))
  50. ids = logits.asnumpy()
  51. ids = np.argmax(ids, axis=-1)
  52. ids = list(ids)
  53. res = label_generation(text, ids)
  54. return res
  55. def submit(model, path, sequence_length):
  56. """
  57. submit task
  58. """
  59. data = []
  60. for line in open(path):
  61. if not line.strip():
  62. continue
  63. oneline = json.loads(line.strip())
  64. res = process(model, oneline["text"], sequence_length)
  65. print("text", oneline["text"])
  66. print("res:", res)
  67. data.append(json.dumps({"label": res}, ensure_ascii=False))
  68. open("ner_predict.json", "w").write("\n".join(data))