def test_unique(self): size = ht.MPI_WORLD.size rank = ht.MPI_WORLD.rank torch_array = torch.arange(size, dtype=torch.int32, device=device).expand(size, size) split_zero = ht.array(torch_array, split=0, device=ht_device) exp_axis_none = ht.array([rank], dtype=ht.int32, device=ht_device) res = split_zero.unique(sorted=True) self.assertTrue((res._DNDarray__array == exp_axis_none._DNDarray__array).all()) exp_axis_zero = ht.arange(size, dtype=ht.int32, device=ht_device).expand_dims(0) res = ht.unique(split_zero, sorted=True, axis=0) self.assertTrue((res._DNDarray__array == exp_axis_zero._DNDarray__array).all()) exp_axis_one = ht.array([rank], dtype=ht.int32, device=ht_device).expand_dims(0) split_zero_transposed = ht.array(torch_array.transpose(0, 1), split=0, device=ht_device) res = ht.unique(split_zero_transposed, sorted=False, axis=1) self.assertTrue((res._DNDarray__array == exp_axis_one._DNDarray__array).all()) split_one = ht.array(torch_array, dtype=ht.int32, split=1, device=ht_device) exp_axis_none = ht.arange(size, dtype=ht.int32, device=ht_device) res = ht.unique(split_one, sorted=True) self.assertTrue((res._DNDarray__array == exp_axis_none._DNDarray__array).all()) exp_axis_zero = ht.array([rank], dtype=ht.int32, device=ht_device).expand_dims(0) res = ht.unique(split_one, sorted=False, axis=0) self.assertTrue((res._DNDarray__array == exp_axis_zero._DNDarray__array).all()) exp_axis_one = ht.array([rank] * size, dtype=ht.int32, device=ht_device).expand_dims(1) res = ht.unique(split_one, sorted=True, axis=1) self.assertTrue((res._DNDarray__array == exp_axis_one._DNDarray__array).all()) torch_array = torch.tensor( [[1, 2], [2, 3], [1, 2], [2, 3], [1, 2]], dtype=torch.int32, device=device ) data = ht.array(torch_array, split=0, device=ht_device) res, inv = ht.unique(data, return_inverse=True, axis=0) _, exp_inv = torch_array.unique(dim=0, return_inverse=True, sorted=True) self.assertTrue(torch.equal(inv, exp_inv.to(dtype=inv.dtype))) res, inv = ht.unique(data, return_inverse=True, axis=1) _, exp_inv = torch_array.unique(dim=1, return_inverse=True, sorted=True) self.assertTrue(torch.equal(inv, exp_inv.to(dtype=inv.dtype))) torch_array = torch.tensor( [[1, 1, 2], [1, 2, 2], [2, 1, 2], [1, 3, 2], [0, 1, 2]], dtype=torch.int32, device=device, ) exp_res, exp_inv = torch_array.unique(return_inverse=True, sorted=True) data_split_none = ht.array(torch_array, device=ht_device) res, inv = ht.unique(data_split_none, return_inverse=True, sorted=True) self.assertTrue(torch.equal(inv, exp_inv.to(dtype=inv.dtype))) data_split_zero = ht.array(torch_array, split=0, device=ht_device) res, inv = ht.unique(data_split_zero, return_inverse=True, sorted=True) self.assertTrue(torch.equal(inv, exp_inv.to(dtype=inv.dtype)))
def fit(self, X, y, sample_weight=None): """ Fit Gaussian Naive Bayes according to X, y Parameters ---------- X : ht.tensor of shape (n_samples, n_features) Training set, where n_samples is the number of samples and n_features is the number of features. y : ht.tensor of shape (n_samples,) Labels for training set. sample_weight : ht.tensor of shape (n_samples,), optional (default=None) Weights applied to individual samples (1. for unweighted). Returns ------- self : object """ # sanitize input - to be moved to sanitation module, cf. #468 if not isinstance(X, ht.DNDarray): raise ValueError( "input needs to be a ht.DNDarray, but was {}".format(type(X))) if not isinstance(y, ht.DNDarray): raise ValueError( "input needs to be a ht.DNDarray, but was {}".format(type(y))) if y.numdims != 1: raise ValueError("expected y to be a 1-D tensor, is {}-D".format( y.numdims)) if sample_weight is not None: if not isinstance(sample_weight, ht.DNDarray): raise ValueError( "sample_weight needs to be a ht.DNDarray, but was {}". format(type(sample_weight))) classes = ht.unique(y, sorted=True) if classes.split is not None: classes = ht.resplit(classes, axis=None) return self.__partial_fit(X, y, classes, _refit=True, sample_weight=sample_weight)
def fit(self, x: DNDarray, y: DNDarray, sample_weight: Optional[DNDarray] = None): """ Fit Gaussian Naive Bayes according to ``x`` and ``y`` Parameters ---------- x : DNDarray Training set, where n_samples is the number of samples and n_features is the number of features. Shape = (n_classes, n_features) y : DNDarray Labels for training set. Shape = (n_samples, ) sample_weight : DNDarray, optional Weights applied to individual samples (1. for unweighted). Shape = (n_samples, ) """ # sanitize input - to be moved to sanitation module, cf. #468 if not isinstance(x, ht.DNDarray): raise ValueError( "input needs to be a ht.DNDarray, but was {}".format(type(x))) if not isinstance(y, ht.DNDarray): raise ValueError( "input needs to be a ht.DNDarray, but was {}".format(type(y))) if y.ndim != 1: raise ValueError("expected y to be a 1-D tensor, is {}-D".format( y.ndim)) if sample_weight is not None: if not isinstance(sample_weight, ht.DNDarray): raise ValueError( "sample_weight needs to be a ht.DNDarray, but was {}". format(type(sample_weight))) classes = ht.unique(y, sorted=True) if classes.split is not None: classes = ht.resplit(classes, axis=None) return self.__partial_fit(x, y, classes, _refit=True, sample_weight=sample_weight)
def __partial_fit(self, X, y, classes=None, _refit=False, sample_weight=None): """ Actual implementation of Gaussian NB fitting. Adapted to HeAT from scikit-learn. Parameters ---------- X : ht.tensor of shape (n_samples, n_features) Training set, where n_samples is the number of samples and n_features is the number of features. y : ht.tensor of shape (n_samples,) Labels for training set. classes : ht.tensor of shape (n_classes,), optional (default=None) List of all the classes that can possibly appear in the y vector. Must be provided at the first call to partial_fit, can be omitted in subsequent calls. _refit : bool, optional (default=False) If true, act as though this were the first time __partial_fit is called (ie, throw away any past fitting and start over). sample_weight : ht.tensor of shape (n_samples,), optional (default=None) Weights applied to individual samples (1. for unweighted). Returns ------- self : object """ # TODO: sanitize X and y shape: sanitation/validation module, cf. #468 n_samples = X.shape[0] if X.numdims != 2: raise ValueError("expected X to be a 2-D tensor, is {}-D".format( X.numdims)) if y.shape[0] != n_samples: raise ValueError( "y.shape[0] must match number of samples {}, is {}".format( n_samples, y.shape[0])) # TODO: sanitize sample_weight: sanitation/validation module, cf. #468 if sample_weight is not None: if sample_weight.numdims != 1: raise ValueError("Sample weights must be 1D tensor") if sample_weight.shape != (n_samples, ): raise ValueError( "sample_weight.shape == {}, expected {}!".format( sample_weight.shape, (n_samples, ))) # If the ratio of data variance between dimensions is too small, it # will cause numerical errors. To address this, we artificially # boost the variance by epsilon, a small fraction of the standard # deviation of the largest dimension. self.epsilon_ = self.var_smoothing * ht.var(X, axis=0).max() if _refit: self.classes_ = None if self.__check_partial_fit_first_call(classes): # This is the first call to partial_fit: # initialize various cumulative counters n_features = X.shape[1] n_classes = len(self.classes_) self.theta_ = ht.zeros((n_classes, n_features), dtype=X.dtype, device=X.device) self.sigma_ = ht.zeros((n_classes, n_features), dtype=X.dtype, device=X.device) self.class_count_ = ht.zeros((n_classes, ), dtype=ht.float64, device=X.device) # Initialise the class prior # Take into account the priors if self.priors is not None: if not isinstance(self.priors, ht.DNDarray): priors = ht.array(self.priors, dtype=X.dtype, split=None, device=X.device) else: priors = self.priors # Check that the provide prior match the number of classes if len(priors) != n_classes: raise ValueError("Number of priors must match number of" " classes.") # Check that the sum is 1 if not ht.isclose(priors.sum(), ht.array(1.0, dtype=priors.dtype)): raise ValueError("The sum of the priors should be 1.") # Check that the prior are non-negative if (priors < 0).any(): raise ValueError("Priors must be non-negative.") self.class_prior_ = priors else: # Initialize the priors to zeros for each class self.class_prior_ = ht.zeros(len(self.classes_), dtype=ht.float64, split=None, device=X.device) else: if X.shape[1] != self.theta_.shape[1]: raise ValueError( "Number of features {} does not match previous data {}.". format(X.shape[1], self.theta_.shape[1])) # Put epsilon back in each time self.sigma_[:, :] -= self.epsilon_ classes = self.classes_ unique_y = ht.unique(y, sorted=True) if unique_y.split is not None: unique_y = ht.resplit(unique_y, axis=None) unique_y_in_classes = ht.eq(unique_y, classes) if not ht.all(unique_y_in_classes): raise ValueError("The target label(s) {} in y do not exist in the " "initial classes {}".format( unique_y[~unique_y_in_classes], classes)) for y_i in unique_y: # assuming classes.split is None if y_i in classes: i = ht.where(classes == y_i).item() else: classes_ext = torch.cat((classes._DNDarray__array, y_i._DNDarray__array.unsqueeze(0))) i = torch.argsort(classes_ext)[-1].item() where_y_i = ht.where(y == y_i)._DNDarray__array.tolist() X_i = X[where_y_i, :] if sample_weight is not None: sw_i = sample_weight[where_y_i] if 0 not in sw_i.shape: N_i = sw_i.sum() else: N_i = 0.0 sw_i = None else: sw_i = None N_i = X_i.shape[0] new_theta, new_sigma = self.__update_mean_variance( self.class_count_[i], self.theta_[i, :], self.sigma_[i, :], X_i, sw_i) self.theta_[i, :] = new_theta self.sigma_[i, :] = new_sigma self.class_count_[i] += N_i self.sigma_[:, :] += self.epsilon_ # Update if only no priors is provided if self.priors is None: # Empirical prior, with sample_weight taken into account self.class_prior_ = self.class_count_ / self.class_count_.sum() return self
def __partial_fit( self, x: DNDarray, y: DNDarray, classes: Optional[DNDarray] = None, _refit: bool = False, sample_weight: Optional[DNDarray] = None, ): """ Actual implementation of Gaussian NB fitting. Adapted to HeAT from scikit-learn. Parameters ---------- x : DNDarray Training set, where n_samples is the number of samples and n_features is the number of features. Shape = (n_samples, n_features) y : DNDarray Labels for training set. Shape = (n_samples,) classes : DNDarray, optional List of all the classes that can possibly appear in the y vector. Must be provided at the first call to :func:`partial_fit`, can be omitted in subsequent calls. Shape = (n_classes,) _refit : bool, optional If ``True``, act as though this were the first time :func:`__partial_fit` is called (ie, throw away any past fitting and start over). sample_weight : DNDarray, optional Weights applied to individual samples (1. for unweighted). Shape = (n_samples,) """ # TODO: sanitize x and y shape: sanitation/validation module, cf. #468 n_samples = x.shape[0] if x.ndim != 2: raise ValueError("expected x to be a 2-D tensor, is {}-D".format( x.ndim)) if y.shape[0] != n_samples: raise ValueError( "y.shape[0] must match number of samples {}, is {}".format( n_samples, y.shape[0])) # TODO: sanitize sample_weight: sanitation/validation module, cf. #468 if sample_weight is not None: if sample_weight.ndim != 1: raise ValueError("Sample weights must be 1D tensor") if sample_weight.shape != (n_samples, ): raise ValueError( "sample_weight.shape == {}, expected {}!".format( sample_weight.shape, (n_samples, ))) # If the ratio of data variance between dimensions is too small, it # will cause numerical errors. To address this, we artificially # boost the variance by epsilon, a small fraction of the standard # deviation of the largest dimension. self.epsilon_ = self.var_smoothing * ht.var(x, axis=0).max() if _refit: self.classes_ = None if self.__check_partial_fit_first_call(classes): # This is the first call to partial_fit: # initialize various cumulative counters n_features = x.shape[1] n_classes = len(self.classes_) self.theta_ = ht.zeros((n_classes, n_features), dtype=x.dtype, device=x.device) self.sigma_ = ht.zeros((n_classes, n_features), dtype=x.dtype, device=x.device) self.class_count_ = ht.zeros((x.comm.size, n_classes), dtype=ht.float64, device=x.device, split=0) # Initialise the class prior # Take into account the priors if self.priors is not None: if not isinstance(self.priors, ht.DNDarray): priors = ht.array(self.priors, dtype=x.dtype, split=None, device=x.device) else: priors = self.priors # Check that the provide prior match the number of classes if len(priors) != n_classes: raise ValueError("Number of priors must match number of" " classes.") # Check that the sum is 1 if not ht.isclose(priors.sum(), ht.array(1.0, dtype=priors.dtype)): raise ValueError("The sum of the priors should be 1.") # Check that the prior are non-negative if (priors < 0).any(): raise ValueError("Priors must be non-negative.") self.class_prior_ = priors else: # Initialize the priors to zeros for each class self.class_prior_ = ht.zeros(len(self.classes_), dtype=ht.float64, split=None, device=x.device) else: if x.shape[1] != self.theta_.shape[1]: raise ValueError( "Number of features {} does not match previous data {}.". format(x.shape[1], self.theta_.shape[1])) # Put epsilon back in each time self.sigma_[:, :] -= self.epsilon_ classes = self.classes_ unique_y = ht.unique(y, sorted=True).resplit_(None) unique_y_in_classes = ht.eq(unique_y, classes) if not ht.all(unique_y_in_classes): raise ValueError("The target label(s) {} in y do not exist in the " "initial classes {}".format( unique_y[~unique_y_in_classes], classes)) # from now on: extract torch tensors for local operations # DNDarrays for distributed operations only for y_i in unique_y.larray: # assuming classes.split is None if y_i in classes.larray: i = torch.where(classes.larray == y_i)[0].item() else: classes_ext = torch.cat( (classes.larray, y_i.larray.unsqueeze(0))) i = torch.argsort(classes_ext)[-1].item() where_y_i = torch.where(y.larray == y_i)[0] X_i = x[where_y_i, :] if sample_weight is not None: sw_i = sample_weight[where_y_i] if 0 not in sw_i.shape: N_i = sw_i.sum().item() else: N_i = 0.0 sw_i = None else: sw_i = None N_i = X_i.shape[0] new_theta, new_sigma = self.__update_mean_variance( self.class_count_.larray[:, i].item(), self.theta_[i, :], self.sigma_[i, :], X_i, sw_i, ) self.theta_[i, :] = new_theta self.sigma_[i, :] = new_sigma self.class_count_.larray[:, i] += N_i self.sigma_[:, :] += self.epsilon_ # Update only if no priors are provided if self.priors is None: # distributed class_count_: sum along distribution axis self.class_count_ = self.class_count_.sum(axis=0, keepdim=True) # Empirical prior, with sample_weight taken into account self.class_prior_ = (self.class_count_ / self.class_count_.sum()).squeeze(0) return self