def nn_guess(mol, S, P0=None): """This method returns a guess for the density matrix of a molecule that can be used as input for scf calculations. Until now only the diagonal elements are estimated by the network, while the rest is Args: - atoms <list<str>>: a list of chemical symbols for the atoms in the molecule - S <np.array>: the density matrix of the molecule - P0 <np.array>: a (cheap) initial guess in which the network guess (which calculates only the diagonal) shall be placed. Returns: - <np.array>: a matrix containing the initial guess for the density matrix """ atoms = mol.species sess = tf.Session() #--- acquire model and setup graph --- model_database = normpath("./models") networks = {} for species in list(set(atoms)): model = np.load(join(model_database, species + ".npy"), encoding="bytes") nn = EluFixedValue(*model) nn.setup() networks[species] = nn sess.run(tf.global_variables_initializer()) #--- # if no init guess is given used pyscf default guess if P0 is None: P0 = init_guess_by_minao(mol.get_pyscf_molecule()) dm = [] for atom_index, atom in enumerate(atoms): inputs = [Descriptor.values(S, atoms, atom_index)] dm += (list(networks[atom].run(sess, inputs))) # overwrite the diag of P0 np.fill_diagonal(P0, dm) return P0
def main(species="H"): #--- assemble the dataset --- root_directory = normpath(join(dirname(realpath(__file__)), "../")) dataset_source_folder = join(root_directory, "dataset/") sources = [ join(dataset_source_folder, directory) \ for directory in ["GMTKN55"] ] dataset = Dataset(*assemble_batch(sources, species)) #--- #--- setup and train the network --- dim = N_BASIS[species] structure = [dim, 25, dim] network = EluTrNNN(structure) network, sess = train_network(network, dataset) #--- save_path = join(root_directory, "tmp" + species + ".npy") #try: #--- save trained model --- save_object = [ network.structure, network.weights_values(sess), network.biases_values(sess) ] np.save(save_path, save_object) sess.close() msg.info("Session closed", 1) #--- #--- load and reinitialize model --- msg.info("Starting new session and loading the model ...", 1) sess = tf.Session() model = np.load(save_path) new_network = EluFixedValue(*model) new_network.setup() sess.run(tf.global_variables_initializer()) #finally: if isfile(save_path): remove(save_path)
def main(): #todo this funciton and taining should become part of the library!! # sodass man nur mehr savepath und dataset angeben muss! msg.info("Traing a network for butadien", 2) msg.info("Fetching dataset ... ", 2) dataset = prep_dataset() save_path = "butadien/data/networks/networkS400.npy" user_input = msg.input( "This will overwrite the model at " + save_path + \ "Are you sure you want that? (y for yes)" ) if user_input.upper() != "Y": msg.info("Aborting", 2) return msg.info("Try to fetch current model") try: model = np.load(save_path, encoding="latin1") structure, weights, biases = model[0], model[1], model[2] network = EluFixedValue(structure, weights, biases) test_error = model[3] user_input = msg.input( "Model found with test error : " + str(test_error) + \ ". Do you want to continue to train it? (y for yes)" ) if user_input.upper() != "Y": msg.info("Creating new network", 2) model = None except: model = None if model is None: dim_triu = int(DIM * (DIM + 1) / 2) structure = [ dim_triu, int(dim_triu * 0.75), int(dim_triu * 0.5), dim_triu, dim_triu ] test_error = 1e10 msg.info("Train ... ", 2) network = EluTrNNN(structure) train_network(dataset, network, save_path, test_error) msg.info("All done. Bye bye..", 2)
def test_load_model(self): tf.reset_default_graph() model_path = "tests/data/C_model.npy" try: sess = tf.Session() #load model model = np.load(model_path, encoding="latin1") structure, weights, biases = model[0], model[1], model[2] network = EluFixedValue(structure, weights, biases) y = network.setup() x = network.input_tensor # try to run the network x_result = sess.run(y, feed_dict={y: np.random.rand(1, structure[0])}) except Exception as ex: self.fail("Network could not be loaded: " + str(ex))
def do_analysis(network_path, dataset, molecules, s_raw, log_file): #--- fetch network --- msg.info("Fetching pretained network ", 2) graph = tf.Graph() structure, weights, biases = np.load( network_path, encoding="latin1" )[:3] with graph.as_default(): sess = tf.Session() network = EluFixedValue(structure, weights, biases) network.setup() sess.run(tf.global_variables_initializer()) #--- #--- calculate guesses --- msg.info("Calculating guesses ...",2) msg.info("Neural network ", 1) p_nn = network.run(sess, dataset.testing[0]) msg.info("McWheenys", 1) p_batch = make_matrix_batch(p_nn, DIM, True) p_mcw1 = np.array(list(map(lambda x: multi_mc_wheeny(x[0], x[1], n_max=1), zip(p_batch, s_raw)))) p_mcw5 = np.array(list(map(lambda x: multi_mc_wheeny(x[0], x[1], n_max=5), zip (p_batch, s_raw)))) msg.info("Classics", 1) p_1e = np.array([ hf.init_guess_by_1e(mol.get_pyscf_molecule()) for mol in molecules[1] ]) p_sap = np.array([ hf.init_guess_by_atom(mol.get_pyscf_molecule()) for mol in molecules[1] ]) p_minao = np.array([ hf.init_guess_by_minao(mol.get_pyscf_molecule()) for mol in molecules[1] ]) p_gwh = np.array([ hf.init_guess_by_wolfsberg_helmholtz(mol.get_pyscf_molecule()) for mol in molecules[1] ]) #--- msg.info("Results NN: ", 1) with open(log_file, "a+") as f: f.write("\n+++++ Plain NN +++++\n") measure_and_display( p_nn, dataset, molecules, True, log_file, s=s_raw ) with open(log_file, "a+") as f: f.write("\n\n+++++ McW 1 +++++\n") msg.info("Results McWheeny 1: ",1) measure_and_display( p_mcw1.reshape(-1, DIM**2), dataset, molecules, False, log_file, s=s_raw ) with open(log_file, "a+") as f: f.write("\n\n+++++ McW 5 +++++\n") msg.info("Results McWheeny 5: ", 1) measure_and_display( p_mcw5.reshape(-1, DIM**2), dataset, molecules, False, log_file, s=s_raw ) with open(log_file, "a+") as f: f.write("\n\n+++++ H_Core +++++\n") msg.info("Results H_Core: ", 1) measure_and_display( p_1e.reshape(-1, DIM**2), dataset, molecules, False, log_file, s=s_raw ) with open(log_file, "a+") as f: f.write("\n\n+++++ SAP +++++\n") msg.info("Results SAP: ", 1) measure_and_display( p_sap.reshape(-1, DIM**2), dataset, molecules, False, log_file, s=s_raw ) with open(log_file, "a+") as f: f.write("\n\n+++++ MINAO +++++\n") msg.info("Results MINAO: ", 1) measure_and_display( p_minao.reshape(-1, DIM**2), dataset, molecules, False, log_file, s=s_raw ) with open(log_file, "a+") as f: f.write("\n\n+++++ GWH +++++\n") msg.info("Results GWH: ", 1) measure_and_display( p_gwh.reshape(-1, DIM**2), dataset, molecules, False, log_file, s=s_raw )
def main(sample_index): msg.print_level = 2 msg.info("Hi. Measurements for butadien", 2) #--- fetch dataset and constants --- msg.info("Fetching sample " + str(sample_index) + " from datase", 2) dim = DIM molecules = np.load( "butadien/data/molecules.npy" ) S = np.load( "butadien/data/S.npy") P = np.load( "butadien/data/P.npy") dataset = make_butadien_dataset(molecules, S, P)[0] molecule = molecules[sample_index].get_pyscf_molecule() s = S[sample_index].reshape(dim, dim) s_normed = dataset.inverse_input_transform(s.reshape(1, -1)).reshape(1,-1) p = P[sample_index].reshape(dim, dim) #--- #--- fetch network --- msg.info("Fetching pretained network ", 2) graph = tf.Graph() structure, weights, biases = np.load( "butadien/data/network.npy", encoding="latin1" ) with graph.as_default(): sess = tf.Session() network = EluFixedValue(structure, weights, biases) network.setup() sess.run(tf.global_variables_initializer()) #--- #--- calculate guesses --- msg.info("Calculating guesses ...",2) msg.info("Neural network ", 1) p_nn = network.run(sess, s_normed).reshape(dim, dim) msg.info("McWheenys", 1) p_mcw1 = multi_mc_wheeny(p_nn, s, n_max=1) p_mcw5 = multi_mc_wheeny(p_nn, s, n_max=5) msg.info("Classics", 1) p_sap = hf.init_guess_by_atom(molecule) p_minao = hf.init_guess_by_minao(molecule) p_gwh = hf.init_guess_by_wolfsberg_helmholtz(molecule) #--- #--- Measureing & print --- outfile = "butadien/results/cube_" + str(sample_index) + "_" msg.info("Results Converged ", 1) cubegen.density(molecule, outfile + "converged.cube", p.astype("float64")) msg.info("Results NN: ", 1) cubegen.density(molecule, outfile + "nn.cube", p_nn.astype("float64")) msg.info("Results McWheeny 1: ",1) cubegen.density(molecule, outfile + "mcw1.cube", p_mcw1.astype("float64")) msg.info("Results McWheeny 5: ", 1) cubegen.density(molecule, outfile + "mcw5.cube", p_mcw5.astype("float64")) msg.info("Results SAP: ", 1) cubegen.density(molecule, outfile + "sap.cube", p_sap) msg.info("Results MINAO: ", 1) cubegen.density(molecule, outfile + "minao.cube", p_minao) msg.info("Results GWH: ", 1) cubegen.density(molecule, outfile + "gwh.cube", p_gwh)
def main(molecule_type): msg.print_level = 2 msg.info("Hi. Measurements for " + molecule_type, 2) #--- fetch dataset and constants --- msg.info("Fetching dataset", 2) dataset_triu, molecules = fetch_dataset(molecule_type, True) dataset, _ = fetch_dataset(molecule_type, False) dim = DIM[molecule_type] #--- #--- fetch network --- msg.info("Fetching pretained network ", 2) graph = tf.Graph() structure, weights, biases = np.load( "cc2ai/" + molecule_type + "/network_" + molecule_type + ".npy", encoding="latin1" ) #--- with graph.as_default(): sess = tf.Session() network = EluFixedValue(structure, weights, biases) network.setup() sess.run(tf.global_variables_initializer()) #--- #--- calculate guesses --- msg.info("Calculating guesses ...",2) s_raw = make_matrix_batch(dataset_triu.inverse_input_transform(dataset_triu.testing[0]), dim, True) msg.info("Neural network ", 1) p_nn = network.run(sess, dataset_triu.testing[0]) msg.info("McWheenys", 1) p_batch = make_matrix_batch(p_nn, dim, True) p_mcw1 = np.array(list(map(lambda x: multi_mc_wheeny(x[0], x[1], n_max=1), zip(p_batch, s_raw)))) p_mcw5 = np.array(list(map(lambda x: multi_mc_wheeny(x[0], x[1], n_max=5), zip (p_batch, s_raw)))) msg.info("Classics", 1) p_1e = np.array([ hf.init_guess_by_1e(mol.get_pyscf_molecule()) for mol in molecules[1] ]) p_sap = np.array([ hf.init_guess_by_atom(mol.get_pyscf_molecule()) for mol in molecules[1] ]) p_minao = np.array([ hf.init_guess_by_minao(mol.get_pyscf_molecule()) for mol in molecules[1] ]) p_gwh = np.array([ hf.init_guess_by_wolfsberg_helmholtz(mol.get_pyscf_molecule()) for mol in molecules[1] ]) #--- #--- Measureing & print --- log_file = "cc2ai/" + molecule_type + "/pretrained_" + str(date.today()) + ".log" with open(log_file, "a+") as f: f.write("##### Analysis of " + str(datetime.now()) + " #####\n") msg.info("Results NN: ", 1) with open(log_file, "a+") as f: f.write("\n+++++ Plain NN +++++\n") measure_and_display( p_nn, dataset_triu, molecules, molecule_type, True, log_file ) with open(log_file, "a+") as f: f.write("\n\n+++++ McW 1 +++++\n") msg.info("Results McWheeny 1: ",1) measure_and_display( p_mcw1.reshape(-1, dim**2), dataset, molecules, molecule_type, False, log_file ) with open(log_file, "a+") as f: f.write("\n\n+++++ McW 5 +++++\n") msg.info("Results McWheeny 5: ", 1) measure_and_display( p_mcw5.reshape(-1, dim**2), dataset, molecules, molecule_type, False, log_file ) with open(log_file, "a+") as f: f.write("\n\n+++++ H_Core +++++\n") msg.info("Results H_Core: ", 1) measure_and_display( p_1e.reshape(-1, dim**2), dataset, molecules, molecule_type, False, log_file ) with open(log_file, "a+") as f: f.write("\n\n+++++ SAP +++++\n") msg.info("Results SAP: ", 1) measure_and_display( p_sap.reshape(-1, dim**2), dataset, molecules, molecule_type, False, log_file ) with open(log_file, "a+") as f: f.write("\n\n+++++ MINAO +++++\n") msg.info("Results MINAO: ", 1) measure_and_display( p_minao.reshape(-1, dim**2), dataset, molecules, molecule_type, False, log_file ) with open(log_file, "a+") as f: f.write("\n\n+++++ GWH +++++\n") msg.info("Results GWH: ", 1) measure_and_display( p_gwh.reshape(-1, dim**2), dataset, molecules, molecule_type, False, log_file )
from SCFInitialGuess.nn.training import train_network dim = 26 model_save_path = "butadien/model.npy" source = "butadien/data" msg.info("Welcome", 2) #--- fetching the network --- msg.info("Loading Network", 2) sess = tf.Session() model = np.load(model_save_path) network = EluFixedValue(*model) network.setup() sess.run(tf.global_variables_initializer()) #--- #--- fetching the molecules --- msg.info("Fetching the molecules", 2) def grep_molecule(input_file): import re with open(input_file) as f: molecule = re.search(r"\$molecule.*\$end", f.read(), re.DOTALL) if molecule is None: raise ValueError("No molecule found in " + f.name) else: