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.

export.py 2.3 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. """ncf export file"""
  16. import numpy as np
  17. from mindspore import Tensor, context, load_checkpoint, load_param_into_net, export
  18. import src.constants as rconst
  19. from utils.config import config
  20. from ncf import NCFModel, PredictWithSigmoid
  21. context.set_context(mode=context.GRAPH_MODE, device_target=config.device_target)
  22. if config.device_target == "Ascend":
  23. context.set_context(device_id=config.device_id)
  24. if __name__ == "__main__":
  25. topk = rconst.TOP_K
  26. num_eval_neg = rconst.NUM_EVAL_NEGATIVES
  27. if config.dataset == "ml-1m":
  28. num_eval_users = 6040
  29. num_eval_items = 3706
  30. elif config.dataset == "ml-20m":
  31. num_eval_users = 138493
  32. num_eval_items = 26744
  33. else:
  34. raise ValueError("not supported dataset")
  35. ncf_net = NCFModel(num_users=num_eval_users,
  36. num_items=num_eval_items,
  37. num_factors=config.num_factors,
  38. model_layers=config.layers,
  39. mf_regularization=0,
  40. mlp_reg_layers=[0.0, 0.0, 0.0, 0.0],
  41. mf_dim=16)
  42. param_dict = load_checkpoint(config.ckpt_file)
  43. load_param_into_net(ncf_net, param_dict)
  44. network = PredictWithSigmoid(ncf_net, topk, num_eval_neg)
  45. users = Tensor(np.zeros([config.eval_batch_size, 1]).astype(np.int32))
  46. items = Tensor(np.zeros([config.eval_batch_size, 1]).astype(np.int32))
  47. masks = Tensor(np.zeros([config.eval_batch_size, 1]).astype(np.float32))
  48. input_data = [users, items, masks]
  49. export(network, *input_data, file_name=config.file_name, file_format=config.file_format)