def test_en_kern_vec(params, ihyps): name, cutoffs, hyps_mask_list, _ = params hyps_mask = hyps_mask_list[ihyps] hyps = hyps_mask["hyps"] kernel = str_to_kernel_set(hyps_mask["kernels"], "mc", hyps_mask) np.random.seed(10) test_point = get_tstp() size1 = len(flare.gp_algebra._global_training_data[name]) size2 = len(flare.gp_algebra._global_training_structures[name]) # test the parallel implementation for multihyps vec = en_kern_vec(name, kernel[3], kernel[2], test_point, hyps, cutoffs, hyps_mask) vec_par = en_kern_vec( name, kernel[3], kernel[2], test_point, hyps, cutoffs, hyps_mask, n_cpus=2, n_sample=100, ) assert all(np.equal(vec, vec_par)), "parallel implementation is wrong" assert vec.shape[0] == size1 * 3 + size2
def predict_local_energy(self, x_t: AtomicEnvironment) -> float: """Predict the local energy of a local environment. Args: x_t (AtomicEnvironment): Input local environment. Return: float: Local energy predicted by the GP. """ if self.parallel and not self.per_atom_par: n_cpus = self.n_cpus else: n_cpus = 1 _global_training_data[self.name] = self.training_data _global_training_labels[self.name] = self.training_labels_np k_v = en_kern_vec(self.name, self.energy_force_kernel, x_t, self.hyps, cutoffs=self.cutoffs, hyps_mask=self.hyps_mask, n_cpus=n_cpus, n_sample=self.n_sample) pred_mean = np.matmul(k_v, self.alpha) return pred_mean
def _GenGrid_inner(self, name, s, e, bond_lengths, env12, kernel_info): ''' Calculate kv segments of the given batch of training data for all grids ''' kernel, en_force_kernel, cutoffs, hyps, hyps_mask = kernel_info size = e - s k12_v = np.zeros([len(bond_lengths), size * 3]) for b, r in enumerate(bond_lengths): env12.bond_array_2 = np.array([[r, 1, 0, 0]]) k12_v[b, :] = en_kern_vec(name, s, e, env12, en_force_kernel, hyps, cutoffs, hyps_mask) return k12_v
def _GenGrid_inner(self, name, s, e, bonds1, bonds2, bonds12, env12, kernel_info): ''' Calculate kv segments of the given batch of training data for all grids ''' kernel, en_force_kernel, cutoffs, hyps, hyps_mask = kernel_info # open saved k vector file, and write to new file size = (e - s) * 3 k12_v = np.zeros([len(bonds1), len(bonds2), len(bonds12), size]) for b12, r12 in enumerate(bonds12): for b1, r1 in enumerate(bonds1): for b2, r2 in enumerate(bonds2): env12.bond_array_3 = np.array([[r1, 1, 0, 0], [r2, 0, 0, 0]]) env12.cross_bond_dists = np.array([[0, r12], [r12, 0]]) k12_v[b1, b2, b12, :] = en_kern_vec(name, s, e, env12, en_force_kernel, hyps, cutoffs, hyps_mask) # open saved k vector file, and write to new file if self.update: raise NotImplementedError("the update function is not yet"\ "implemented") s, e = block chunk = e - s new_kv_file = np.zeros( (chunk, self.grid_num[0] * self.grid_num[1] + 1, total_size)) new_kv_file[:, 0, 0] = np.ones(chunk) * total_size for i in range(s, e): kv_filename = f'{self.kv3name}/{i}' if kv_filename in os.listdir(self.kv3name): old_kv_file = np.load(kv_filename + '.npy') last_size = int(old_kv_file[0, 0]) new_kv_file[i, :, :last_size] = old_kv_file else: last_size = 0 ds = [1, 2, 3] nop = self.grid_num[0] k12_v = new_kv_file[:, 1:, :] for i in range(s, e): np.save(f'{self.kv3name}/{i}', new_kv_file[i, :, :]) return k12_v
def predict_local_energy_and_var(self, x_t: AtomicEnvironment): """Predict the local energy of a local environment and its uncertainty. Args: x_t (AtomicEnvironment): Input local environment. Return: (float, float): Mean and predictive variance predicted by the GP. """ if self.parallel and not self.per_atom_par: n_cpus = self.n_cpus else: n_cpus = 1 self.sync_data() # get kernel vector k_v = en_kern_vec( self.name, self.energy_force_kernel, self.energy_kernel, x_t, self.hyps, cutoffs=self.cutoffs, hyps_mask=self.hyps_mask, n_cpus=n_cpus, n_sample=self.n_sample, ) # get predictive mean pred_mean = np.matmul(k_v, self.alpha) # get predictive variance v_vec = solve_triangular(self.l_mat, k_v, lower=True) args = from_mask_to_args(self.hyps, self.cutoffs, self.hyps_mask) self_kern = self.energy_kernel(x_t, x_t, *args) pred_var = self_kern - np.matmul(v_vec, v_vec) return pred_mean, pred_var