train_config_entity.py 992 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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, model_type=str, lr: float = None):
  13. # 模型类型
  14. self._model_type = model_type
  15. # 学习率
  16. self._lr = lr
  17. @property
  18. def model_type(self):
  19. return self._model_type
  20. @property
  21. def lr(self):
  22. return self._lr
  23. @staticmethod
  24. def from_config(config_path: str):
  25. """
  26. 从配置文件生成实体类
  27. """
  28. if os.path.exists(config_path):
  29. with open(config_path, mode="r", encoding="utf-8") as f:
  30. j = json.loads(f.read())
  31. else:
  32. raise GeneralException(ResultCodesEnum.NOT_FOUND, message=f"指配置文件【{config_path}】不存在")
  33. return TrainConfigEntity(**j)
  34. if __name__ == "__main__":
  35. pass