def get_smd(fresh: bool) -> Smd: if not fresh: try: with open( MODELS_REPO + "market/" + MEME_CLF_VERSION + "/stonks/" + STONK_VERSION + "/smd.json", "r", ) as f: smd = json.load(f) except Exception: with open( backup(MODELS_REPO) + "market/" + MEME_CLF_VERSION + "/stonks/" + STONK_VERSION + "/smd.json", "r", ) as f: smd = json.load(f) init = init_smd() for prop in init.keys(): try: smd[prop] except Exception: smd[prop] = init[prop] else: smd = init_smd() return smd
def check_point(self, is_backup: bool) -> None: self.model = self.model.to(torch.device("cpu")) REPO = backup(STONK_REPO) if is_backup else STONK_REPO torch.save(self.model, REPO.format("reg") + self.name + ".pt") jit.save( cast(ScriptModule, jit.script(self.model)), REPO.format("jit") + self.name + ".pt", ) with open(REPO.format("cp") + self.name + ".json", "w") as f: json.dump(self.cp, f, indent=4) self.model = self.model.to(torch.device("cuda:0"))
def dump_smd(smd: Smd): with open( MODELS_REPO + "market/" + MEME_CLF_VERSION + "/stonks/" + STONK_VERSION + "/smd.json", "w", ) as f: json.dump(smd, f, indent=4) with open( backup(MODELS_REPO) + "market/" + MEME_CLF_VERSION + "/stonks/" + STONK_VERSION + "/smd.json", "w", ) as f: json.dump(smd, f, indent=4)
def __init__(self, name: str, fresh: bool): self.fresh = fresh super(StonkTrainer, self).__init__() self.patience = 0 self.cp = load_cp(STONK_REPO.format("cp") + name, self.fresh) self.name = name try: self.features: nn.Module = torch.load( MEME_CLF_REPO.format("reg") + "features.pt").to(device) except Exception: self.features: nn.Module = torch.load( backup(MEME_CLF_REPO.format("reg")) + "features.pt").to(device) self.features = self.features.eval() self.model: Stonk = self.get_model(100).to(device)
def get_static_names(version: str, check_init: bool = True) -> Static: try: with open(LOAD_STATIC_PATH.format(version) + "static.json", "r") as f: static = json.load(f) except Exception: try: with open( backup(LOAD_STATIC_PATH.format(version)) + "static.json", "r") as f: static = json.load(f) except Exception: static = init_static() if not PROD and check_init: for prop in (init := init_static()).keys(): try: static[prop] except Exception: static[prop] = init[prop] with open(LOAD_STATIC_PATH.format(version) + "static.json", "w") as f: json.dump(static, f, indent=4) with open( backup(LOAD_STATIC_PATH.format(version)) + "static.json", "w") as f: json.dump(static, f, indent=4)
def get_model(self, size: int) -> Stonk: if self.fresh: model = Stonk(size) else: try: model: Stonk = torch.load( STONK_REPO.format("reg") + self.name + ".pt") except Exception as e: print(e) try: model: Stonk = torch.load( backup(STONK_REPO.format("reg")) + self.name + ".pt") except Exception as e: print(e) model = Stonk(size) return model
def get_model(self, output_size: int, fresh: bool) -> MemeClf: if fresh: model = MemeClf(output_size=output_size) else: try: model: MemeClf = torch.load( MEME_CLF_REPO.format("reg") + self.name + ".pt") except Exception as e: print(e) print("wtf") try: model: MemeClf = torch.load( backup(MEME_CLF_REPO).format("reg") + self.name + ".pt") except Exception as e: print(e) print("wtf") model = MemeClf(output_size=output_size) return model
def load_cp(cp_path: str, fresh: bool) -> CP: if not fresh: try: with open(cp_path + f".json", "r") as f: cp = json.load(f) except Exception: try: with open(backup(cp_path) + f".json", "r") as f: cp = json.load(f) except Exception: cp = init_cp() init = init_cp() for prop in init.keys(): try: cp[prop] except Exception: cp[prop] = init[prop] else: cp = init_cp() return cp