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.

test.py 2.7 kB

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