def __init__(self, prior=None, kernel=None): if kernel is None: self.kernel = SquaredExponential() else: self.kernel = kernel if prior is None: self.prior = ZeroPrior() else: self.prior = prior
class GaussianProcess(): '''Gaussian Process Regression It is recomended to be used with other Priors and Kernels from ase.optimize.gpmin Parameters: prior: Prior class, as in ase.optimize.gpmin.prior Defaults to ZeroPrior kernel: Kernel function for the regression, as in ase.optimize.gpmin.kernel Defaults to the Squared Exponential kernel with derivatives ''' def __init__(self, prior=None, kernel=None): if kernel is None: self.kernel = SquaredExponential() else: self.kernel = kernel if prior is None: self.prior = ZeroPrior() else: self.prior = prior def set_hyperparams(self, params): '''Set hyperparameters of the regression. This is a list containing the parameters of the kernel and the regularization (noise) of the method as the last entry. ''' self.hyperparams = params self.kernel.set_params(params[:-1]) self.noise = params[-1] def train(self, X, Y, noise=None): '''Produces a PES model from data. Given a set of observations, X, Y, compute the K matrix of the Kernel given the data (and its cholesky factorization) This method should be executed whenever more data is added. Parameters: X: observations(i.e. positions). numpy array with shape: nsamples x D Y: targets (i.e. energy and forces). numpy array with shape (nsamples, D+1) noise: Noise parameter in the case it needs to be restated. ''' if noise is not None: self.noise = noise # Set noise atribute to a different value self.X = X.copy() # Store the data in an atribute K = self.kernel.kernel_matrix(X) # Compute the kernel matrix n = self.X.shape[0] D = self.X.shape[1] regularization = np.array( n * ([self.noise * self.kernel.l**2] + D * [self.noise])) K[range(K.shape[0]), range(K.shape[0])] += regularization**2 self.m = self.prior.prior(X) self.L, self.lower = cho_factor(K, lower=True, check_finite=True) self.a = Y.flatten() - self.m cho_solve((self.L, self.lower), self.a, overwrite_b=True, check_finite=True) def predict(self, x, get_variance=False): '''Given a trained Gaussian Process, it predicts the value and the uncertainty at point x. It returns f and V: f : prediction: [y, grady] V : Covariance matrix. Its diagonal is the variance of each component of f. Parameters: x (1D np.array): The position at which the prediction is computed get_variance (bool): if False, only the prediction f is returned if True, the prediction f and the variance V are returned: Note V is O(D*nsample2)''' n = self.X.shape[0] k = self.kernel.kernel_vector(x, self.X, n) f = self.prior.prior(x) + np.dot(k, self.a) if get_variance: v = k.T.copy() v = solve_triangular(self.L, v, lower=True, check_finite=False) variance = self.kernel.kernel(x, x) #covariance = np.matmul(v.T, v) covariance = np.tensordot(v, v, axes=(0, 0)) V = variance - covariance return f, V return f def neg_log_likelihood(self, l, *args): '''Negative logarithm of the marginal likelihood and its derivative. It has been built in the form that suits the best its optimization, with the scipy minimize module, to find the optimal hyperparameters. Parameters: l: The scale for which we compute the marginal likelihood *args: Should be a tuple containing the inputs and targets in the training set- ''' X, Y = args self.kernel.set_params(np.array([self.kernel.weight, l, self.noise])) self.train(X, Y) y = Y.flatten() # Compute log likelihood logP = -0.5 * np.dot(y-self.m, self.a) - \ np.sum(np.log(np.diag(self.L)))-X.shape[0]*0.5*np.log(2*np.pi) # Gradient of the loglikelihood grad = self.kernel.gradient(X) # vectorizing the derivative of the log likelyhood D_P_input = np.array( [np.dot(np.outer(self.a, self.a), g) for g in grad]) D_complexity = np.array( [cho_solve((self.L, self.lower), g) for g in grad]) DlogP = 0.5 * np.trace(D_P_input - D_complexity, axis1=1, axis2=2) return -logP, -DlogP def fit_hyperparameters(self, X, Y): '''Given a set of observations, X, Y; optimize the scale of the Gaussian Process maximizing the marginal log-likelihood. This method calls TRAIN there is no need to call the TRAIN method again. The method also sets the parameters of the Kernel to their optimal value at the end of execution Parameters: X: observations(i.e. positions). numpy array with shape: nsamples x D Y: targets (i.e. energy and forces). numpy array with shape (nsamples, D+1) ''' l = np.copy(self.hyperparams)[1] arguments = (X, Y) result = minimize(self.neg_log_likelihood, l, args=arguments, method='L-BFGS-B', jac=True) if not result.success: print(result) raise NameError("The Gaussian Process could not be fitted.") else: self.hyperparams = np.array( [self.kernel.weight, result.x.copy(), self.noise]) self.set_hyperparams(self.hyperparams) return self.hyperparams
def __init__(self, atoms, restart=None, logfile='-', trajectory=None, prior=None, master=None, noise=0.005, weight=1., update_prior_strategy='maximum', scale=0.4, force_consistent=None, batch_size=5, update_hyperparams=False): """Optimize atomic positions using GPMin algorithm, which uses both potential energies and forces information to build a PES via Gaussian Process (GP) regression and then minimizes it. Parameters: atoms: Atoms object The Atoms object to relax. restart: string Pickle file used to store the training set. If set, file with such a name will be searched and the data in the file incorporated to the new training set, if the file exists. logfile: file object or str If *logfile* is a string, a file with that name will be opened. Use '-' for stdout trajectory: string Pickle file used to store trajectory of atomic movement. master: boolean Defaults to None, which causes only rank 0 to save files. If set to True, this rank will save files. force_consistent: boolean or None Use force-consistent energy calls (as opposed to the energy extrapolated to 0 K). By default (force_consistent=None) uses force-consistent energies if available in the calculator, but falls back to force_consistent=False if not. prior: Prior object or None Prior for the GP regression of the PES surface See ase.optimize.gpmin.prior If *Prior* is None, then it is set as the ConstantPrior with the constant being updated using the update_prior_strategy specified as a parameter noise: float Regularization parameter for the Gaussian Process Regression. weight: float Prefactor of the Squared Exponential kernel. If *update_hyperparams* is False, changing this parameter has no effect on the dynamics of the algorithm. update_prior_strategy: string Strategy to update the constant from the ConstantPrior when more data is collected. It does only work when Prior = None options: 'maximum': update the prior to the maximum sampled energy 'init' : fix the prior to the initial energy 'average': use the average of sampled energies as prior scale: float scale of the Squared Exponential Kernel update_hyperparams: boolean Update the scale of the Squared exponential kernel every batch_size-th iteration by maximizing the marginal likelhood. batch_size: int Number of new points in the sample before updating the hyperparameters. Only relevant if the optimizer is executed in update mode: (update = True) """ self.nbatch = batch_size self.strategy = update_prior_strategy self.update_hp = update_hyperparams self.function_calls = 1 self.force_calls = 0 self.x_list = [] # Training set features self.y_list = [] # Training set targets Optimizer.__init__(self, atoms, restart, logfile, trajectory, master, force_consistent) if prior is None: self.update_prior = True prior = ConstantPrior(constant=None) else: self.update_prior = False Kernel = SquaredExponential() GaussianProcess.__init__(self, prior, Kernel) self.set_hyperparams(np.array([weight, scale, noise]))
class GaussianProcess(): """Gaussian Process Regression It is recomended to be used with other Priors and Kernels from ase.optimize.gpmin Parameters: prior: Prior class, as in ase.optimize.gpmin.prior Defaults to ZeroPrior kernel: Kernel function for the regression, as in ase.optimize.gpmin.kernel Defaults to the Squared Exponential kernel with derivatives """ def __init__(self, prior=None, kernel=None): if kernel is None: self.kernel = SquaredExponential() else: self.kernel = kernel if prior is None: self.prior = ZeroPrior() else: self.prior = prior def set_hyperparams(self, params): """Set hyperparameters of the regression. This is a list containing the parameters of the kernel and the regularization (noise) of the method as the last entry. """ self.hyperparams = params self.kernel.set_params(params[:-1]) self.noise = params[-1] def train(self, X, Y, noise=None): """Produces a PES model from data. Given a set of observations, X, Y, compute the K matrix of the Kernel given the data (and its cholesky factorization) This method should be executed whenever more data is added. Parameters: X: observations (i.e. positions). numpy array with shape: nsamples x D Y: targets (i.e. energy and forces). numpy array with shape (nsamples, D+1) noise: Noise parameter in the case it needs to be restated. """ if noise is not None: self.noise = noise # Set noise attribute to a different value self.X = X.copy() # Store the data in an attribute n = self.X.shape[0] D = self.X.shape[1] regularization = np.array( n * ([self.noise * self.kernel.l] + D * [self.noise])) K = self.kernel.kernel_matrix(X) # Compute the kernel matrix K[range(K.shape[0]), range(K.shape[0])] += regularization**2 self.m = self.prior.prior(X) self.a = Y.flatten() - self.m self.L, self.lower = cho_factor(K, lower=True, check_finite=True) cho_solve((self.L, self.lower), self.a, overwrite_b=True, check_finite=True) def predict(self, x, get_variance=False): """Given a trained Gaussian Process, it predicts the value and the uncertainty at point x. It returns f and V: f : prediction: [y, grady] V : Covariance matrix. Its diagonal is the variance of each component of f. Parameters: x (1D np.array): The position at which the prediction is computed get_variance (bool): if False, only the prediction f is returned if True, the prediction f and the variance V are returned: Note V is O(D*nsample2) """ n = self.X.shape[0] k = self.kernel.kernel_vector(x, self.X, n) f = self.prior.prior(x) + np.dot(k, self.a) if get_variance: v = solve_triangular(self.L, k.T.copy(), lower=True, check_finite=False) variance = self.kernel.kernel(x, x) # covariance = np.matmul(v.T, v) covariance = np.tensordot(v, v, axes=(0, 0)) V = variance - covariance return f, V return f def neg_log_likelihood(self, params, *args): """Negative logarithm of the marginal likelihood and its derivative. It has been built in the form that suits the best its optimization, with the scipy minimize module, to find the optimal hyperparameters. Parameters: l: The scale for which we compute the marginal likelihood *args: Should be a tuple containing the inputs and targets in the training set- """ X, Y = args # Come back to this self.kernel.set_params(np.array([params[0], params[1], self.noise])) self.train(X, Y) y = Y.flatten() # Compute log likelihood logP = (-0.5 * np.dot(y - self.m, self.a) - np.sum(np.log(np.diag(self.L))) - X.shape[0] * 0.5 * np.log(2 * np.pi)) # Gradient of the loglikelihood grad = self.kernel.gradient(X) # vectorizing the derivative of the log likelihood D_P_input = np.array( [np.dot(np.outer(self.a, self.a), g) for g in grad]) D_complexity = np.array( [cho_solve((self.L, self.lower), g) for g in grad]) DlogP = 0.5 * np.trace(D_P_input - D_complexity, axis1=1, axis2=2) return -logP, -DlogP def fit_hyperparameters(self, X, Y, tol=1e-2, eps=None): """Given a set of observations, X, Y; optimize the scale of the Gaussian Process maximizing the marginal log-likelihood. This method calls TRAIN there is no need to call the TRAIN method again. The method also sets the parameters of the Kernel to their optimal value at the end of execution Parameters: X: observations(i.e. positions). numpy array with shape: nsamples x D Y: targets (i.e. energy and forces). numpy array with shape (nsamples, D+1) tol: tolerance on the maximum component of the gradient of the log-likelihood. (See scipy's L-BFGS-B documentation: https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html) eps: include bounds to the hyperparameters as a +- a percentage if eps is None there are no bounds in the optimization Returns: result (dict) : result = {'hyperparameters': (numpy.array) New hyperparameters, 'converged': (bool) True if it converged, False otherwise } """ params = np.copy(self.hyperparams)[:2] arguments = (X, Y) if eps is not None: bounds = [((1 - eps) * p, (1 + eps) * p) for p in params] else: bounds = None result = minimize(self.neg_log_likelihood, params, args=arguments, method='L-BFGS-B', jac=True, bounds=bounds, options={ 'gtol': tol, 'ftol': 0.01 * tol }) if not result.success: converged = False else: converged = True self.hyperparams = np.array( [result.x.copy()[0], result.x.copy()[1], self.noise]) self.set_hyperparams(self.hyperparams) return {'hyperparameters': self.hyperparams, 'converged': converged}
def __init__(self, atoms, restart=None, logfile='-', trajectory=None, prior=None, kernel=None, master=None, noise=None, weight=None, scale=None, force_consistent=None, batch_size=None, bounds=None, update_prior_strategy="maximum", update_hyperparams=False): """Optimize atomic positions using GPMin algorithm, which uses both potential energies and forces information to build a PES via Gaussian Process (GP) regression and then minimizes it. Default behaviour: -------------------- The default values of the scale, noise, weight, batch_size and bounds parameters depend on the value of update_hyperparams. In order to get the default value of any of them, they should be set up to None. Default values are: update_hyperparams = True scale : 0.3 noise : 0.004 weight: 2. bounds: 0.1 batch_size: 1 update_hyperparams = False scale : 0.4 noise : 0.005 weight: 1. bounds: irrelevant batch_size: irrelevant Parameters: ------------------ atoms: Atoms object The Atoms object to relax. restart: string JSON file used to store the training set. If set, file with such a name will be searched and the data in the file incorporated to the new training set, if the file exists. logfile: file object or str If *logfile* is a string, a file with that name will be opened. Use '-' for stdout trajectory: string File used to store trajectory of atomic movement. master: boolean Defaults to None, which causes only rank 0 to save files. If set to True, this rank will save files. force_consistent: boolean or None Use force-consistent energy calls (as opposed to the energy extrapolated to 0 K). By default (force_consistent=None) uses force-consistent energies if available in the calculator, but falls back to force_consistent=False if not. prior: Prior object or None Prior for the GP regression of the PES surface See ase.optimize.gpmin.prior If *prior* is None, then it is set as the ConstantPrior with the constant being updated using the update_prior_strategy specified as a parameter kernel: Kernel object or None Kernel for the GP regression of the PES surface See ase.optimize.gpmin.kernel If *kernel* is None the SquaredExponential kernel is used. Note: It needs to be a kernel with derivatives!!!!! noise: float Regularization parameter for the Gaussian Process Regression. weight: float Prefactor of the Squared Exponential kernel. If *update_hyperparams* is False, changing this parameter has no effect on the dynamics of the algorithm. update_prior_strategy: string Strategy to update the constant from the ConstantPrior when more data is collected. It does only work when Prior = None options: 'maximum': update the prior to the maximum sampled energy 'init' : fix the prior to the initial energy 'average': use the average of sampled energies as prior scale: float scale of the Squared Exponential Kernel update_hyperparams: boolean Update the scale of the Squared exponential kernel every batch_size-th iteration by maximizing the marginal likelihood. batch_size: int Number of new points in the sample before updating the hyperparameters. Only relevant if the optimizer is executed in update_hyperparams mode: (update_hyperparams = True) bounds: float, 0<bounds<1 Set bounds to the optimization of the hyperparameters. Let t be a hyperparameter. Then it is optimized under the constraint (1-bound)*t_0 <= t <= (1+bound)*t_0 where t_0 is the value of the hyperparameter in the previous step. If bounds is False, no constraints are set in the optimization of the hyperparameters. .. warning:: The memory of the optimizer scales as O(n²N²) where N is the number of atoms and n the number of steps. If the number of atoms is sufficiently high, this may cause a memory issue. This class prints a warning if the user tries to run GPMin with more than 100 atoms in the unit cell. """ # Warn the user if the number of atoms is very large if len(atoms) > 100: warning = ('Possible Memory Issue. There are more than ' '100 atoms in the unit cell. The memory ' 'of the process will increase with the number ' 'of steps, potentially causing a memory issue. ' 'Consider using a different optimizer.') warnings.warn(warning) # Give it default hyperparameters if update_hyperparams: # Updated GPMin if scale is None: scale = 0.3 if noise is None: noise = 0.004 if weight is None: weight = 2. if bounds is None: self.eps = 0.1 elif bounds is False: self.eps = None else: self.eps = bounds if batch_size is None: self.nbatch = 1 else: self.nbatch = batch_size else: # GPMin without updates if scale is None: scale = 0.4 if noise is None: noise = 0.001 if weight is None: weight = 1. if bounds is not None: warning = ('The parameter bounds is of no use ' 'if update_hyperparams is False. ' 'The value provided by the user ' 'is being ignored.') warnings.warn(warning, UserWarning) if batch_size is not None: warning = ('The parameter batch_size is of no use ' 'if update_hyperparams is False. ' 'The value provided by the user ' 'is being ignored.') warnings.warn(warning, UserWarning) # Set the variables to something anyways self.eps = False self.nbatch = None self.strategy = update_prior_strategy self.update_hp = update_hyperparams self.function_calls = 1 self.force_calls = 0 self.x_list = [] # Training set features self.y_list = [] # Training set targets Optimizer.__init__(self, atoms, restart, logfile, trajectory, master, force_consistent) if prior is None: self.update_prior = True prior = ConstantPrior(constant=None) else: self.update_prior = False if kernel is None: kernel = SquaredExponential() GaussianProcess.__init__(self, prior, kernel) self.set_hyperparams(np.array([weight, scale, noise]))