deepmd.jax.jax2tf.tfmodel#

Attributes#

Classes#

TFModelWrapper

Base neural network module class.

Functions#

decode_list_of_bytes(→ list[str])

Decode a list of bytes to a list of strings.

Module Contents#

deepmd.jax.jax2tf.tfmodel.OUTPUT_DEFS[source]#
deepmd.jax.jax2tf.tfmodel.decode_list_of_bytes(list_of_bytes: list[bytes]) list[str][source]#

Decode a list of bytes to a list of strings.

class deepmd.jax.jax2tf.tfmodel.TFModelWrapper(model: str)[source]#

Bases: tf.Module

Base neural network module class.

A module is a named container for tf.Variable`s, other `tf.Module`s and functions which apply to user input. For example a dense layer in a neural network might be implemented as a `tf.Module:

>>> class Dense(tf.Module):
...   def __init__(self, input_dim, output_size, name=None):
...     super().__init__(name=name)
...     self.w = tf.Variable(
...       tf.random.normal([input_dim, output_size]), name='w')
...     self.b = tf.Variable(tf.zeros([output_size]), name='b')
...   def __call__(self, x):
...     y = tf.matmul(x, self.w) + self.b
...     return tf.nn.relu(y)

You can use the Dense layer as you would expect:

>>> d = Dense(input_dim=3, output_size=2)
>>> d(tf.ones([1, 3]))
<tf.Tensor: shape=(1, 2), dtype=float32, numpy=..., dtype=float32)>

By subclassing tf.Module instead of object any tf.Variable or tf.Module instances assigned to object properties can be collected using the variables, trainable_variables or submodules property:

>>> d.variables
    (<tf.Variable 'b:0' shape=(2,) dtype=float32, numpy=...,
    dtype=float32)>,
    <tf.Variable 'w:0' shape=(3, 2) dtype=float32, numpy=..., dtype=float32)>)

Subclasses of tf.Module can also take advantage of the _flatten method which can be used to implement tracking of any other types.

All tf.Module classes have an associated tf.name_scope which can be used to group operations in TensorBoard and create hierarchies for variable names which can help with debugging. We suggest using the name scope when creating nested submodules/parameters or for forward methods whose graph you might want to inspect in TensorBoard. You can enter the name scope explicitly using with self.name_scope: or you can annotate methods (apart from __init__) with @tf.Module.with_name_scope.

>>> class MLP(tf.Module):
...   def __init__(self, input_size, sizes, name=None):
...     super().__init__(name=name)
...     self.layers = []
...     with self.name_scope:
...       for size in sizes:
...         self.layers.append(Dense(input_dim=input_size, output_size=size))
...         input_size = size
...   @tf.Module.with_name_scope
...   def __call__(self, x):
...     for layer in self.layers:
...       x = layer(x)
...     return x
>>> module = MLP(input_size=5, sizes=[5, 5])
>>> module.variables
(<tf.Variable 'mlp/b:0' shape=(5,) dtype=float32, numpy=..., dtype=float32)>,
<tf.Variable 'mlp/w:0' shape=(5, 5) dtype=float32, numpy=...,
   dtype=float32)>,
<tf.Variable 'mlp/b:0' shape=(5,) dtype=float32, numpy=..., dtype=float32)>,
<tf.Variable 'mlp/w:0' shape=(5, 5) dtype=float32, numpy=...,
   dtype=float32)>)
model[source]#
_call_lower[source]#
_call_lower_atomic_virial[source]#
_call[source]#
_call_atomic_virial[source]#
type_map[source]#
rcut[source]#
dim_fparam[source]#
dim_aparam[source]#
sel_type[source]#
_is_aparam_nall[source]#
_model_output_type[source]#
_mixed_types[source]#
sel[source]#
model_def_script[source]#
__call__(coord: deepmd.jax.env.jnp.ndarray, atype: deepmd.jax.env.jnp.ndarray, box: deepmd.jax.env.jnp.ndarray | None = None, fparam: deepmd.jax.env.jnp.ndarray | None = None, aparam: deepmd.jax.env.jnp.ndarray | None = None, do_atomic_virial: bool = False) Any[source]#

Return model prediction.

Parameters:
coord

The coordinates of the atoms. shape: nf x (nloc x 3)

atype

The type of atoms. shape: nf x nloc

box

The simulation box. shape: nf x 9

fparam

frame parameter. nf x ndf

aparam

atomic parameter. nf x nloc x nda

do_atomic_virial

If calculate the atomic virial.

Returns:
ret_dict

The result dict of type dict[str,jnp.ndarray]. The keys are defined by the ModelOutputDef.

call(coord: deepmd.jax.env.jnp.ndarray, atype: deepmd.jax.env.jnp.ndarray, box: deepmd.jax.env.jnp.ndarray | None = None, fparam: deepmd.jax.env.jnp.ndarray | None = None, aparam: deepmd.jax.env.jnp.ndarray | None = None, do_atomic_virial: bool = False) dict[str, deepmd.jax.env.jnp.ndarray][source]#

Return model prediction.

Parameters:
coord

The coordinates of the atoms. shape: nf x (nloc x 3)

atype

The type of atoms. shape: nf x nloc

box

The simulation box. shape: nf x 9

fparam

frame parameter. nf x ndf

aparam

atomic parameter. nf x nloc x nda

do_atomic_virial

If calculate the atomic virial.

Returns:
ret_dict

The result dict of type dict[str,jnp.ndarray]. The keys are defined by the ModelOutputDef.

model_output_def() deepmd.dpmodel.output_def.ModelOutputDef[source]#
call_lower(extended_coord: deepmd.jax.env.jnp.ndarray, extended_atype: deepmd.jax.env.jnp.ndarray, nlist: deepmd.jax.env.jnp.ndarray, mapping: deepmd.jax.env.jnp.ndarray | None = None, fparam: deepmd.jax.env.jnp.ndarray | None = None, aparam: deepmd.jax.env.jnp.ndarray | None = None, do_atomic_virial: bool = False, charge_spin: deepmd.jax.env.jnp.ndarray | None = None) dict[str, deepmd.jax.env.jnp.ndarray][source]#
get_type_map() list[str][source]#

Get the type map.

get_rcut() float[source]#

Get the cut-off radius.

get_dim_fparam() int[source]#

Get the number (dimension) of frame parameters of this atomic model.

get_dim_aparam() int[source]#

Get the number (dimension) of atomic parameters of this atomic model.

get_sel_type() list[int][source]#

Get the selected atom types of this model.

Only atoms with selected atom types have atomic contribution to the result of the model. If returning an empty list, all atom types are selected.

is_aparam_nall() bool[source]#

Check whether the shape of atomic parameters is (nframes, nall, ndim).

If False, the shape is (nframes, nloc, ndim).

model_output_type() list[str][source]#

Get the output type for the model.

abstractmethod serialize() dict[source]#

Serialize the model.

Returns:
dict

The serialized data

classmethod deserialize(data: dict) TFModelWrapper[source]#
Abstractmethod:

Deserialize the model.

Parameters:
datadict

The serialized data

Returns:
BaseModel

The deserialized model

get_model_def_script() str[source]#

Get the model definition script.

get_min_nbor_dist() float | None[source]#

Get the minimum distance between two atoms.

get_nnei() int[source]#

Returns the total number of selected neighboring atoms in the cut-off radius.

get_sel() list[int][source]#
get_nsel() int[source]#

Returns the total number of selected neighboring atoms in the cut-off radius.

mixed_types() bool[source]#
classmethod update_sel(train_data: deepmd.utils.data_system.DeepmdDataSystem, type_map: list[str] | None, local_jdata: dict) tuple[dict, float | None][source]#
Abstractmethod:

Update the selection and perform neighbor statistics.

Parameters:
train_dataDeepmdDataSystem

data used to do neighbor statictics

type_maplist[str], optional

The name of each type of atoms

local_jdatadict

The local data refer to the current class

Returns:
dict

The updated local data

float

The minimum distance between two atoms

classmethod get_model(model_params: dict) TFModelWrapper[source]#
Abstractmethod:

Get the model by the parameters.

By default, all the parameters are directly passed to the constructor. If not, override this method.

Parameters:
model_paramsdict

The model parameters

Returns:
BaseBaseModel

The model

has_default_fparam() bool[source]#

Check whether the model has default frame parameters.

get_default_fparam() list[float] | None[source]#

Get the default frame parameters.