<jupyter_start><jupyter_text>Quick tour with QM9This notebook showcases a simple example of training a neural network potential on the QM9 dataset with PiNN.<jupyter_code>import os, warnings
import tensorflow as tf
from glob import glob
from ase.collections import g2
from pinn.io import load_qm9, sparse_batch
from pinn.models import potential_model
from pinn.calculator import PiNN_calc
# CPU is used for documentation generation, feel free to use your GPU!
os.environ['CUDA_VISIBLE_DEVICES'] = '' 
# We heavily use indexed slices to do sparse summations,
# which causes tensorflow to complain, 
# we believe it's safe to ignore this warning.
index_warning = 'Converting sparse IndexedSlices'
warnings.filterwarnings('ignore', index_warning)<jupyter_output><empty_output><jupyter_text>Getting the datasetPiNN adapts TensorFlow's dataset API to handle different datasets.For this and the following notebooks the QM9 dataset (https://doi.org/10.6084/m9.figshare.978904) is used.  To follow the notebooks, download the dataset and change the directory accordingly.The dataset will be automatically split into subsets according to the split_ratio.  Note that to use the dataset with the estimator, the datasets should be a function, instead of a dataset object.<jupyter_code>filelist = glob('/home/yunqi/datasets/QM9/dsgdb9nsd/*.xyz')
dataset = lambda: load_qm9(filelist, split={'train':8, 'test':2})
train = lambda: dataset()['train'].repeat().shuffle(1000).apply(sparse_batch(100))
test = lambda: dataset()['test'].repeat().apply(sparse_batch(100))<jupyter_output><empty_output><jupyter_text>Defining the modelIn PiNN, models are defined at two levels: models and networks. - A model (model_fn) defines the target, loss and training details of the network.- A network defines the structure of the neural network.In this example, we will use the potential model, and the PiNet network.The configuration of a model is stored in a nested dictionary like: ```Python{'model_dir': 'PiNet_QM9', 'network': 'pinet', 'network_params': {     'depth': 4,     'rc':4.0,     'atom_types':[1,6,7,8,9]}, 'model_params':{     'learning_rate': 3e-4,     ...}}```Available options of the network and model can be found in the documentation.The easiest way to specify the network function is with its name.  You can also provide a custom function here.<jupyter_code>params = {'model_dir': '/tmp/PiNet_QM9',
          'network': 'pinet',
          'network_params': {},
          'model_params': {'learning_rate': 1e-3}}
model = potential_model(params)<jupyter_output>INFO:tensorflow:Using default config.
INFO:tensorflow:Using config: {'_model_dir': '/tmp/PiNet_QM9', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': allow_soft_placement: true
graph_options {
  rewrite_options {
    meta_optimizer_iterations: ONE
  }
}
, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_train_distribute': None, '_device_fn': None, '_protocol': None, '_eval_distribute': None, '_experimental_distribute': None, '_service': None, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x7f8114523710>, '_task_type': 'worker', '_task_id': 0, '_global_id_in_cluster': 0, '_master': '', '_evaluation_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}<jupyter_text>Configuring the training processThe defined model is indeed a tf.Estimator object, thus, the training can be easily controlled<jupyter_code>train_spec = tf.estimator.TrainSpec(input_fn=train, max_steps=1000)
eval_spec = tf.estimator.EvalSpec(input_fn=test, steps=100)<jupyter_output><empty_output><jupyter_text>Train and evaluate<jupyter_code>tf.estimator.train_and_evaluate(model, train_spec, eval_spec)<jupyter_output>INFO:tensorflow:Not using Distribute Coordinator.
INFO:tensorflow:Running training and evaluation locally (non-distributed).
INFO:tensorflow:Start train and evaluate loop. The evaluate will happen after every checkpoint. Checkpoint frequency is determined based on RunConfig arguments: save_checkpoints_steps None or save_checkpoints_secs 600.
WARNING:tensorflow:From /home/yunqi/miniconda3/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
WARNING:tensorflow:From /home/yunqi/miniconda3/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py:429: py_func (from tensorflow.python.ops.script_ops) is deprecated and will be removed in a future version.
Instructions for updating:
tf.py_func is deprecated in TF V2. Instead, use
    tf.py_function, which takes a python function [...]<jupyter_text>Using the modelThe trained model can be used as an ASE calculator.<jupyter_code>calc = PiNN_calc(model)
atoms = g2['C2H4']
atoms.set_calculator(calc)
atoms.get_forces(), atoms.get_potential_energy()<jupyter_output>INFO:tensorflow:Calling model_fn.
INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:Graph was finalized.
INFO:tensorflow:Restoring parameters from /tmp/PiNet_QM9/model.ckpt-1000
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.
{'fraction_non_alphanumeric': 0.10044977511244378, 'fraction_numerical': 0.020614692653673165, 'mean_word_length': 6.591749644381223, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '332158', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1718, 'n_tokens_neox': 1621, 'n_words': 483, 'path': 'doc/notebooks/Quick_tour.ipynb', 'repo_name': 'FZJ-IAS5-MLMD/PiNN', 'size': '5336'}