train_config_entity.py 835 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. # -*- coding: utf-8 -*-
  2. """
  3. @author: yq
  4. @time: 2024/11/1
  5. @desc: 模型训练超参数配置类
  6. """
  7. import json
  8. import os
  9. from commom import GeneralException
  10. from enums import ResultCodesEnum
  11. class TrainConfigEntity():
  12. def __init__(self, lr: float):
  13. # 学习率
  14. self._lr = lr
  15. @property
  16. def lr(self):
  17. return self._lr
  18. @staticmethod
  19. def from_config(config_path: str):
  20. """
  21. 从配置文件生成实体类
  22. """
  23. if os.path.exists(config_path):
  24. with open(config_path, mode="r", encoding="utf-8") as f:
  25. j = json.loads(f.read())
  26. else:
  27. raise GeneralException(ResultCodesEnum.NOT_FOUND, message=f"指配置文件【{config_path}】不存在")
  28. return TrainConfigEntity(**j)
  29. if __name__ == "__main__":
  30. pass