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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. """export ckpt to model"""
  16. import argparse
  17. import numpy as np
  18. from mindspore import context, Tensor
  19. from mindspore.train.serialization import export, load_checkpoint
  20. from src.bgcf import BGCF
  21. from src.callback import ForwardBGCF
  22. parser = argparse.ArgumentParser(description="bgcf export")
  23. parser.add_argument("--device_id", type=int, default=0, help="Device id")
  24. parser.add_argument("--ckpt_file", type=str, required=True, help="Checkpoint file path.")
  25. parser.add_argument("--file_name", type=str, default="bgcf", help="output file name.")
  26. parser.add_argument("--file_format", type=str, choices=["AIR", "ONNX", "MINDIR"], default="AIR", help="file format")
  27. parser.add_argument("--device_target", type=str, choices=["Ascend", "GPU", "CPU"], default="Ascend",
  28. help="device target")
  29. parser.add_argument("--input_dim", type=int, choices=[64, 128], default=64, help="embedding dimension")
  30. parser.add_argument("--embedded_dimension", type=int, default=64, help="output embedding dimension")
  31. parser.add_argument("--row_neighs", type=int, default=40, help="num of sampling neighbors in raw graph")
  32. parser.add_argument("--gnew_neighs", type=int, default=20, help="num of sampling neighbors in sample graph")
  33. parser.add_argument("--activation", type=str, default="tanh", choices=["relu", "tanh"], help="activation function")
  34. args = parser.parse_args()
  35. context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target)
  36. if args.device_target == "Ascend":
  37. context.set_context(device_id=args.device_id)
  38. if __name__ == "__main__":
  39. num_user, num_item = 7068, 3570
  40. network = BGCF([args.input_dim, num_user, num_item],
  41. args.embedded_dimension,
  42. args.activation,
  43. [0.0, 0.0, 0.0],
  44. num_user,
  45. num_item,
  46. args.input_dim)
  47. load_checkpoint(args.ckpt_file, net=network)
  48. forward_net = ForwardBGCF(network)
  49. users = Tensor(np.zeros([num_user,]).astype(np.int32))
  50. items = Tensor(np.zeros([num_item,]).astype(np.int32))
  51. neg_items = Tensor(np.zeros([num_item, 1]).astype(np.int32))
  52. u_test_neighs = Tensor(np.zeros([num_user, args.row_neighs]).astype(np.int32))
  53. u_test_gnew_neighs = Tensor(np.zeros([num_user, args.gnew_neighs]).astype(np.int32))
  54. i_test_neighs = Tensor(np.zeros([num_item, args.row_neighs]).astype(np.int32))
  55. i_test_gnew_neighs = Tensor(np.zeros([num_item, args.gnew_neighs]).astype(np.int32))
  56. input_data = [users, items, neg_items, u_test_neighs, u_test_gnew_neighs, i_test_neighs, i_test_gnew_neighs]
  57. export(forward_net, *input_data, file_name=args.file_name, file_format=args.file_format)