def save_to_file_zip(save_path, data=None, params=None): """Save model to a .zip archive :param save_path: (str or file-like) Where to store the model :param data: (OrderedDict) Class parameters being stored :param params: (OrderedDict) Model parameters being stored """ # data/params can be None, so do not # try to serialize them blindly if data is not None: serialized_data = data_to_json(data) if params is not None: serialized_params = params_to_bytes(params) # We also have to store list of the parameters # to store the ordering for OrderedDict. # We can trust these to be strings as they # are taken from the Tensorflow graph. serialized_param_list = json.dumps(list(params.keys()), indent=4) # Check postfix if save_path is a string if isinstance(save_path, str): _, ext = os.path.splitext(save_path) if ext == "": save_path += ".zip" # Create a zip-archive and write our objects # there. This works when save_path # is either str or a file-like with zipfile.ZipFile(save_path, "w") as file_: # Do not try to save "None" elements if data is not None: file_.writestr("data", serialized_data) if params is not None: file_.writestr("parameters", serialized_params) file_.writestr("parameter_list", serialized_param_list)
def save(self, path): # save weights w_path = osp.join(path, 'model') self.policy.save(w_path) # save data data = { "gamma": self.gamma, "n_steps": self.n_steps, "vf_coef": self.vf_coef, "ent_coef": self.ent_coef, "max_grad_norm": self.max_grad_norm, "learning_rate": self.learning_rate, "gae_lambda": self.gae_lambda, "batch_size": self.batch_size, "n_epochs": self.n_epochs, "clip_range": self.clip_range, "clip_range_vf": self.clip_range_vf, "verbose": self.verbose, "policy_class": self.policy_class, "observation_space": self.observation_space, "action_space": self.action_space, "n_envs": self.n_envs, "seed": self.seed, "policy_kwargs": self.policy_kwargs } d_path = osp.join(path, 'params') serialized_data = data_to_json(data) with open(d_path, 'w') as f: f.write(serialized_data)
def _serializeAndExportTransferAgent(self, toAgentParams): with open('ToAgentExtracted/data') as jsonFile: data = json.load(jsonFile) serializedData = data_to_json(data) serializedParams = params_to_bytes(toAgentParams) serializedParamList = json.dumps( list(toAgentParams.keys()), indent=4 ) # Add zip to save path if isinstance(self.transferAgentSavePath, str): _, ext = os.path.splitext(self.transferAgentSavePath) if ext == "": self.transferAgentSavePath += ".zip" # Serialize data and write to zip file with zipfile.ZipFile(self.transferAgentSavePath, "w") as file_: if serializedData is not None: file_.writestr("data", serializedData) if toAgentParams is not None: file_.writestr("parameters", serializedParams) file_.writestr("parameter_list", serializedParamList)
def save(self, save_path): if self.is_pickle(): assert save_path.endswith(".pkl") with open(save_path, "wb") as file_: cloudpickle.dump((self.data, self.params), file_) else: assert save_path.endswith(".zip") data = self.data params = self.params if data is not None: serialized_data = data_to_json(data) if params is not None: serialized_params = params_to_bytes(params) # We also have to store list of the parameters # to store the ordering for OrderedDict. # We can trust these to be strings as they # are taken from the Tensorflow graph. serialized_param_list = json.dumps(list(params.keys()), indent=4) # Create a zip-archive and write our objects # there. This works when save_path # is either str or a file-like with zipfile.ZipFile(save_path, "w") as file_: # Do not try to save "None" elements if data is not None: file_.writestr("data", serialized_data) if params is not None: file_.writestr("parameters", serialized_params) file_.writestr("parameter_list", serialized_param_list)