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

5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. BGCF evaluation script.
  17. """
  18. import datetime
  19. import mindspore.context as context
  20. from mindspore.train.serialization import load_checkpoint
  21. from mindspore.common import set_seed
  22. from src.bgcf import BGCF
  23. from src.utils import BGCFLogger
  24. from src.config import parser_args
  25. from src.metrics import BGCFEvaluate
  26. from src.callback import ForwardBGCF, TestBGCF
  27. from src.dataset import TestGraphDataset, load_graph
  28. set_seed(1)
  29. def evaluation():
  30. """evaluation"""
  31. num_user = train_graph.graph_info()["node_num"][0]
  32. num_item = train_graph.graph_info()["node_num"][1]
  33. eval_class = BGCFEvaluate(parser, train_graph, test_graph, parser.Ks)
  34. for _epoch in range(parser.eval_interval, parser.num_epoch+1, parser.eval_interval) \
  35. if parser.device_target == "Ascend" else range(parser.num_epoch, parser.num_epoch+1):
  36. bgcfnet_test = BGCF([parser.input_dim, num_user, num_item],
  37. parser.embedded_dimension,
  38. parser.activation,
  39. [0.0, 0.0, 0.0],
  40. num_user,
  41. num_item,
  42. parser.input_dim)
  43. load_checkpoint(parser.ckptpath + "/bgcf_epoch{}.ckpt".format(_epoch), net=bgcfnet_test)
  44. forward_net = ForwardBGCF(bgcfnet_test)
  45. user_reps, item_reps = TestBGCF(forward_net, num_user, num_item, parser.input_dim, test_graph_dataset)
  46. test_recall_bgcf, test_ndcg_bgcf, \
  47. test_sedp, test_nov = eval_class.eval_with_rep(user_reps, item_reps, parser)
  48. if parser.log_name:
  49. log.write(
  50. 'epoch:%03d, recall_@10:%.5f, recall_@20:%.5f, ndcg_@10:%.5f, ndcg_@20:%.5f, '
  51. 'sedp_@10:%.5f, sedp_@20:%.5f, nov_@10:%.5f, nov_@20:%.5f\n' % (_epoch,
  52. test_recall_bgcf[1],
  53. test_recall_bgcf[2],
  54. test_ndcg_bgcf[1],
  55. test_ndcg_bgcf[2],
  56. test_sedp[0],
  57. test_sedp[1],
  58. test_nov[1],
  59. test_nov[2]))
  60. else:
  61. print('epoch:%03d, recall_@10:%.5f, recall_@20:%.5f, ndcg_@10:%.5f, ndcg_@20:%.5f, '
  62. 'sedp_@10:%.5f, sedp_@20:%.5f, nov_@10:%.5f, nov_@20:%.5f\n' % (_epoch,
  63. test_recall_bgcf[1],
  64. test_recall_bgcf[2],
  65. test_ndcg_bgcf[1],
  66. test_ndcg_bgcf[2],
  67. test_sedp[0],
  68. test_sedp[1],
  69. test_nov[1],
  70. test_nov[2]))
  71. if __name__ == "__main__":
  72. parser = parser_args()
  73. context.set_context(mode=context.GRAPH_MODE,
  74. device_target=parser.device_target,
  75. save_graphs=False)
  76. if parser.device_target == "Ascend":
  77. context.set_context(device_id=int(parser.device))
  78. train_graph, test_graph, sampled_graph_list = load_graph(parser.datapath)
  79. test_graph_dataset = TestGraphDataset(train_graph, sampled_graph_list, num_samples=parser.raw_neighs,
  80. num_bgcn_neigh=parser.gnew_neighs,
  81. num_neg=parser.num_neg)
  82. if parser.log_name:
  83. now = datetime.datetime.now().strftime("%b_%d_%H_%M_%S")
  84. name = "bgcf" + '-' + parser.log_name + '-' + parser.dataset
  85. log_save_path = './log-files/' + name + '/' + now
  86. log = BGCFLogger(logname=name, now=now, foldername='log-files', copy=False)
  87. log.open(log_save_path + '/log.train.txt', mode='a')
  88. for arg in vars(parser):
  89. log.write(arg + '=' + str(getattr(parser, arg)) + '\n')
  90. else:
  91. for arg in vars(parser):
  92. print(arg + '=' + str(getattr(parser, arg)))
  93. evaluation()