def TrainRBFKernelServer(self, lmbda=.1, gamma=50., sampling_factor=1./1000.): """ Fit Kernelized RBF Ridge Regression to the current image on the server :param lmbda: :param gamma: gamma factor to build the kernel (https://en.wikipedia.org/wiki/Radial_basis_function_kernel) :param sampling_factor: percentage of pixels to sample to build the model :return: """ self._BuildDataSet(sampling_factor, normalize=True) inputs = self.inputs outputs = self.outputs labels_input = self.bands_modeling_estimation_input labels_output = self.bands_modeling_estimation_output # Train self.distance_krr = kernel.RBFDistance(kernel.RBFDistance(gamma)) self.gamma = gamma self.lmbda = lmbda self.kernel_rbf = kernel.Kernel(inputs, labels_input, self.distance_krr, weight_property="weight") outputs_eeArray = converters.eeFeatureCollectionToeeArray(outputs, labels_output) self.alpha = self.kernel_rbf.getAlphaeeArray(outputs_eeArray, lmbda)
def TrainRBFKernelLocal(self, lmbda=.1, gamma=.5, sampling_factor=1./100., with_task=False, with_cross_validation=False): """ Fit Kernelized RBF Ridge Regression to the current image subsampled. It downloads the data to fit. It uses sklearn.kernel_ridge library. :param lmbda: regularization factor :param gamma: gamma factor to build the kernel (https://en.wikipedia.org/wiki/Radial_basis_function_kernel) :param sampling_factor: percentage of pixels to sample to build the model :param with_cross_validation: donwload the data to fit the model with a task :param with_task: donwload the data to fit the model with a task :return: """ best_params = None if not with_cross_validation: best_params = {"alpha": lmbda, "gamma":gamma} modelo = model_sklearn.KRRModel(best_params=best_params,verbose=1) self._BuildDataSet(sampling_factor, normalize=False) ds_total = converters.eeFeatureCollectionToPandas(self.datos, self.bands_modeling_estimation+["weight"], with_task=with_task) logger.info("Size of downloaded ds: {}".format(ds_total.shape)) output_mean,output_std = model_sklearn.fit_model_local(ds_total,modelo, self.bands_modeling_estimation_input, self.bands_modeling_estimation_output) if with_cross_validation: logger.info("Best params: {}".format(modelo.named_steps["randomizedsearchcv"].best_params_)) ridge_kernel = modelo.named_steps["randomizedsearchcv"].best_estimator_ else: ridge_kernel = modelo.named_steps["kernelridge"] # Copy model parameters self.gamma = ridge_kernel.gamma self.lmbda = ridge_kernel.alpha self.alpha = ee.Array(ridge_kernel.dual_coef_.tolist()) # column vector self.inputs_krr =ee.Array(ridge_kernel.X_fit_.tolist()) self.distance_krr = kernel.RBFDistance(self.gamma) self.kernel_rbf = kernel.Kernel(self.inputs, self.bands_modeling_estimation_input, self.distance_krr, weight_property="weight") # Copy normalization stuff self.inputs_mean = ee.Array(modelo.named_steps["standardscaler"].mean_.tolist()) self.inputs_std = ee.Array(modelo.named_steps["standardscaler"].scale_.tolist()) self.outputs_mean = ee.Array(output_mean.tolist()) self.outputs_std = ee.Array(output_std.tolist()) return