Skip to content

Commit

Permalink
update for keras 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
PatReis committed Nov 15, 2023
1 parent d884686 commit 0a27b26
Show file tree
Hide file tree
Showing 12 changed files with 396 additions and 308 deletions.
12 changes: 11 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
v4.0.0

* Reworked training scripts to have a single ``train_graph.py`` script. Command line arguments are now optional and just used for verification, but `category` to select a model/hyperparameter combination from hyper file.
Completely reworked version of kgcnn for Keras 3.0 and multi-backend support. A lot of fundamental changes have been made.
However, we tried to keep as much of the API from kgcnn 3.0 so that models in literature can be used with minimal changes.
Mainly, the ``"input_tensor_type"="ragged"`` model parameter has to be added if ragged tensors are used as input in tensorflow.
The scope of models has been reduced for initial release but will be extended in upcoming versions.

* Reworked training scripts to have a single ``train_graph.py`` script. Command line arguments are now optional and just used for verification, all but `category` which has to select a model/hyperparameter combination from hyper file.
Since the hyperparameter file already contains all necessary information.
* Train test indices can now also be set and loaded from the dataset directly.
* Scaler behaviour has changed with regard to `transform_dataset`. Key names of properties to transform has been moved to the constructor!
Also be sure to check ``StandardLabelScaler`` if you want to scale regression targets, since target properties are default here.
* Literature Models have an optional output scaler from new `kgcnn.layers.scale` layer controlled by `output_scaling` model argument.
* Input embedding in literature models is now controlled with separate ``input_node_embedding`` or ``input_edge_embedding`` arguments which can be set to `None` for no embedding.
Also embedding input tokens must be of dtype int now. No autocasting from float anymore.
*



Expand Down
26 changes: 14 additions & 12 deletions kgcnn/literature/AttentiveFP/_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@
],
"input_tensor_type": "padded",
"cast_disjoint_kwargs": {},
"input_embedding": None,
"input_embedding": None, # deprecated
"input_node_embedding": {"input_dim": 95, "output_dim": 64},
"input_edge_embedding": {"input_dim": 5, "output_dim": 64},
"attention_args": {"units": 32},
"depthmol": 2,
"depthato": 2,
"dropout": 0.1,
"verbose": 10,
"output_embedding": "graph", "output_to_tensor": True,
"output_embedding": "graph",
"output_to_tensor": True, # deprecated
"output_tensor_type": "padded",
"output_mlp": {"use_bias": [True, True, False], "units": [25, 10, 1],
"activation": ["relu", "relu", "sigmoid"]}
Expand All @@ -57,36 +58,37 @@ def make_model(inputs: list = None,
input_tensor_type: str = None,
input_node_embedding: dict = None,
input_edge_embedding: dict = None,
input_embedding: dict = None,
input_embedding: dict = None, # noqa
depthmol: int = None,
depthato: int = None,
dropout: float = None,
attention_args: dict = None,
name: str = None,
verbose: int = None,
verbose: int = None, # noqa
output_embedding: str = None,
output_to_tensor: bool = None,
output_to_tensor: bool = None, # noqa
output_tensor_type: str = None,
output_scaling: dict = None,
output_mlp: dict = None
):
r"""Make `AttentiveFP <https://doi.org/10.1021/acs.jmedchem.9b00959>`_ graph network via functional API.
r"""Make `AttentiveFP <https://doi.org/10.1021/acs.jmedchem.9b00959>`__ graph network via functional API.
Default parameters can be found in :obj:`kgcnn.literature.AttentiveFP.model_default`.
Model inputs:
Model uses the list template of inputs and standard output template.
The supported inputs are :obj:`[nodes, edges, edge_indices, ...]`
with '...' indicating mask or id tensors following the template below:
%s
Model outputs:
The standard output template:
%s
%s
Args:
inputs (list): List of dictionaries unpacked in :obj:`tf.keras.layers.Input`. Order must match model definition.
cast_disjoint_kwargs (dict): Dictionary of arguments for :obj:`CastBatchedIndicesToDisjoint` .
inputs (list): List of dictionaries unpacked in :obj:`Input`. Order must match model definition.
cast_disjoint_kwargs (dict): Dictionary of arguments for casting layers if used.
input_tensor_type (str): Input type of graph tensor. Default is "padded".
input_embedding (dict): Deprecated in favour of `input_node_embedding` etc.
input_node_embedding (dict): Dictionary of arguments for nodes unpacked in :obj:`Embedding` layers.
Expand Down Expand Up @@ -119,8 +121,8 @@ def make_model(inputs: list = None,
# Wrapping disjoint model.
out = model_disjoint(
[n, ed, disjoint_indices, batch_id_node, count_nodes],
use_node_embedding=len(inputs[0]['shape']) < 2,
use_edge_embedding=len(inputs[1]['shape']) < 2,
use_node_embedding=("int" in inputs[0]['dtype']) if input_node_embedding is not None else False,
use_edge_embedding=("int" in inputs[1]['dtype']) if input_edge_embedding is not None else False,
input_node_embedding=input_node_embedding,
input_edge_embedding=input_edge_embedding,
depthmol=depthmol,
Expand Down Expand Up @@ -154,4 +156,4 @@ def set_scale(*args, **kwargs):
return model


make_model.__doc__ = make_model.__doc__ % (template_cast_list_input.__doc__, template_cast_output.__doc__)
make_model.__doc__ = make_model.__doc__ % (template_cast_list_input.__doc__, template_cast_output.__doc__)
61 changes: 31 additions & 30 deletions kgcnn/literature/DMPNN/_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
model_default = {
"name": "DMPNN",
"inputs": [
{"shape": (None,), "name": "node_attributes", "dtype": "float32"},
{"shape": (None,), "name": "edge_attributes", "dtype": "float32"},
{"shape": (None,), "name": "node_number", "dtype": "int64"},
{"shape": (None,), "name": "edge_number", "dtype": "int64"},
{"shape": (None, 2), "name": "edge_indices", "dtype": "int64"},
{"shape": (None, 1), "name": "edge_indices_reverse", "dtype": "int64"},
{"shape": (), "name": "total_nodes", "dtype": "int64"},
Expand All @@ -48,7 +48,8 @@
"edge_activation": {"activation": "relu"},
"node_dense": {"units": 128, "use_bias": True, "activation": "relu"},
"verbose": 10, "depth": 5, "dropout": {"rate": 0.1},
"output_embedding": "graph", "output_to_tensor": True,
"output_embedding": "graph",
"output_to_tensor": None, # deprecated
"output_tensor_type": "padded",
"output_mlp": {"use_bias": [True, True, False], "units": [64, 32, 1],
"activation": ["relu", "relu", "linear"]},
Expand All @@ -61,7 +62,7 @@ def make_model(name: str = None,
inputs: list = None,
input_tensor_type: str = None,
cast_disjoint_kwargs: dict = None,
input_embedding: dict = None,
input_embedding: dict = None, # noqa
input_node_embedding: dict = None,
input_edge_embedding: dict = None,
input_graph_embedding: dict = None,
Expand All @@ -72,41 +73,36 @@ def make_model(name: str = None,
node_dense: dict = None,
dropout: dict = None,
depth: int = None,
verbose: int = None,
verbose: int = None, # noqa
use_graph_state: bool = False,
output_embedding: str = None,
output_to_tensor: bool = None,
output_to_tensor: bool = None, # noqa
output_tensor_type: str = None,
output_mlp: dict = None,
output_scaling: dict = None
):
r"""Make `DMPNN <https://pubs.acs.org/doi/full/10.1021/acs.jcim.9b00237>`__ graph network via functional API.
Default parameters can be found in :obj:`kgcnn.literature.DMPNN.model_default`.
Inputs:
list: `[node_attributes, edge_attributes, edge_indices, edge_pairs, total_nodes, total_edges]` or
`[node_attributes, edge_attributes, edge_indices, edge_pairs, total_nodes, total_edges, state_attributes]`
if `use_graph_state=True` .
- node_attributes (Tensor): Node attributes of shape `(batch, None, F)` or `(batch, None)`
using an embedding layer.
- edge_attributes (Tensor): Edge attributes of shape `(batch, None, F)` or `(batch, None)`
using an embedding layer.
- edge_indices (Tensor): Index list for edges of shape `(batch, None, 2)`.
- edge_pairs (Tensor): Pair mappings for reverse edge for each edge `(batch, None, 1)`.
- state_attributes (Tensor): Environment or graph state attributes of shape `(batch, F)` or `(batch,)`
using an embedding layer.
- total_nodes(Tensor): Number of Nodes in graph of shape `(batch, )` .
- total_edges(Tensor): Number of Edges in graph of shape `(batch, )` .
Outputs:
Tensor: Graph embeddings of shape `(batch, L)` if :obj:`output_embedding="graph"`.
Model inputs:
Model uses the list template of inputs and standard output template.
The supported inputs are :obj:`[nodes, edges, edge_indices, reverse_indices, (graph_state), ...]`
with '...' indicating mask or id tensors following the template below.
Here, reverse indices are in place of angle indices and refer to edges. The graph state is optional and controlled
by `use_graph_state` parameter.
%s
Model outputs:
The standard output template:
%s
Args:
name (str): Name of the model. Should be "DMPNN".
inputs (list): List of dictionaries unpacked in :obj:`keras.layers.Input`. Order must match model definition.
inputs (list): List of dictionaries unpacked in :obj:`Input`. Order must match model definition.
input_tensor_type (str): Input type of graph tensor. Default is "padded".
cast_disjoint_kwargs (dict): Dictionary of arguments for :obj:`CastBatchedIndicesToDisjoint` .
cast_disjoint_kwargs (dict): Dictionary of arguments for casting layers if used.
input_embedding (dict): Deprecated in favour of input_node_embedding etc.
input_node_embedding (dict): Dictionary of arguments for nodes unpacked in :obj:`Embedding` layers.
input_edge_embedding (dict): Dictionary of arguments for edge unpacked in :obj:`Embedding` layers.
Expand All @@ -122,7 +118,7 @@ def make_model(name: str = None,
verbose (int): Level for print information.
use_graph_state (bool): Whether to use graph state information. Default is False.
output_embedding (str): Main embedding task for graph network. Either "node", "edge" or "graph".
output_to_tensor (bool): Whether to cast model output to :obj:`Tensor`.
output_to_tensor (bool): WDeprecated in favour of `output_tensor_type` .
output_tensor_type (str): Output type of graph tensors such as nodes or edges. Default is "padded".
output_mlp (dict): Dictionary of layer arguments unpacked in the final classification :obj:`MLP` layer block.
Defines number of model outputs and activation.
Expand Down Expand Up @@ -150,10 +146,12 @@ def make_model(name: str = None,
# Wrapping disjoint model.
out = model_disjoint(
[n, ed, edi, batch_id_node, e_pairs, count_nodes, gs],
use_node_embedding=len(inputs[0]['shape']) < 2, use_edge_embedding=len(inputs[1]['shape']) < 2,
use_graph_embedding=len(inputs[7]["shape"]) < 1 if use_graph_state else False,
use_node_embedding=("int" in inputs[0]['dtype']) if input_node_embedding is not None else False,
use_edge_embedding=("int" in inputs[1]['dtype']) if input_edge_embedding is not None else False,
use_graph_embedding=("int" in inputs[4]['dtype']) if input_graph_embedding is not None else False,
input_node_embedding=input_node_embedding,
input_edge_embedding=input_edge_embedding, input_graph_embedding=input_graph_embedding,
input_edge_embedding=input_edge_embedding,
input_graph_embedding=input_graph_embedding,
pooling_args=pooling_args, edge_initialize=edge_initialize, edge_activation=edge_activation,
node_dense=node_dense, dropout=dropout, depth=depth, use_graph_state=use_graph_state,
output_embedding=output_embedding, output_mlp=output_mlp, edge_dense=edge_dense
Expand All @@ -180,3 +178,6 @@ def set_scale(*args, **kwargs):
setattr(model, "set_scale", set_scale)

return model


make_model.__doc__ = make_model.__doc__ % (template_cast_list_input.__doc__, template_cast_output.__doc__)
34 changes: 18 additions & 16 deletions kgcnn/literature/GAT/_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
model_default = {
"name": "GAT",
"inputs": [
{"shape": (None,), "name": "node_attributes", "dtype": "float32"},
{"shape": (None,), "name": "edge_attributes", "dtype": "float32"},
{"shape": (None,), "name": "node_number", "dtype": "int64"},
{"shape": (None,), "name": "edge_number", "dtype": "int64"},
{"shape": (None, 2), "name": "edge_indices", "dtype": "int64"},
{"shape": (), "name": "total_nodes", "dtype": "int64"},
{"shape": (), "name": "total_edges", "dtype": "int64"}
Expand All @@ -34,15 +34,16 @@
"cast_disjoint_kwargs": {},
"input_embedding": None, # deprecated
"input_node_embedding": {"input_dim": 95, "output_dim": 64},
"input_edge_embedding": {"input_dim": 5, "output_dim": 64},
"input_edge_embedding": {"input_dim": 10, "output_dim": 64},
"attention_args": {"units": 32, "use_final_activation": False, "use_edge_features": True,
"has_self_loops": True, "activation": "kgcnn>leaky_relu", "use_bias": True},
"pooling_nodes_args": {"pooling_method": "scatter_mean"},
"depth": 3, "attention_heads_num": 5,
"depth": 3,
"attention_heads_num": 5,
"attention_heads_concat": False,
"verbose": 10,
"output_embedding": "graph",
"output_to_tensor": True,
"output_to_tensor": None, # deprecated
"output_tensor_type": "padded",
"output_mlp": {"use_bias": [True, True, False], "units": [25, 10, 1],
"activation": ["relu", "relu", "sigmoid"]},
Expand All @@ -54,7 +55,7 @@
def make_model(inputs: list = None,
input_tensor_type: str = None,
cast_disjoint_kwargs: dict = None,
input_embedding: dict = None,
input_embedding: dict = None, # noqa
input_node_embedding: dict = None,
input_edge_embedding: dict = None,
attention_args: dict = None,
Expand All @@ -63,14 +64,14 @@ def make_model(inputs: list = None,
attention_heads_num: int = None,
attention_heads_concat: bool = None,
name: str = None,
verbose: int = None,
verbose: int = None, # noqa
output_embedding: str = None,
output_to_tensor: bool = None,
output_to_tensor: bool = None, # noqa
output_mlp: dict = None,
output_scaling: dict = None,
output_tensor_type: str = None,
):
r"""Make `GAT <https://arxiv.org/abs/1710.10903>`_ graph network via functional API.
r"""Make `GAT <https://arxiv.org/abs/1710.10903>`__ graph network via functional API.
Default parameters can be found in :obj:`kgcnn.literature.GAT.model_default`.
Model inputs:
Expand All @@ -85,10 +86,9 @@ def make_model(inputs: list = None,
%s
Args:
inputs (list): List of dictionaries unpacked in :obj:`tf.keras.layers.Input`. Order must match model definition.
cast_disjoint_kwargs (dict): Dictionary of arguments for :obj:`CastBatchedIndicesToDisjoint` .
inputs (list): List of dictionaries unpacked in :obj:`Input`. Order must match model definition.
cast_disjoint_kwargs (dict): Dictionary of arguments for casting layers if used.
input_tensor_type (str): Input type of graph tensor. Default is "padded".
input_embedding (dict): Deprecated in favour of input_node_embedding etc.
input_node_embedding (dict): Dictionary of arguments for nodes unpacked in :obj:`Embedding` layers.
Expand All @@ -108,7 +108,7 @@ def make_model(inputs: list = None,
output_scaling (dict): Dictionary of layer arguments unpacked in scaling layers.
Returns:
:obj:`ks.models.Model`
:obj:`keras.models.Model`
"""
# Make input
model_inputs = [Input(**x) for x in inputs]
Expand All @@ -121,8 +121,10 @@ def make_model(inputs: list = None,
# Wrapping disjoint model.
out = model_disjoint(
[n, ed, disjoint_indices, batch_id_node, count_nodes],
use_node_embedding=len(inputs[0]['shape']) < 2, use_edge_embedding=len(inputs[1]['shape']) < 2,
input_node_embedding=input_node_embedding, input_edge_embedding=input_edge_embedding,
use_node_embedding=("int" in inputs[0]['dtype']) if input_node_embedding is not None else False,
use_edge_embedding=("int" in inputs[1]['dtype']) if input_edge_embedding is not None else False,
input_node_embedding=input_node_embedding,
input_edge_embedding=input_edge_embedding,
attention_args=attention_args, pooling_nodes_args=pooling_nodes_args, depth=depth,
attention_heads_num=attention_heads_num, attention_heads_concat=attention_heads_concat,
output_embedding=output_embedding, output_mlp=output_mlp
Expand Down Expand Up @@ -151,4 +153,4 @@ def set_scale(*args, **kwargs):
return model


make_model.__doc__ = make_model.__doc__ % (template_cast_list_input.__doc__, template_cast_output.__doc__)
make_model.__doc__ = make_model.__doc__ % (template_cast_list_input.__doc__, template_cast_output.__doc__)
Loading

0 comments on commit 0a27b26

Please sign in to comment.