deepmd.tf.common
Collection of functions and classes used throughout the whole package.
Module Contents
Functions
| Specify data requirements for training. |
| Recursively iterate over directories taking those that contain type.raw file. |
| Get numpy precision constant from string. |
| Load yaml or json settings file. |
| Assert that supplied dictionary conaines specified key. |
| 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. |
Attributes
- deepmd.tf.common.add_data_requirement(key: str, ndof: int, atomic: bool = False, must: bool = False, high_prec: bool = False, type_sel: bool | None = None, repeat: int = 1, default: float = 0.0, dtype: numpy.dtype | None = None, output_natoms_for_type_sel: bool = False)[source]
Specify data requirements for training.
- Parameters:
- key
str
type of data stored in corresponding *.npy file e.g. forces or energy
- ndof
int
number of the degrees of freedom, this is tied to atomic parameter e.g. forces have atomic=True and ndof=3
- atomicbool,
optional
specifies whwther the ndof keyworrd applies to per atom quantity or not, by default False
- mustbool,
optional
specifi if the *.npy data file must exist, by default False
- high_precbool,
optional
if true load data to np.float64 else np.float32, by default False
- type_selbool,
optional
select only certain type of atoms, by default None
- repeat
int
,optional
if specify repaeat data repeat times, by default 1
- default
float
,optional
, default=0. default value of data
- dtype
np.dtype
,optional
the dtype of data, overwrites high_prec if provided
- output_natoms_for_type_selbool,
optional
if True and type_sel is True, the atomic dimension will be natoms instead of nsel
- key
- 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.dtype
numpy presicion constant
- Raises:
RuntimeError
if 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.j_must_have(jdata: Dict[str, _DICT_VAL], key: str, deprecated_key: List[str] = []) _DICT_VAL [source]
Assert that supplied dictionary conaines specified key.
- Returns:
_DICT_VAL
value that was store unde supplied key
- Raises:
RuntimeError
if the key is not present
- 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.ndarray
mesh
- 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 specifing type for each atoms as integer
- select_types
np.ndarray
types of atoms you want to find indices for
- atom_types
- Returns:
np.ndarray
indices 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.Tensor
x 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.Tensor
x 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:
RuntimeError
if 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.DType
appropriate TF constant
- Raises:
RuntimeError
if 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:
Callable
a 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