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

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