def LL_sor_loss(x_opt, X, y, cv, jitter, disable_pbar=True, leave=False): Lambda = x_opt[0] kMM = X[0] kMN = X[1] Mactive, Nsample = kMN.shape kernel_ = kMM + np.dot(kMN, kMN.T) / Lambda**2 + np.diag( np.ones(Mactive)) * jitter y_ = np.dot(kMN, y) / Lambda**2 # Get log likelihood score L = np.linalg.cholesky(kernel_) z = sp.linalg.solve_triangular(L, y_, lower=True) alpha = sp.linalg.solve_triangular(L.T, z, lower=False, overwrite_b=True).flatten() #alpha = np.linalg.solve(kernel_train, y_train).flatten() #diag = np.zeros((Mactive)) #for ii in range(Mactive): diag[ii] = L[ii,ii] logDet = 0 for ii in range(Mactive): logDet += np.log(L[ii, ii]) logL = -0.5 * Mactive * np.log(2 * np.pi) - 0.5 * np.dot( y_.flatten(), alpha) - logDet return logL
def sor_loss(x_opt, X, y, cv, jitter, disable_pbar=True, leave=False, return_score=False): Lambda = x_opt[0] kMM = X[0] kMN = X[1] Mactive, Nsample = kMN.shape mse = 0 y_p = np.zeros((Nsample, )) scores = [] for train, test in tqdm_cs(cv.split(kMN.T), total=cv.n_splits, disable=disable_pbar, leave=False): # prepare SoR kernel kMN_train = kMN[:, train] kernel_train = kMM + np.dot(kMN_train, kMN_train.T) / Lambda**2 + np.diag( np.ones(Mactive)) * jitter y_train = np.dot(kMN_train, y[train]) / Lambda**2 # train the KRR model alpha = np.linalg.solve(kernel_train, y_train).flatten() # make predictions kernel_test = kMN[:, test] y_pred = np.dot(alpha, kernel_test).flatten() if return_score is True: scores.append(get_score(y_pred, y[test])) #y_p[test] = y_pred mse += np.sum((y_pred - y[test])**2) mse /= len(y) if return_score is True: #score = get_score(y_p,y) score = {} for k in scores[0]: aa = [] for sc in scores: aa.append(sc[k]) score[k] = np.mean(aa) return score return mse
def soap_cov_loss(x_opt, rawsoaps, y, cv, jitter, disable_pbar=True, leave=False, compressor=None, active_ids=None, return_score=False): Lambda = x_opt[0] fj = x_opt[1:] compressor.set_scaling_weights(fj) X = compressor.transform(rawsoaps) X_pseudo = X[active_ids] kMM = np.dot(X_pseudo, X_pseudo.T) kMN = np.dot(X_pseudo, X.T) Mactive, Nsample = kMN.shape mse = 0 y_p = np.zeros((Nsample, )) for train, test in tqdm_cs(cv.split(rawsoaps), total=cv.n_splits, disable=disable_pbar, leave=False): # prepare SoR kernel kMN_train = kMN[:, train] kernel_train = (kMM + np.dot(kMN_train, kMN_train.T) / Lambda**2) + np.diag(np.ones(Mactive)) * jitter y_train = np.dot(kMN_train, y[train]) / Lambda**2 # train the KRR model alpha = np.linalg.solve(kernel_train, y_train).flatten() # make predictions kernel_test = kMN[:, test] y_pred = np.dot(alpha, kernel_test).flatten() if return_score is True: y_p[test] = y_pred mse += np.sum((y_pred - y[test])**2) mse /= len(y) if return_score is True: score = get_score(y_p, y) return score return mse
def sor_fj_loss(x_opt, data, y, cv, jitter, disable_pbar=True, leave=False, kernel=None, compressor=None, strides=None, active_strides=None, stride_size=None, return_score=False): Lambda = x_opt[0] scaling_factors = x_opt[1:] compressor.to_reshape = False compressor.set_scaling_weights(scaling_factors) unlinsoaps = data[0] unlinsoaps_active = data[1] X = compressor.scale_features(unlinsoaps, stride_size) X_active = compressor.scale_features(unlinsoaps_active, stride_size) # X = compressor.transform(unlinsoaps) # X_active = compressor.transform(unlinsoaps_active) if strides is not None and active_strides is not None: X_active = dict(strides=active_strides, feature_matrix=X_active) X = dict(strides=strides, feature_matrix=X) kMM = kernel.transform(X_active, X_train=X_active) kMN = kernel.transform(X_active, X_train=X) Mactive, Nsample = kMN.shape mse = 0 y_p = np.zeros((Nsample, )) scores = [] for train, test in tqdm_cs(cv.split(y.reshape((-1, 1))), total=cv.n_splits, disable=disable_pbar, leave=False): # prepare SoR kernel kMN_train = kMN[:, train] kernel_train = (kMM + np.dot(kMN_train, kMN_train.T) / Lambda**2) + np.diag(np.ones(Mactive)) * jitter y_train = np.dot(kMN_train, y[train]) / Lambda**2 # train the KRR model alpha = np.linalg.solve(kernel_train, y_train).flatten() # make predictions kernel_test = kMN[:, test] y_pred = np.dot(alpha, kernel_test).flatten() if return_score is True: scores.append(get_score(y_pred, y[test])) #y_p[test] = y_pred mse += np.sum((y_pred - y[test])**2) mse /= len(y) if return_score is True: #score = get_score(y_p,y) score = {} for k in scores[0]: aa = [] for sc in scores: aa.append(sc[k]) score[k] = np.mean(aa) return score return mse
Nsample = Kmat.shape[0] # trainer = TrainerCholesky(memory_efficient=True) # model = KRR(jitter,delta,trainer) lc = LCSplit(shuffler, **lc_params) scores = [] results = dict(input_params=inp,results=[]) ii = 0 for train,test in tqdm_cs(lc.split(y.reshape((-1,1))),total=lc.n_splits,desc='LC'): if ii >= start_from_iter: if is_SoR is True: Mactive = kMN.shape[0] kMN_train = kMN[:,train] k_train = kMM + np.dot(kMN_train,kMN_train.T)/Lambda**2 + np.diag(np.ones(Mactive))*jitter y_train = np.dot(kMN_train,y[train])/Lambda**2 k_test = kMN[:,test] else: Ntrain = len(train) k_train = Kmat[np.ix_(train,train)] + np.diag(np.ones(Ntrain))*jitter y_train = y[train] k_test = Kmat[np.ix_(train,test)] alpha = np.linalg.solve(k_train, y_train).flatten() y_pred = np.dot(alpha,k_test).flatten() #model.fit(k_train,y_train) #y_pred = model.predict(k_test) sc = get_score(y_pred,y[test]) dd = dict(Ntrain = len(train), Ntest = len(test),iter=ii)