pyNNsMD.layers package

Submodules

pyNNsMD.layers.features module

class pyNNsMD.layers.features.Angles(*args, **kwargs)[source]

Bases: keras.engine.base_layer.Layer

Compute angles from coordinates.

The index-list of atoms to compute angles from is added as a static non-trainable weight.

__init__(angle_shape, **kwargs)[source]

Initialize layer. The index list is initialized to zero.

Parameters

angle_shape (list) – Shape of the angle index-list without batch dimension (N, 3).

build(input_shape)[source]

Build model. Angle list is built in init.

Parameters

input_shape (list) – Input shape.

call(inputs, **kwargs)[source]

Forward pass.

Parameters

inputs (tf.tensor) – Coordinate input as (batch, N, 3).

Returns

Flatten list of angles from index.

Return type

angs_rad (tf.tensor)

get_config()[source]

Return config for layer.

Returns

Config from base class plus angle index shape.

Return type

config (dict)

class pyNNsMD.layers.features.Dihedral(*args, **kwargs)[source]

Bases: keras.engine.base_layer.Layer

Compute dihedral angles from coordinates.

The index-list of atoms to compute angles from is added as a static non-trainable weight.

__init__(dihed_shape, **kwargs)[source]

Initialize layer. The index list is initialized to zero.

Parameters

dihed_shape (list) – Shape of the angle index-list without batch dimension of (N, 4).

build(input_shape)[source]

Build model. Angle list is built in init.

Parameters

input_shape (list) – Input shape.

call(inputs, **kwargs)[source]

Forward pass.

Parameters

inputs (tf.tensor) – Coordinates of shape (batch, N, 3).

Returns

Dihedral angles from index-list and coordinates of shape (batch, M).

Return type

angs_rad (tf.tensor)

get_config()[source]

Return config for layer.

Returns

Config from base class plus angle index shape.

Return type

config (dict)

class pyNNsMD.layers.features.FeatureGeometric(*args, **kwargs)[source]

Bases: keras.engine.base_layer.Layer

Feature representation consisting of inverse distances, angles and dihedral angles.

Uses InverseDistanceIndexed, Angles, Dihedral layer definition if input index is not empty.

__init__(invd_shape=None, angle_shape=None, dihed_shape=None, **kwargs)[source]

Init of the layer.

Parameters
  • invd_shape (list, optional) – Index-Shape of atoms to calculate inverse distances. Defaults to None.

  • angle_shape (list, optional) – Index-Shape of atoms to calculate angles. Defaults to None.

  • dihed_shape (list, optional) – Index-Shape of atoms to calculate dihedral angles. Defaults to None.

  • **kwargs

build(input_shape)[source]

Build model. Passes to base class.

Parameters

input_shape (list) – Input shape.

call(inputs, **kwargs)[source]

Forward pass of the layer. Call().

Parameters

inputs (tf.tensor) – Coordinates of shape (batch,N,3).

Returns

Feature description of shape (batch,M).

Return type

out (tf.tensor)

get_config()[source]

Return config for layer.

Returns

Config from base class plus index info.

Return type

config (dict)

get_feature_type_segmentation()[source]

Get the feature output segmentation length of [invd, angle, dihed]

Returns

Segmentation length

Return type

feat_segments (list)

set_mol_index(invd_index, angle_index, dihed_index)[source]

Set weights for atomic index for distance and angles.

Parameters
  • invd_index (np.array) – Indices for inverse distances. Shape (N, 2).

  • angle_index (np.array) – Indices for angles. Shape (N, 3).

  • dihed_index (np.array) – Indices for dihed angles. Shape (N, 4).

class pyNNsMD.layers.features.InverseDistance(*args, **kwargs)[source]

Bases: keras.engine.base_layer.Layer

build(input_shape)[source]

Creates the variables of the layer (optional, for subclass implementers).

This is a method that implementers of subclasses of Layer or Model can override if they need a state-creation step in-between layer instantiation and layer call. It is invoked automatically before the first execution of call().

This is typically used to create the weights of Layer subclasses (at the discretion of the subclass implementer).

Parameters

input_shape – Instance of TensorShape, or list of instances of TensorShape if the layer expects a list of inputs (one instance per input).

call(inputs, **kwargs)[source]

This is where the layer’s logic lives.

The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state in __init__(), or the build() method that is called automatically before call() executes the first time.

Parameters
  • inputs

    Input tensor, or dict/list/tuple of input tensors. The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero

    arguments, and inputs cannot be provided via the default value of a keyword argument.

    • NumPy array or Python scalar values in inputs get cast as tensors.

    • Keras mask metadata is only collected from inputs.

    • Layers are built (build(input_shape) method) using shape info from inputs only.

    • input_spec compatibility is only checked against inputs.

    • Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.

    • The SavedModel input specification is generated using inputs only.

    • Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.

  • *args – Additional positional arguments. May contain tensors, although this is not recommended, for the reasons above.

  • **kwargs

    Additional keyword arguments. May contain tensors, although this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating

    whether the call is meant for training or inference.

    • mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).

Returns

A tensor or list/tuple of tensors.

class pyNNsMD.layers.features.InverseDistanceIndexed(*args, **kwargs)[source]

Bases: keras.engine.base_layer.Layer

Compute inverse distances from coordinates.

The index-list of atoms to compute distances from is added as a static non-trainable weight.

__init__(invd_shape, **kwargs)[source]

Initialize layer. The index list is initialized to zero.

Parameters

invd_shape (list) – Shape of the index-pair list without batch dimension (N, 2).

build(input_shape)[source]

Build model. Index list is built in init.

Parameters

input_shape (list) – Input shape.

call(inputs, **kwargs)[source]

Forward pass.

Parameters

inputs (tf.tensor) – Coordinate input as (batch, N, 3).

Returns

Flatten list of inverse distances from index.

Return type

invd (tf.tensor)

get_config()[source]

Return config for layer.

Returns

Config from base class plus angle invd shape.

Return type

config (dict)

pyNNsMD.layers.gradients module

class pyNNsMD.layers.gradients.EmptyGradient(*args, **kwargs)[source]

Bases: keras.engine.base_layer.Layer

Layer to generate empty gradient output.

__init__(mult_states=1, atoms=1, **kwargs)[source]

Initialize empty gradient layer.

Parameters
  • mult_states (int) – Number of output states.

  • atoms (int) – Number of atoms.

  • **kwargs

build(input_shape)[source]

Build layer.

call(inputs, **kwargs)[source]

Generate any empty gradient placeholder.

Parameters

inputs (tf.tensor) – Energy tensor.

Returns:

get_config()[source]

Update config for layer.

class pyNNsMD.layers.gradients.PropagateEnergyGradient(*args, **kwargs)[source]

Bases: keras.engine.base_layer.Layer

Layer to propagate the gradients with precomputed layers.

__init__(mult_states=1, **kwargs)[source]

Initialize layer.

Parameters
  • mult_states (int) – Number of states

  • **kwargs

build(input_shape)[source]

Build layer.

call(inputs, **kwargs)[source]

Propagate gradients

Parameters
  • inputs – [grads, grads2]

  • grads (-) – Gradient for NN of shape (batch, states, features)

  • grads2 (-) – Gradients of static features. (batch, features, atoms, 3)

  • **kwargs

Returns

Gradients with respect to coordinates.

Return type

out (tf.tensor)

get_config()[source]

Update config for layer.

class pyNNsMD.layers.gradients.PropagateNACGradient(*args, **kwargs)[source]

Bases: keras.engine.base_layer.Layer

Propagate partial gradients for virtual NAC potentials.

__init__(mult_states=1, atoms=1, **kwargs)[source]

Initialize layer.

Parameters
  • mult_states (int) – number of states

  • atoms (int) – number of atoms

  • **kwargs

build(input_shape)[source]

Build layer.

call(inputs, **kwargs)[source]

Propagate gradients for virtual NACs.

Parameters
  • inputs – [grads, grads2]

  • grads (-) – Gradient for NN of shape (batch, states, atoms, features)

  • grads2 (-) – Gradients of static features. (batch, features, atoms, 3)

  • **kwargs

Returns

Gradients with respect to coordinates.

Return type

out (tf.tensor)

get_config()[source]

Update config for layer.

class pyNNsMD.layers.gradients.PropagateNACGradient2(*args, **kwargs)[source]

Bases: keras.engine.base_layer.Layer

Layer to propagate direct gradient predictions for NACs.

__init__(axis=(2, 1), **kwargs)[source]

Initialize layer

Parameters
  • axis (tuple) – Which axis the batch-dot is done. Default is (2,1)

  • **kwargs

build(input_shape)[source]

Build layer.

call(inputs, **kwargs)[source]

Simple batch-dot for partial gradients.

Parameters
  • inputs – [grads, grads2]

  • grads (-) – Gradient for NN.

  • grads2 (-) – Gradients of static features.

  • **kwargs

Returns:

get_config()[source]

Update config for layer.

pyNNsMD.layers.mlp module

class pyNNsMD.layers.mlp.MLP(*args, **kwargs)[source]

Bases: keras.engine.base_layer.Layer

Multilayer perceptron that consist of N dense keras layers.

Last layer can be modified sperately. Hidden layers are all the same.

__init__(dense_units, dense_depth=1, dense_bias=True, dense_bias_last=True, dense_activ=None, dense_activ_last=None, dense_activity_regularizer=None, dense_kernel_regularizer=None, dense_bias_regularizer=None, dropout_use=False, dropout_dropout=0, **kwargs)[source]

Init MLP as for dense.

Parameters
  • dense_units (int) – Size of hidden layers.

  • dense_depth (int, optional) – Number of hidden layers. Defaults to 1.

  • dense_bias (bool, optional) – Use bias for hidden layers. Defaults to True.

  • dense_bias_last (bool, optional) – Bias for last layer. Defaults to True.

  • dense_activ (str, optional) – Activity identifier. Defaults to None.

  • dense_activ_last (str, optional) – Activity identifier for last layer. Defaults to None.

  • dense_activity_regularizer (str, optional) – Activity regularizer identifier. Defaults to None.

  • dense_kernel_regularizer (str, optional) – Kernel regularizer identifier. Defaults to None.

  • dense_bias_regularizer (str, optional) – Bias regularizer identifier. Defaults to None.

  • dropout_use (bool, optional) – Use dropout. Defaults to False.

  • dropout_dropout (float, optional) – Fraction of dropout. Defaults to 0.

  • **kwargs

build(input_shape)[source]

Build layer.

Parameters

input_shape (list) – Input shape.

call(inputs, training=False)[source]

Forward pass.

Parameters
  • inputs (tf.tensor) – Input tensor of shape (…,N).

  • training (bool, optional) – Training mode. Defaults to False.

Returns

Last activity.

Return type

out (tf.tensor)

get_config()[source]

Update config.

Returns

Base class config plus MLP info.

Return type

config (dict)

pyNNsMD.layers.normalize module

class pyNNsMD.layers.normalize.ConstLayerNormalization(*args, **kwargs)[source]

Bases: keras.engine.base_layer.Layer

Layer normalization with constant scaler of input.

Note that this should be replaced with keras normalization layer where trainable could be altered.

__init__(axis=- 1, **kwargs)[source]

Init the layer.

Parameters
  • axis (int,list, optional) – Which axis match the input on build. Defaults to -1.

  • **kwargs

build(input_shape)[source]

Build the layer.

Parameters

input_shape (list) – Shape of Input.

Raises

TypeError – Axis argument is not valud.

call(inputs, **kwargs)[source]

Forward pass of the layer. Call().

Parameters
  • **kwargs

  • inputs (tf.tensor) – Tensor to scale.

Returns

(inputs-mean)/std

Return type

out (tf.tensor)

compute_const_normalization(feat_x)[source]

Calculate and set the constant normalization of this layer.

Parameters

feat (np.array) – features of shape (batch,N)

Returns:

get_config()[source]

Config for the layer.

Returns

super.config with updated axis parameter.

Return type

config (dict)

class pyNNsMD.layers.normalize.DummyLayer(*args, **kwargs)[source]

Bases: keras.engine.base_layer.Layer

Empty layer that does nothing.

build(input_shape)[source]

Creates the variables of the layer (optional, for subclass implementers).

This is a method that implementers of subclasses of Layer or Model can override if they need a state-creation step in-between layer instantiation and layer call. It is invoked automatically before the first execution of call().

This is typically used to create the weights of Layer subclasses (at the discretion of the subclass implementer).

Parameters

input_shape – Instance of TensorShape, or list of instances of TensorShape if the layer expects a list of inputs (one instance per input).

call(inputs, **kwargs)[source]

This is where the layer’s logic lives.

The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state in __init__(), or the build() method that is called automatically before call() executes the first time.

Parameters
  • inputs

    Input tensor, or dict/list/tuple of input tensors. The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero

    arguments, and inputs cannot be provided via the default value of a keyword argument.

    • NumPy array or Python scalar values in inputs get cast as tensors.

    • Keras mask metadata is only collected from inputs.

    • Layers are built (build(input_shape) method) using shape info from inputs only.

    • input_spec compatibility is only checked against inputs.

    • Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.

    • The SavedModel input specification is generated using inputs only.

    • Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.

  • *args – Additional positional arguments. May contain tensors, although this is not recommended, for the reasons above.

  • **kwargs

    Additional keyword arguments. May contain tensors, although this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating

    whether the call is meant for training or inference.

    • mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).

Returns

A tensor or list/tuple of tensors.

get_config()[source]

Returns the config of the layer.

A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.

The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).

Note that get_config() does not guarantee to return a fresh copy of dict every time it is called. The callers should make a copy of the returned dict if they want to modify it.

Returns

Python dictionary.

Module contents