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.

attach.py 1.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. """
  2. Copyright 2020 Tianshu AI Platform. All Rights Reserved.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. =============================================================
  13. """
  14. from typing import Sequence, Callable
  15. from numbers import Number
  16. from kamal.core import exceptions
  17. class AttachTo(object):
  18. """ Attach task, metrics or visualizer to specified model outputs
  19. """
  20. def __init__(self, attach_to=None):
  21. if attach_to is not None and not isinstance(attach_to, (Sequence, Number, str, Callable) ):
  22. raise exceptions.InvalidMapping
  23. self._attach_to = attach_to
  24. def __call__(self, *tensors):
  25. if self._attach_to is not None:
  26. if isinstance(self._attach_to, Callable):
  27. return self._attach_to( *tensors )
  28. if isinstance(self._attach_to, Sequence):
  29. _attach_to = self._attach_to
  30. else:
  31. _attach_to = [ self._attach_to for _ in range(len(tensors)) ]
  32. _attach_to = _attach_to[:len(tensors)]
  33. tensors = [ tensor[index] for (tensor, index) in zip( tensors, _attach_to ) ]
  34. if len(tensors)==1:
  35. tensors = tensors[0]
  36. return tensors
  37. def __repr__(self):
  38. rep = "AttachTo: %s"%(self._attach_to)

一站式算法开发平台、高性能分布式深度学习框架、先进算法模型库、视觉模型炼知平台、数据可视化分析平台等一系列平台及工具,在模型高效分布式训练、数据处理和可视分析、模型炼知和轻量化等技术上形成独特优势,目前已在产学研等各领域近千家单位及个人提供AI应用赋能

Contributors (1)