# Install konda
!pip -q install konda
import konda
konda.install()
# Accept Anaconda Terms of Service
!conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/main
!conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/r
# Create the environment
!konda create -q -n pinn_env python=3.10 -y
!konda activate pinn_env
!konda run "pip -q install 'tensorflow[and-cuda]==2.15.1' 'ase>=3.25' 'PyYAML~=6.0.1' 'numpy<2'"
!konda run "pip -q install git+https://github.com/Teoroo-CMC/PiNN"
!wget -nv -nc https://raw.githubusercontent.com/Teoroo-CMC/PiNN_lab/master/resources/qm9_train.{yml,tfr}
Loading data
For the purpose of testing we download a subset of the QM9 dataset used in PiNN_lab.
%%writefile layer_debug.py
import warnings
from pinn.io import load_tfrecord, sparse_batch
index_warning = 'Converting sparse IndexedSlices'
warnings.filterwarnings('ignore', index_warning)
dataset = load_tfrecord("qm9_train.yml").apply(sparse_batch(10))
for datum in dataset:
print({k: v.shape for k, v in datum.items()})
break
!konda run "python layer_debug.py"
Using PiNN Layers
PiNN networks and layers are Keras Layers and Models.
To use them, you create an instance of layer, after that, the layer object can be used as a function. Each layer is initialized with different parameters and requires different input tensors, see their individual documentation for the details.
%%writefile -a layer_debug.py
from pinn.layers import CellListNL
nl = CellListNL(rc=5)
for datum in dataset:
nl(datum)
break
!konda run "python layer_debug.py"
The definition of a layer needs three parts:
__init__defines the layer object;buildcreates the necessary variables or sub-layers;calldefines how the input tensors are processed.
The build() method is only called once when the layer is used for the first tiem (e.g. in a loop).
See below for an example definition for the PILayer
%%writefile check_PILayer.py
from pinn.networks.pinet import PILayer
import inspect
print(inspect.getsource(PILayer))
!konda run "python check_PILayer.py"
Using PiNN Networks
network (Keras Models) are defined similarly, but they can be directly used to perform regression task.
By default, network produces per-atom predictions, this can be changed by the out_pool parameter to
get some simple per-structure predictions. In that case, the network object can be used to perform trainig directly.
%%writefile -a layer_debug.py
from pinn.networks.pinet import PiNet
def label_data(data):
# defines the label to train on
x = data
y = data['lumo']
return x, y
train = dataset.map(label_data)
pinet = PiNet(out_pool='min')
pinet.compile(optimizer='Adam', loss='MAE')
pinet.fit(train, epochs=3)
!konda run "python layer_debug.py"
Further benchmarks
For more advanced usage you are recommended to use the Model API to define the trainig loss, derived predicates.
For traininig potential energy surfaces, you are recommended to use pinn.models.potential_model in combination with the command line interface (CLI).
Alternatively, see the Trainig Tips notebook to see how to run the tranining interactively in a notebook.