def predict(self, input_data: InputData, verbose=False) -> OutputData: if len(self.nodes_from) == 0: raise ValueError('') parent_results = [] if verbose: print(f'Obtain predictions from all parent nodes: {self.model}') for parent in self._nodes_from_with_fixed_order(): parent_results.append(parent.predict(input_data=input_data)) target = input_data.target secondary_input = Data.from_predictions(outputs=parent_results, target=target) if verbose: print( f'Obtain prediction in secondary node with model: {self.model}' ) preprocessed_data = copy(secondary_input) preprocessed_data.features = self.cache.actual_cached_state.preprocessor.apply( preprocessed_data.features) evaluation_result = self.model.predict( fitted_model=self.cache.actual_cached_state.model, data=preprocessed_data) return OutputData(idx=input_data.idx, features=input_data.features, predict=evaluation_result, task_type=input_data.task_type)
def test_data_from_predictions(output_dataset): data_1 = output_dataset data_2 = output_dataset data_3 = output_dataset target = output_dataset.predict new_input_data = Data.from_predictions(outputs=[data_1, data_2, data_3], target=target) assert new_input_data.features.all() == np.array( [data_1.predict, data_2.predict, data_3.predict]).all()
def predict(self, input_data: InputData, verbose=False) -> OutputData: if len(self.nodes_from) == 0: raise ValueError('') parent_results = [] if verbose: print(f'Obtain predictions from all parent nodes: {self.model}') parent_nodes = self._nodes_from_with_fixed_order() target = input_data.target # TODO refactor if any(['affects_target' in parent_node.model_tags for parent_node in parent_nodes]): if len(parent_nodes) == 1: # is the previous model is the model that changes target parent_result = parent_nodes[0].predict(input_data=input_data) target = parent_result.predict parent_results.append(parent_result) else: raise NotImplementedError() else: for parent in parent_nodes: parent_results.append(parent.predict(input_data=input_data)) secondary_input = Data.from_predictions(outputs=parent_results, target=target) if verbose: print(f'Obtain prediction in secondary node with model: {self.model}') preprocessed_data = transformation_function_for_data(input_data.data_type, self.model.metadata.input_types)(secondary_input) evaluation_result = self.model.predict(fitted_model=self.cache.actual_cached_state.model, data=preprocessed_data) return OutputData(idx=input_data.idx, features=input_data.features, predict=evaluation_result, data_type=self.model.output_datatype(input_data.data_type), task=input_data.task)
def fit(self, input_data: InputData, verbose=False) -> OutputData: if len(self.nodes_from) == 0: raise ValueError() parent_results = [] if verbose: print(f'Fit all parent nodes in secondary node with model: {self.model}') target = input_data.target parent_nodes = self._nodes_from_with_fixed_order() are_nodes_affect_target = ['affects_target' in parent_node.model_tags for parent_node in parent_nodes] if any(are_nodes_affect_target): if len(parent_nodes) == 1: # is the previous model is the model that changes target parent_result = parent_nodes[0].fit(input_data=input_data) target = parent_result.predict parent_results.append(parent_result) else: raise NotImplementedError() else: for parent in parent_nodes: parent_results.append(parent.fit(input_data=input_data)) secondary_input = Data.from_predictions(outputs=parent_results, target=target) if verbose: print(f'Trying to fit secondary node with model: {self.model}') model_predict = self._fit_using_cache(input_data=secondary_input, with_preprocessing=False) return OutputData(idx=input_data.idx, features=input_data.features, predict=model_predict, task=input_data.task, data_type=self.model.output_datatype(input_data.data_type))
def fine_tune(self, input_data: InputData, max_lead_time: timedelta = timedelta(minutes=5), iterations: int = 30, verbose: bool = False): parent_results = [] if verbose: print(f'Tune all parent nodes in secondary node with model: {self.model}') target = input_data.target for parent in self._nodes_from_with_fixed_order(): parent.fine_tune(input_data=input_data, max_lead_time=max_lead_time) parent_results.append(parent.predict(input_data=input_data)) secondary_input = Data.from_predictions(outputs=parent_results, target=target) preprocessed_data = transformation_function_for_data(input_data.data_type, self.model.metadata.input_types)(secondary_input) fitted_model, predict_train = self.model.fine_tune(preprocessed_data, iterations=iterations, max_lead_time=max_lead_time) self.cache.append(CachedState(preprocessor=None, model=fitted_model))
def fit(self, input_data: InputData, verbose=False) -> OutputData: if len(self.nodes_from) == 0: raise ValueError() parent_results = [] if verbose: print( f'Fit all parent nodes in secondary node with model: {self.model}' ) for parent in self._nodes_from_with_fixed_order(): parent_results.append(parent.fit(input_data=input_data)) target = input_data.target secondary_input = Data.from_predictions(outputs=parent_results, target=target) if verbose: print(f'Trying to fit secondary node with model: {self.model}') model_predict = self._fit_using_cache(input_data=secondary_input) return OutputData(idx=input_data.idx, features=input_data.features, predict=model_predict, task_type=input_data.task_type)