def save(self, name=None, model_plot=False): """ Save the model to disk :param name: Folder name to be saved :type name: string :param model_plot: True to plot model too :type model_plot: boolean :return: A saved folder on disk """ # Only generate a folder automatically if no name provided if self.folder_name is None and name is None: self.folder_name = folder_runnum() else: if name is not None: self.folder_name = name # if foldername provided, then create a directory if not os.path.exists(os.path.join(self.currentdir, self.folder_name)): os.makedirs(os.path.join(self.currentdir, self.folder_name)) self.fullfilepath = os.path.join(self.currentdir, self.folder_name + '/') txt_file_path = self.fullfilepath + 'hyperparameter.txt' if os.path.isfile(txt_file_path): self.hyper_txt = open(txt_file_path, 'a') self.hyper_txt.write("\n") self.hyper_txt.write("======Another Run======") else: self.hyper_txt = open(txt_file_path, 'w') self.hyper_txt.write(f"Model: {self.name} \n") self.hyper_txt.write(f"Model Type: {self._model_type} \n") self.hyper_txt.write( f"astroNN identifier: {self._model_identifier} \n") self.hyper_txt.write(f"Python Version: {self._python_info} \n") self.hyper_txt.write(f"astroNN Version: {self._astronn_ver} \n") self.hyper_txt.write(f"Keras Version: {self._keras_ver} \n") self.hyper_txt.write(f"Tensorflow Version: {self._tf_ver} \n") self.hyper_txt.write(f"Folder Name: {self.folder_name} \n") self.hyper_txt.write(f"Batch size: {self.batch_size} \n") self.hyper_txt.write( f"Optimizer: {self.optimizer.__class__.__name__} \n") self.hyper_txt.write(f"Maximum Epochs: {self.max_epochs} \n") self.hyper_txt.write(f"Learning Rate: {self.lr} \n") self.hyper_txt.write(f"Validation Size: {self.val_size} \n") self.hyper_txt.write(f"Input Shape: {self.input_shape} \n") self.hyper_txt.write(f"Label Shape: {self.labels_shape} \n") self.hyper_txt.write(f"Number of Training Data: {self.num_train} \n") self.hyper_txt.write(f"Number of Validation Data: {self.val_num} \n") if model_plot is True: self.plot_model() self.post_training_checklist_child() self.virtual_cvslogger.savefile(folder_name=self.folder_name)
def save(self, name=None, model_plot=False): """ Save the model to disk :param name: Folder name to be saved :type name: string :param model_plot: True to plot model too :type model_plot: boolean :return: A saved folder on disk """ self.has_model_check() # Only generate a folder automatically if no name provided if self.folder_name is None and name is None: self.folder_name = folder_runnum() elif name is not None: self.folder_name = name # if foldername provided, then create a directory, if exist append something to avoid overwrite if not os.path.exists(os.path.join(self.currentdir, self.folder_name)): os.makedirs(os.path.join(self.currentdir, self.folder_name)) else: i_back = 2 while True: if not os.path.exists( os.path.join(self.currentdir, self.folder_name + f'_{i_back}')): break i_back += 1 new_folder_name_temp = self.folder_name + f'_{i_back}' warnings.warn( f'To prevent your model being overwritten, your folder name changed from {self.folder_name} ' f'to {new_folder_name_temp}', UserWarning) self.folder_name = new_folder_name_temp os.makedirs(os.path.join(self.currentdir, self.folder_name)) self.fullfilepath = os.path.join(self.currentdir, self.folder_name + os.sep) txt_file_path = self.fullfilepath + 'hyperparameter.txt' if os.path.isfile(txt_file_path): self.hyper_txt = open(txt_file_path, 'a') self.hyper_txt.write("\n") self.hyper_txt.write("======Another Run======") else: self.hyper_txt = open(txt_file_path, 'w') self.hyper_txt.write(f"Model: {self.name} \n") self.hyper_txt.write(f"Model Type: {self._model_type} \n") self.hyper_txt.write( f"astroNN identifier: {self._model_identifier} \n") self.hyper_txt.write(f"Python Version: {self._python_info} \n") self.hyper_txt.write(f"astroNN Version: {self._astronn_ver} \n") self.hyper_txt.write(f"Keras Version: {self._keras_ver} \n") self.hyper_txt.write(f"Tensorflow Version: {self._tf_ver} \n") self.hyper_txt.write(f"Folder Name: {self.folder_name} \n") self.hyper_txt.write(f"Batch size: {self.batch_size} \n") self.hyper_txt.write( f"Optimizer: {self.optimizer.__class__.__name__} \n") self.hyper_txt.write(f"Maximum Epochs: {self.max_epochs} \n") self.hyper_txt.write(f"Learning Rate: {self.lr} \n") self.hyper_txt.write(f"Validation Size: {self.val_size} \n") self.hyper_txt.write(f"Input Shape: {self._input_shape} \n") self.hyper_txt.write(f"Label Shape: {self._labels_shape} \n") self.hyper_txt.write(f"Number of Training Data: {self.num_train} \n") self.hyper_txt.write(f"Number of Validation Data: {self.val_num} \n") if model_plot is True: self.plot_model() self.post_training_checklist_child() if self.virtual_cvslogger is not None: # in case you save without training, so cvslogger is None self.virtual_cvslogger.savefile(folder_name=self.folder_name)