xnn.gnn.models.dimenet.DimeNet#
- class xnn.gnn.models.dimenet.DimeNet(n_features=128, n_interactions=6, n_rbf=6, n_spherical=7, cutoff=5.0, n_bilinear=8, n_output_features=None, p=6, n_before_skip=1, n_after_skip=2, n_output_layers=3, activation='silu', output_init='glorot_orthogonal', trainable_rbf=True, radial_gate='kj', reference_basis=False, species=None, atomic_energies=None)[source]#
Bases:
InteratomicPotentialDimeNet: directional message passing with the bilinear interaction (ICLR 2020).
See the module docstring for the equation-by-equation walk-through. The defaults are the paper’s QM9/MD17 architecture (Appendix B):
F = 128, six interaction blocks,N_SHBF = 7,N_SRBF = N_RBF = 6,N_bilinear = 8, cutoff 5 Angstrom, envelope exponentp = 6, one residual block before and two after the skip connection, three dense layers in the output blocks. Works for molecules and periodic systems (periodicity enters through the edge vectors).- Parameters:
n_features (int, optional) – Embedding and message width
F, by default 128.n_interactions (int, optional) – Number of interaction blocks
T, by default 6.n_rbf (int, optional) – Number of radial Bessel functions
N_RBF(also the zeros per degreeN_SRBFof the 2D basis, as in the paper), by default 6.n_spherical (int, optional) – Number of spherical-harmonic degrees
N_SHBFof the 2D basis, by default 7.cutoff (float, optional) – Cutoff
cin Angstrom, by default 5.0.n_bilinear (int, optional) – Inner dimension of the bilinear layer, by default 8.
n_output_features (int or None, optional) – Width of the atom features in the output blocks;
None(default, the paper) keepsFwithout an up-projection. DimeNet++ sets it.p (int, optional) – Exponent of the envelope
u(d)of eq 8, by default 6 (the reference code’senvelope_exponent + 1).n_before_skip (int, optional) – Residual blocks before and after the skip connection of each interaction block, by default 1 and 2.
n_after_skip (int, optional) – Residual blocks before and after the skip connection of each interaction block, by default 1 and 2.
n_output_layers (int, optional) – Dense layers of each output block, by default 3.
activation (str or torch.nn.Module, optional) – Activation, by default
"silu"(the paper’s swish); seemake_activation().output_init (str, optional) – Initialization of the final layer of the output blocks:
"glorot_orthogonal"(default, the reference choice for energies) or"zeros"(the fresh model then predicts exactlyatom_ref).trainable_rbf (bool, optional) – Fine-tune the Bessel frequencies
n piby backpropagation (section 5), by defaultTrue.radial_gate (str, optional) – Which distance gates the incoming message
m_kjin the interaction block:"kj"(default) is the reference implementation, where the gate isW e_RBF(d_kj);"ji"is eq 4 and Fig. 4 as printed,W e_RBF(d_ji). The published models were trained with"kj".reference_basis (bool, optional) – Reproduce the 2D basis of the reference implementation, whose envelope carries an extra factor
c / d_kjrelative to eq 6 (its radial and 2D bases share one envelope that includes the1 / dof eq 7).False(default) is the paper’su(d) a_SBF.species (list of int or None, optional) – Only used to interpret
atomic_energies; the model handles all elements up to Z = 99.atomic_energies (array-like or None, optional) – Per-species reference energies loaded into
atom_ref(aligned withspecies).
- Variables:
cutoff (float) – Neighbor-list cutoff
c.rbf (BesselRBF) – Radial basis of eq 7 (trainable frequencies).
sbf (SphericalBesselBasis) – 2D spherical Fourier-Bessel basis of eq 6.
envelope (PolynomialCutoff) – The envelope
u(d)of eq 8.embedding (_Embedding) – The embedding block (holds the atom-type embeddings).
interactions (torch.nn.ModuleList) – The
Tinteraction blocks.outputs (torch.nn.ModuleList) – The
T + 1output blocks (one after the embedding block).atom_ref (torch.nn.Embedding) – Learnable per-element energy reference, initialized to zero.
node_feature_dim (int) – Width of the invariant per-atom features returned as
"node_features"(the summed output-block features before their final layer; what e.g. the LES charges are read from).
- set_atomic_energies(species, values)[source]#
Initialize the per-element reference energies
atom_ref.- Parameters:
- Raises:
ValueError – If the number of values does not match the number of species.
- Return type:
None
- node_features_energy(atomic_numbers, edge_index, edge_vec)[source]#
TorchScript-compatible core: tensors in, features + energy out.
The single implementation reused by
node_energy()(the deploy entry point) andforward().- Parameters:
atomic_numbers (Tensor) – Per-atom atomic numbers, shape
(N,).edge_index (Tensor) – Edge index of shape
(2, E); row 0 is the sourcejand row 1 the destinationiof each edge (the messagem_ji).edge_vec (Tensor) – Edge vectors
x_i - x_j, shape(E, 3)(cell shifts included).
- Returns:
The invariant per-atom features
(N, node_feature_dim)(the output-block features before their final layer, summed over the blocks) and the per-atom energy(N,),sum_l t_i^(l) + atom_ref[Z_i].- Return type:
tuple of Tensor
- node_energy(atomic_numbers, edge_index, edge_vec)[source]#
Per-atom energy, shape
(N,)(thin wrapper overnode_features_energy(); the deploy wrappers call this).
- forward(data)[source]#
Compute per-atom and total energies for a batch of structures.
- Parameters:
data (AtomicGraph) – Batched atomic graph providing atomic numbers, edge index and edge vectors.
- Returns:
"node_energy"(per-atom energy, shape(N,)),"energy"(per-structure total energy, the sum over atoms) and"node_features"(invariant per-atom features, shape(N, node_feature_dim)).- Return type:
- classmethod from_config(cfg)[source]#
Build a
DimeNetfrom a configuration object.Core fields:
cfg.n_features->F,cfg.n_interactions->T,cfg.n_rbf->N_RBF = N_SRBF,cfg.cutoff->c.cfg.extramay holdn_spherical,n_bilinear,n_output_features,p(or the reference code’senvelope_exponent = p - 1),n_before_skip,n_after_skip,n_output_layers,activation,output_init,trainable_rbf,reference_basis,speciesandatomic_energies. The reference code’s key spellings (emb_size,num_blocks, …) are translated byxnn.common.config.translate.- Parameters:
cfg (xnn.common.config.schema.ModelConfig) – The core model config.
- Returns:
Instantiated model.
- Return type: