deepmd.tf.common#
Collection of functions and classes used throughout the whole package.
Attributes#
Functions#
| Recursively iterate over directories taking those that contain type.raw file. |
| Get numpy precision constant from string. |
| Load yaml or json settings file. |
| Make mesh. |
| Build map of indices for element supplied element types from all atoms list. |
| Gaussian Error Linear Unit. |
| Gaussian Error Linear Unit. |
| Get activation function callable based on string name. |
| Convert str to TF DType constant. |
| Convert a Tensor from a precision to another precision. |
| A decorator that casts and casts back the input |
| Reset all state generated by DeePMD-kit. |
Module Contents#
- deepmd.tf.common.expand_sys_str(root_dir: str | pathlib.Path) list[str][source]#
Recursively iterate over directories taking those that contain type.raw file.
- deepmd.tf.common.get_np_precision(precision: _PRECISION) numpy.dtype[source]#
Get numpy precision constant from string.
- Parameters:
- precision
_PRECISION string name of numpy constant or default
- precision
- Returns:
np.dtypenumpy precision constant
- Raises:
RuntimeErrorif string is invalid
- deepmd.tf.common.j_loader(filename: str | pathlib.Path) dict[str, Any][source]#
Load yaml or json settings file.
- deepmd.tf.common.make_default_mesh(pbc: bool, mixed_type: bool) numpy.ndarray[source]#
Make mesh.
Only the size of mesh matters, not the values: * 6 for PBC, no mixed types * 0 for no PBC, no mixed types * 7 for PBC, mixed types * 1 for no PBC, mixed types
- Parameters:
- Returns:
np.ndarraymesh
- deepmd.tf.common.select_idx_map(atom_types: numpy.ndarray, select_types: numpy.ndarray) numpy.ndarray[source]#
Build map of indices for element supplied element types from all atoms list.
- Parameters:
- atom_types
np.ndarray array specifying type for each atoms as integer
- select_types
np.ndarray types of atoms you want to find indices for
- atom_types
- Returns:
np.ndarrayindices of types of atoms defined by select_types in atom_types array
Warning
select_types array will be sorted before finding indices in atom_types
- deepmd.tf.common.gelu(x: deepmd.tf.env.tf.Tensor) deepmd.tf.env.tf.Tensor[source]#
Gaussian Error Linear Unit.
This is a smoother version of the RELU, implemented by custom operator.
- Parameters:
- x
tf.Tensor float Tensor to perform activation
- x
- Returns:
tf.Tensorx with the GELU activation applied
References
Original paper https://arxiv.org/abs/1606.08415
- deepmd.tf.common.gelu_tf(x: deepmd.tf.env.tf.Tensor) deepmd.tf.env.tf.Tensor[source]#
Gaussian Error Linear Unit.
This is a smoother version of the RELU, implemented by TF.
- Parameters:
- x
tf.Tensor float Tensor to perform activation
- x
- Returns:
tf.Tensorx with the GELU activation applied
References
Original paper https://arxiv.org/abs/1606.08415
- deepmd.tf.common.get_activation_func(activation_fn: deepmd.common._ACTIVATION | None) Callable[[deepmd.tf.env.tf.Tensor], deepmd.tf.env.tf.Tensor][source]#
Get activation function callable based on string name.
- Parameters:
- activation_fn
_ACTIVATION one of the defined activation functions
- activation_fn
- Returns:
- Raises:
RuntimeErrorif unknown activation function is specified
- deepmd.tf.common.get_precision(precision: deepmd.common._PRECISION) Any[source]#
Convert str to TF DType constant.
- Parameters:
- precision
_PRECISION one of the allowed precisions
- precision
- Returns:
tf.python.framework.dtypes.DTypeappropriate TF constant
- Raises:
RuntimeErrorif supplied precision string does not have acorresponding TF constant
- deepmd.tf.common.safe_cast_tensor(input: deepmd.tf.env.tf.Tensor, from_precision: deepmd.tf.env.tf.DType, to_precision: deepmd.tf.env.tf.DType) deepmd.tf.env.tf.Tensor[source]#
Convert a Tensor from a precision to another precision.
If input is not a Tensor or without the specific precision, the method will not cast it.
- deepmd.tf.common.cast_precision(func: Callable) Callable[source]#
A decorator that casts and casts back the input and output tensor of a method.
The decorator should be used in a classmethod.
The decorator will do the following thing: (1) It casts input Tensors from GLOBAL_TF_FLOAT_PRECISION to precision defined by property precision. (2) It casts output Tensors from precision to GLOBAL_TF_FLOAT_PRECISION. (3) It checks inputs and outputs and only casts when input or output is a Tensor and its dtype matches GLOBAL_TF_FLOAT_PRECISION and precision, respectively. If it does not match (e.g. it is an integer), the decorator will do nothing on it.
- Returns:
Callablea decorator that casts and casts back the input and output tensor of a method
Examples
>>> class A: ... @property ... def precision(self): ... return tf.float32 ... ... @cast_precision ... def f(x: tf.Tensor, y: tf.Tensor) -> tf.Tensor: ... return x**2 + y