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

4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # Copyright 2021 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. """NAML export."""
  16. import numpy as np
  17. from mindspore import Tensor
  18. from mindspore.train.serialization import load_checkpoint, export
  19. from src.naml import NAML, NAMLWithLossCell
  20. from src.option import get_args
  21. if __name__ == '__main__':
  22. args = get_args("export")
  23. net = NAML(args)
  24. net.set_train(False)
  25. net_with_loss = NAMLWithLossCell(net)
  26. load_checkpoint(args.checkpoint_path, net_with_loss)
  27. news_encoder = net.news_encoder
  28. user_encoder = net.user_encoder
  29. bs = args.batch_size
  30. category = Tensor(np.zeros([bs, 1], np.int32))
  31. subcategory = Tensor(np.zeros([bs, 1], np.int32))
  32. title = Tensor(np.zeros([bs, args.n_words_title], np.int32))
  33. abstract = Tensor(np.zeros([bs, args.n_words_abstract], np.int32))
  34. news_input_data = [category, subcategory, title, abstract]
  35. export(news_encoder, *news_input_data, file_name=f"naml_news_encoder_bs_{bs}", file_format=args.file_format)
  36. browsed_news = Tensor(np.zeros([bs, args.n_browsed_news, args.n_filters], np.float32))
  37. export(user_encoder, browsed_news, file_name=f"naml_user_encoder_bs_{bs}", file_format=args.file_format)