def generate_distances(mols, args): t = time.time() if args.method in ["bob", "cm", "ucm"]: X = np.asarray([mol.descriptor for mol in mols], dtype=float) if args.exponents[0] == 1: D = manhattan_distance(X.T, X.T) else: # prevent overflow in l2 X = X[:, (X > 1.5).all(0)] D = l2_distance(X.T, X.T) else: #X = np.asarray([np.concatenate(mol.local_descriptor) for mol in mols]) #D = manhattan_distance(X.T, X.T) # initialize D = np.zeros((len(mols), len(mols)), dtype=float) resids = np.unique(mols[0].residues) for res in resids: if args.ca: indices = np.where((mols[0].residues == res) & (mols[0].atomnames == "CA"))[0] else: indices = np.where(mols[0].residues == res)[0] D_res = np.zeros((len(mols), len(mols)), dtype=float) for i in indices: X = np.asarray([mol.local_descriptor[i] for mol in mols]) if args.exponents[2] == 1: D_res += manhattan_distance(X.T, X.T)**args.exponents[1] else: D_res += l2_distance(X.T, X.T)**args.exponents[1] if args.exponents[1] == 2: np.sqrt(D_res, out=D_res) D += D_res**args.exponents[0] if args.exponents[0] == 2: np.sqrt(D, out=D) print "Generated distances in %.1f seconds" % (time.time() - t) return D
# Set hyper-parameters sigma = 10**(4.2) llambda = 10**(-10.0) # print "Calculating K-matrix ...", print(u"Calculating: K\u1D62\u2C7C = k(q\u1D62, q\u2C7C) ... ", end="") sys.stdout.flush() start = time.time() # Gaussian kernel usually better for atomic properties # K = gaussian_kernel(X, X, sigma) # Alternatively, just calculate the L2 distance, and convert # to kernel matrix (e.g. for multiple sigmas, etc): D = l2_distance(X, X) D /= -2.0 * sigma * sigma D = np.exp(K) print("%7.2f seconds" % (time.time() - start)) for i in xrange(n_train): K[i, i] += llambda print(u"Calculating: \u03B1 = (K + \u03BBI)\u207B\u00B9 y ... ", end="") sys.stdout.flush() start = time.time() alpha = cho_solve(K, Y) print("%7.2f seconds" % (time.time() - start))
sigma = float(sys.argv[1]) llambda = float(sys.argv[2]) xyz_file = sys.argv[3] mol = Molecule() mol.read_xyz(xyz_file) mol.generate_atomic_coulomb_matrix() Ys = np.zeros((mol.natoms)) s = (-0.5 / sigma**2) print "Running predicition ..." for a, atom in enumerate(mol.atomtypes): Xs = np.reshape(mol.atomic_coulomb_matrix[a], (len(mol.atomic_coulomb_matrix[a]), 1)) Ds = l2_distance(Xs, X[atom]) Ds *= s np.array(np.exp(Ds, Ds)) Ys[a] = np.dot(Ds[0, :], alpha[atom]) error = np.sum(Ys) Ys -= np.mean(Ys) print "Final charges:" for i, q in enumerate(Ys): print "%s %12.8f" % (mol.atomtypes[i], q)