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

5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. """ test_training """
  16. import os
  17. from mindspore import Model, context
  18. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  19. from src.wide_and_deep import PredictWithSigmoid, TrainStepWrap, NetWithLossClass, WideDeepModel
  20. from src.callbacks import LossCallBack, EvalCallBack
  21. from src.datasets import create_dataset
  22. from src.metrics import AUCMetric
  23. from src.config import WideDeepConfig
  24. def get_WideDeep_net(config):
  25. """
  26. Get network of wide&deep model.
  27. """
  28. WideDeep_net = WideDeepModel(config)
  29. loss_net = NetWithLossClass(WideDeep_net, config)
  30. train_net = TrainStepWrap(loss_net)
  31. eval_net = PredictWithSigmoid(WideDeep_net)
  32. return train_net, eval_net
  33. class ModelBuilder():
  34. """
  35. Wide and deep model builder
  36. """
  37. def __init__(self):
  38. pass
  39. def get_hook(self):
  40. pass
  41. def get_train_hook(self):
  42. hooks = []
  43. callback = LossCallBack()
  44. hooks.append(callback)
  45. if int(os.getenv('DEVICE_ID')) == 0:
  46. pass
  47. return hooks
  48. def get_net(self, config):
  49. return get_WideDeep_net(config)
  50. def test_eval(config):
  51. """
  52. test evaluate
  53. """
  54. data_path = config.data_path
  55. batch_size = config.batch_size
  56. ds_eval = create_dataset(data_path, train_mode=False, epochs=2,
  57. batch_size=batch_size)
  58. print("ds_eval.size: {}".format(ds_eval.get_dataset_size()))
  59. net_builder = ModelBuilder()
  60. train_net, eval_net = net_builder.get_net(config)
  61. param_dict = load_checkpoint(config.ckpt_path)
  62. load_param_into_net(eval_net, param_dict)
  63. auc_metric = AUCMetric()
  64. model = Model(train_net, eval_network=eval_net, metrics={"auc": auc_metric})
  65. eval_callback = EvalCallBack(model, ds_eval, auc_metric, config)
  66. model.eval(ds_eval, callbacks=eval_callback)
  67. if __name__ == "__main__":
  68. widedeep_config = WideDeepConfig()
  69. widedeep_config.argparse_init()
  70. context.set_context(mode=context.GRAPH_MODE, device_target=widedeep_config.device_target)
  71. test_eval(widedeep_config)