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.7 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. """
  16. ##############test textcnn example on movie review#################
  17. python eval.py
  18. """
  19. import mindspore.nn as nn
  20. from mindspore.nn.metrics import Accuracy
  21. from mindspore import context
  22. from mindspore.train.model import Model
  23. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  24. from utils.moxing_adapter import moxing_wrapper
  25. from utils.device_adapter import get_device_id
  26. from utils.config import config
  27. from src.textcnn import TextCNN
  28. from src.dataset import MovieReview, SST2, Subjectivity
  29. @moxing_wrapper()
  30. def eval_net():
  31. '''eval net'''
  32. if config.dataset == 'MR':
  33. instance = MovieReview(root_dir=config.data_path, maxlen=config.word_len, split=0.9)
  34. elif config.dataset == 'SUBJ':
  35. instance = Subjectivity(root_dir=cfg.data_path, maxlen=cfg.word_len, split=0.9)
  36. elif config.dataset == 'SST2':
  37. instance = SST2(root_dir=config.data_path, maxlen=config.word_len, split=0.9)
  38. device_target = config.device_target
  39. context.set_context(mode=context.GRAPH_MODE, device_target=config.device_target)
  40. if device_target == "Ascend":
  41. context.set_context(device_id=get_device_id())
  42. dataset = instance.create_test_dataset(batch_size=config.batch_size)
  43. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True)
  44. net = TextCNN(vocab_len=instance.get_dict_len(), word_len=config.word_len,
  45. num_classes=config.num_classes, vec_length=config.vec_length)
  46. opt = nn.Adam(filter(lambda x: x.requires_grad, net.get_parameters()), learning_rate=0.001,
  47. weight_decay=float(config.weight_decay))
  48. param_dict = load_checkpoint(config.checkpoint_file_path)
  49. print("load checkpoint from [{}].".format(config.checkpoint_file_path))
  50. load_param_into_net(net, param_dict)
  51. net.set_train(False)
  52. model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc': Accuracy()})
  53. acc = model.eval(dataset)
  54. print("accuracy: ", acc)
  55. if __name__ == '__main__':
  56. eval_net()