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.

nst.py 1.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 .kd import KDDistiller
  15. from kamal.core.tasks.loss import KDLoss
  16. from kamal.core.tasks.loss import NSTLoss
  17. import torch
  18. import torch.nn as nn
  19. import time
  20. class NSTDistiller(KDDistiller):
  21. def __init__(self, logger=None, tb_writer=None ):
  22. super(NSTDistiller, self).__init__( logger, tb_writer )
  23. def setup(self, student, teacher, dataloader, optimizer, T=1.0, alpha=1.0, beta=1.0, gamma=1.0, stu_hooks=[], tea_hooks=[], out_flags=[], device=None):
  24. super( NSTDistiller, self ).setup(
  25. student, teacher, dataloader, optimizer, T=T, alpha=alpha, beta=beta, gamma=gamma, device=device )
  26. self.stu_hooks = stu_hooks
  27. self.tea_hooks = tea_hooks
  28. self.out_flags = out_flags
  29. self._nst_loss = NSTLoss()
  30. def additional_kd_loss(self, engine, batch):
  31. feat_s = [f.feat_out if flag else f.feat_in for (f, flag) in zip(self.stu_hooks, self.out_flags)]
  32. feat_t = [f.feat_out.detach() if flag else f.feat_in for (f, flag) in zip(self.tea_hooks, self.out_flags)]
  33. g_s = feat_s[1:-1]
  34. g_t = feat_t[1:-1]
  35. return self._nst_loss(g_s, g_t)

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

Contributors (1)