def get(cls, model_id): model = Model.get(model_id) dataset = DatasetsService().get_dataset_for_training( model.requirements) if dataset and model.status == TrainingStatus.WAITING.name: model.status = TrainingStatus.READY.name model.update() return model
def link_model_to_dataset(self, model_id): has_dataset = False model = Model.get(model_id) dataset = DatasetsService().get_dataset_for_training( model.requirements) if not dataset: return model_id, self.get_id(), has_dataset model.link_to_dataset(dataset) model.update() self.federated_aggregator_connector.accept_model_training( self.get_id(), model_id) return model_id, self.get_id(), not has_dataset
def step(self, model_id, step_data, public_key): """ :param model_id: :param step_data: :param public_key: :return: """ model_orm = Model.get(model_id) self.get_stored_model(model_orm, public_key) model = DataOwner().step(model_orm.model, step_data, float(self.config['ETA'])) return model.weights
def model_quality_metrics(self, model_id, weights, model_type, public_key): """ Method used only by validator role. It doesn't use the model built from the data. It gets the model from the federated trainer and use the local data to calculate quality metrics :return: the model quality (currently measured with the MSE) """ data_owner = DataOwner() logging.info("Getting metrics, data owner: {}".format(self.client_id)) model_orm = Model.get(model_id) X_test, y_test = model_orm.get_dataset() model_orm.set_weights(weights) diffs = data_owner.model_quality_metrics(model_orm.model, X_test, y_test) return diffs
def process(self, model_id, weights, public_key): """ Process to run model :param model_type: :param weights: :return: """ logging.info("Initializing local model") model_orm = Model.get(model_id) model_orm.set_weights(weights) model, gradient = DataOwner().calculate_gradient(model_orm.model) self.update_stored_model(model_orm, model, public_key) return gradient
def update_mse(self, model_id, mse, role): """ Method used only by validator role. It doesn't use the model built from the data. It gets the model from the federated trainer and use the local data to calculate quality metrics :return: the model quality (currently measured with the MSE) """ logging.info("Getting metrics, data owner: {}".format(self.client_id)) model_orm = Model.get(model_id) model_orm.add_mse(mse) if model_orm.initial_mse == 0.0: model_orm.initial_mse = mse model_orm.status = TrainingStatus.IN_PROGRESS.name model_orm.improvement = max([ (model_orm.initial_mse - mse) / model_orm.initial_mse, 0 ]) model_orm.iterations += 1 model_orm.role = role model_orm.update() logging.info("Calculated mse: {}".format(mse))
def get_all(cls, args): filters = {k: v for k, v in args.items() if v is not None} return Model.find_all_by(filters)
def finish_training(self, model_id, contribs, improvement): model = Model.get(model_id) model.status = TrainingStatus.FINISHED.name model.improvement = improvement model.earned = self._calculate_earnings(model, contribs) model.update()
def init_model(self, model_id, model_type, reqs): model = Model(model_id, model_type, reqs) model.save() return model_id, self.get_id()
def model_is_linked(self, model_id): return Model.get(model_id).status != TrainingStatus.WAITING.name