def save(self): logger.info(f"save: {self._fname}") # Save JSON config to disk with self._fname_json.open("w") as json_file: json_file.write(self._model.to_json()) # Save weights to disk self._model.save_weights(str(self._fname_weights))
def load(self): logger.info(f"load: {self._fname}") # Reload the model from the 2 files we saved with self._fname_json.open("r") as json_file: json_config = json_file.read() self._model = model_from_json(json_config) self._model.load_weights(str(self._fname_weights)) self.compile()
def fit(self, x=None, y=None, batch_size=None, epochs=1, verbose=1, callbacks=None, validation_split=0.0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0, steps_per_epoch=None, validation_steps=None, validation_freq=1, max_queue_size=10, workers=1, use_multiprocessing=False): if self.model_exists(): logger.info(f"{self._fname} exists. skip fitting") return logger.info(f"start fitting") self.history = self._model.fit( x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing) logger.info(f"finish fitting") self.save()
def fit(self, x=None, y=None, batch_size=None, epochs=1, verbose=1, callbacks=None, validation_split=0.0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0, steps_per_epoch=None, validation_steps=None, validation_freq=1, max_queue_size=10, workers=1, use_multiprocessing=False): teacher = ConvMinst("teacher.h5", self.temp()) student = ConvMinst("student.h5", self.temp()) logger.info(f"start fitting the teacher") self.teacher_history = teacher.fit(x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing) logger.info("evaluate the labels at temperature t") teacher_logit = teacher.get_logit(x) teacher_predicted = tf.nn.softmax(teacher_logit / self._temp) logger.info("train the student model at temperature t") self.student_history = student.fit(x, teacher_predicted, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing) self._model = student.get_model()
def attack_info(k, v): logger.info(f"{prefix}: {k}: {v:.2f}") result[k] = v