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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. """Using for eval the model checkpoint"""
  16. import os
  17. from absl import logging
  18. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  19. from mindspore import context, Model
  20. import src.constants as rconst
  21. from src.dataset import create_dataset
  22. from src.metrics import NCFMetric
  23. from src.ncf import NCFModel, NetWithLossClass, TrainStepWrap, PredictWithSigmoid
  24. from utils.config import config
  25. from utils.moxing_adapter import moxing_wrapper
  26. from utils.device_adapter import get_device_id
  27. logging.set_verbosity(logging.INFO)
  28. @moxing_wrapper()
  29. def run_eval():
  30. """eval method"""
  31. if not os.path.exists(config.output_path):
  32. os.makedirs(config.output_path)
  33. context.set_context(mode=context.GRAPH_MODE,
  34. device_target="Davinci",
  35. save_graphs=False,
  36. device_id=get_device_id())
  37. layers = config.layers
  38. num_factors = config.num_factors
  39. topk = rconst.TOP_K
  40. num_eval_neg = rconst.NUM_EVAL_NEGATIVES
  41. ds_eval, num_eval_users, num_eval_items = create_dataset(test_train=False, data_dir=config.data_path,
  42. dataset=config.dataset, train_epochs=0,
  43. eval_batch_size=config.eval_batch_size)
  44. print("ds_eval.size: {}".format(ds_eval.get_dataset_size()))
  45. ncf_net = NCFModel(num_users=num_eval_users,
  46. num_items=num_eval_items,
  47. num_factors=num_factors,
  48. model_layers=layers,
  49. mf_regularization=0,
  50. mlp_reg_layers=[0.0, 0.0, 0.0, 0.0],
  51. mf_dim=16)
  52. param_dict = load_checkpoint(config.checkpoint_file_path)
  53. load_param_into_net(ncf_net, param_dict)
  54. loss_net = NetWithLossClass(ncf_net)
  55. train_net = TrainStepWrap(loss_net)
  56. eval_net = PredictWithSigmoid(ncf_net, topk, num_eval_neg)
  57. ncf_metric = NCFMetric()
  58. model = Model(train_net, eval_network=eval_net, metrics={"ncf": ncf_metric})
  59. ncf_metric.clear()
  60. out = model.eval(ds_eval)
  61. eval_file_path = os.path.join(config.output_path, config.eval_file_name)
  62. eval_file = open(eval_file_path, "a+")
  63. eval_file.write("EvalCallBack: HR = {}, NDCG = {}\n".format(out['ncf'][0], out['ncf'][1]))
  64. eval_file.close()
  65. print("EvalCallBack: HR = {}, NDCG = {}".format(out['ncf'][0], out['ncf'][1]))
  66. print("=" * 100 + "Eval Finish!" + "=" * 100)
  67. if __name__ == '__main__':
  68. run_eval()