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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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, device_id=args.device_id)
  36. if __name__ == "__main__":
  37. num_user, num_item = 7068, 3570
  38. network = BGCF([args.input_dim, num_user, num_item],
  39. args.embedded_dimension,
  40. args.activation,
  41. [0.0, 0.0, 0.0],
  42. num_user,
  43. num_item,
  44. args.input_dim)
  45. load_checkpoint(args.ckpt_file, net=network)
  46. forward_net = ForwardBGCF(network)
  47. users = Tensor(np.zeros([num_user,]).astype(np.int32))
  48. items = Tensor(np.zeros([num_item,]).astype(np.int32))
  49. neg_items = Tensor(np.zeros([num_item, 1]).astype(np.int32))
  50. u_test_neighs = Tensor(np.zeros([num_user, args.row_neighs]).astype(np.int32))
  51. u_test_gnew_neighs = Tensor(np.zeros([num_user, args.gnew_neighs]).astype(np.int32))
  52. i_test_neighs = Tensor(np.zeros([num_item, args.row_neighs]).astype(np.int32))
  53. i_test_gnew_neighs = Tensor(np.zeros([num_item, args.gnew_neighs]).astype(np.int32))
  54. input_data = [users, items, neg_items, u_test_neighs, u_test_gnew_neighs, i_test_neighs, i_test_gnew_neighs]
  55. export(forward_net, *input_data, file_name=args.file_name, file_format=args.file_format)