compiler
Defines the mG compiler and the basic mG functions.
This package defines a compiler for mG programs and the data structures to instantiate it. It also provides the various classes that allow for the definition of mG functions.
The package contains the following classes:
NodeConfig
EdgeConfig
CompilerConfig
MGCompiler
PsiNonLocal
PsiLocal
PsiGlobal
Phi
Sigma
Constant
Pi
The module contains the following functions:
make_uoperator(op, name)
make_boperator(op, name)
make_koperator(op, name)
PsiLocal ¶
Bases: Psi
A psi function of the mG language that only applies a local transformation of node labels.
A local transformation of node labels \(\psi: T \rightarrow U\).
Examples:
Parameters:
-
f
(Callable | None
, default:None
) –The function that this object will run when called. The function must be compatible with TensorFlow's broadcasting rules. The function is expected to take in input a tensor X with shape
(n_nodes, n_node_features)
, containing the labels of every node in the graph, and return a tensor of shape(n_nodes, n_new_node_features)
, containing the transformed labels. The function is not supposed to use global information, but this is not enforced. -
name
(str | None
, default:None
) –The name of the function.
Source code in libmg/compiler/functions.py
fname
property
¶
The name of this function. This can be either the name provided during initialization, if it was provided, or the dynamic class name.
make
classmethod
¶
Returns a zero-argument function that when called returns an instance of cls
initialized with the provided function f
and name
.
The class cls
is supposed to be a Function
subclass. Calling this method on a suitable Function
subclass
creates a zero-argument lambda that returns an instance of such subclass. This way, whenever that function is used in a mG program, the dictionary
automatically regenerates the instance. This is useful when the function has trainable weights, which are not supposed to be shared with other instances
of the function.
Examples:
Parameters:
-
name
(str | None
) –The name of the function.
-
f
(Callable
) –The function that will be used to instantiate
cls
.
Source code in libmg/compiler/functions.py
make_parametrized
classmethod
¶
make_parametrized(name: str | None, f: Callable[[str], Callable] | Callable[..., Any]) -> Callable[[str], T_A]
Returns a one-argument function that when called with the argument a
returns an instance of cls
initialized with the result of the application of a
to the function f
and name
.
The class cls
is supposed to be a Function
subclass. Calling this method on a suitable Function
subclass
creates a one-argument lambda that returns an instance of such subclass. This way, whenever that function is used in a mG program, the dictionary
automatically regenerates the instance. This is useful when the function has trainable weights, which are not supposed to be shared with other instances
of the function. The function f
may have the form lambda x: lambda ... : ...
or lambda x, ... : ...
. The one argument of the lambda returned
by this function corresponds to the x
argument of f
.
Examples:
>>> Phi.make_parametrized('Add', lambda y: lambda i, e, j: j + int(y) * e)
<function Function.make_parametrized.<locals>.<lambda> at 0x...>
>>> Phi.make_parametrized('Add', lambda y, i, e, j: j + int(y) * e)
<function Function.make_parametrized.<locals>.<lambda> at 0x...>
Parameters:
-
name
(str | None
) –The name of the function returned by
f
. -
f
(Callable[[str], Callable] | Callable[..., Any]
) –The function that when applied to some argument
a
returns the function that will be used to instantiatecls
.
Source code in libmg/compiler/functions.py
PsiGlobal ¶
PsiGlobal(single_op: Callable | None = None, multiple_op: Callable | None = None, name: str | None = None)
Bases: PsiNonLocal
A psi function of the mG language that only applies a global transformation of node labels.
A global transformation (i.e. pooling) of node labels \(\psi: T^* \rightarrow U\). For single graph datasets, which use the
SingleGraphLoader
, only the single_op
parameter is necessary. For multiple graph datasets, using the
MultipleGraphLoader
, only the multiple_op
parameter is necessary. The multiple_op
argument is a function which
takes an additional parameter to distinguish which values in the first argument refer to which graph. For
more information, refer to the disjoint data mode in the Spektral library documentation <https://graphneural.network/data-modes/#disjoint-mode/>
_.
Examples:
>>> PsiGlobal(single_op=lambda x: tf.reduce_sum(x, axis=0, keepdims=True), multiple_op=lambda x, i: tf.math.segment_sum(x, i), name='SumPooling')
<PsiGlobal ...>
Parameters:
-
single_op
(Callable | None
, default:None
) –The function to be used in conjunction with a
SingleGraphLoader
. The function is expected to take in input a tensor X with shape(n_nodes, n_node_features)
, containing the labels of every node in the graph, and return a tensor of shape(n_pooled_features, )
, containing the pooled output. This output is then broadcast to label every node. -
multiple_op
(Callable | None
, default:None
) –The function to be used in conjunction with a
MultipleGraphLoader
. The function is expected to take in input a tensor with shape(n_nodes, n_node_features)
, containing the labels of every node in the graph, and a tensor of graph indices of shape(n_nodes, 1)
, that mark to which graph every node belongs. The function is expected to return a tensor of shape(n_graphs, n_pooled_features)
containing the pooled outputs for each distinct graph index. This output is then broadcast to label every node of each graph with the pooled features for that graph. -
name
(str | None
, default:None
) –The name of the function.
Source code in libmg/compiler/functions.py
fname
property
¶
The name of this function. This can be either the name provided during initialization, if it was provided, or the dynamic class name.
make
classmethod
¶
make(name: str | None, single_op: Callable | None = None, multiple_op: Callable | None = None) -> Callable[[], PsiNonLocal]
Returns a zero-argument function that when called returns an instance of cls
initialized with the provided functions single_op
and/or
multiple_op
and name
.
Calling this method on a PsiNonLocal
class creates a zero-argument lambda that returns an instance of such subclass. This way, whenever that
function is used in a mG program, the dictionary automatically regenerates the instance. This is useful when the function has trainable weights,
which are not supposed to be shared with other instances of the function.
Examples:
>>> PsiNonLocal.make('Successor', lambda x: x + 1)
<function PsiNonLocal.make.<locals>.<lambda> at 0x...>
Parameters:
-
name
(str | None
) –The name of the function returned by
single_op
andmultiple_op
. -
single_op
(Callable | None
, default:None
) –The function that will be used to instantiate
cls
as thesingle_op
argument. -
multiple_op
(Callable | None
, default:None
) –The function that will be used to instantiate
cls
as themultiple_op
argument.
Raises:
-
ValueError
–Neither
single_op
normultiple_op
have been provided.
Source code in libmg/compiler/functions.py
make_parametrized
classmethod
¶
make_parametrized(
name: str | None,
single_op: Callable[[str], Callable] | Callable[..., Any] | None = None,
multiple_op: Callable[[str], Callable] | Callable[..., Any] | None = None,
) -> Callable[[str], PsiNonLocal]
Returns a one-argument function that when called with the argument a
returns an instance of cls
initialized with the result of the
application of a
to the function single_op
and/or multiple_op
, and name
.
Calling this method on a PsiNonLocal
class creates a one-argument lambda that returns an instance of such subclass. This way, whenever that
function is used in a mG program, the dictionary automatically regenerates the instance. This is useful when the function has trainable weights,
which are not supposed to be shared with other instances of the function. The functions single_op
and multiple_op
may have the form lambda
x: lambda ... : ...
or lambda x, ... : ...
. The one argument of the lambda returned by this function corresponds to the x
argument of
single_op
and multiple_op
.
Examples:
>>> PsiNonLocal.make_parametrized(name='Add', single_op=lambda y: lambda x: x + y, multiple_op=lambda y: lambda x, i: x + y)
<function PsiNonLocal.make_parametrized.<locals>.<lambda> at 0x...>
>>> PsiNonLocal.make_parametrized( name='Add', single_op=lambda y, x: x + y, multiple_op=lambda y, x, i: x + y)
<function PsiNonLocal.make_parametrized.<locals>.<lambda> at 0x...>
Parameters:
-
name
(str | None
) –The name of the function returned by
single_op
andmultiple_op
. -
single_op
(Callable[[str], Callable] | Callable[..., Any] | None
, default:None
) –The function that when applied to some argument
a
returns the function that will be used to instantiatecls
as thesingle_op
argument. -
multiple_op
(Callable[[str], Callable] | Callable[..., Any] | None
, default:None
) –The function that when applied to some argument
a
returns the function that will be used to instantiatecls
as themultiple_op
argument.
Raises:
-
ValueError
–Neither
single_op
normultiple_op
have been provided.
Source code in libmg/compiler/functions.py
PsiNonLocal ¶
PsiNonLocal(single_op: Callable | None = None, multiple_op: Callable | None = None, name: str | None = None)
Bases: Psi
A psi function of the mG language.
A non-local function applied on node labels \(\psi: T^* \times T \rightarrow U\). For single graph datasets, which use the
SingleGraphLoader
, only the single_op
parameter is necessary. For multiple graph datasets, using the
MultipleGraphLoader
, only the multiple_op
parameter is necessary. The multiple_op
argument is a function which
takes an additional parameter to distinguish which values in the first argument refer to which graph. For
more information, refer to the disjoint data mode in the Spektral library documentation <https://graphneural.network/data-modes/#disjoint-mode/>
_.
Examples:
>>> PsiNonLocal(single_op=lambda x: x + 1, multiple_op=lambda x, i: x + 1, name='Add1')
<PsiNonLocal ...>
Attributes:
-
single_op
–A function to be used in conjunction with a
SingleGraphLoader
-
multiple_op
–A function to be used in conjunction with a
MultipleGraphLoader
Parameters:
-
single_op
(Callable | None
, default:None
) –The function to be used in conjunction with a
SingleGraphLoader
. The function must be compatible with TensorFlow's broadcasting rules. The function is expected to take in input a tensor X with shape(n_nodes, n_node_features)
, containing the labels of every node in the graph, and return a tensor of shape(n_nodes, n_new_node_features)
, containing the transformed labels. The function can use broadcasting to emulate the tuple (T*, T) in the definition of psi. -
multiple_op
(Callable | None
, default:None
) –The function to be used in conjunction with a
MultipleGraphLoader
. The function must be compatible with TensorFlow's broadcasting rules. The function is expected to take in input a tensor X with shape(n_nodes, n_node_features)
, containing the labels of every node in the graph, and a tensor of graph indices of shape(n_nodes, 1)
, that mark to which graph every node belongs. The function is expected to return a tensor of shape(n_nodes, n_new_node_features)
containing the transformed labels. The function can use broadcasting to emulate the tuple (T*, T) in the definition of psi. -
name
(str | None
, default:None
) –The name of the function.
Source code in libmg/compiler/functions.py
fname
property
¶
The name of this function. This can be either the name provided during initialization, if it was provided, or the dynamic class name.
make
classmethod
¶
make(name: str | None, single_op: Callable | None = None, multiple_op: Callable | None = None) -> Callable[[], PsiNonLocal]
Returns a zero-argument function that when called returns an instance of cls
initialized with the provided functions single_op
and/or
multiple_op
and name
.
Calling this method on a PsiNonLocal
class creates a zero-argument lambda that returns an instance of such subclass. This way, whenever that
function is used in a mG program, the dictionary automatically regenerates the instance. This is useful when the function has trainable weights,
which are not supposed to be shared with other instances of the function.
Examples:
>>> PsiNonLocal.make('Successor', lambda x: x + 1)
<function PsiNonLocal.make.<locals>.<lambda> at 0x...>
Parameters:
-
name
(str | None
) –The name of the function returned by
single_op
andmultiple_op
. -
single_op
(Callable | None
, default:None
) –The function that will be used to instantiate
cls
as thesingle_op
argument. -
multiple_op
(Callable | None
, default:None
) –The function that will be used to instantiate
cls
as themultiple_op
argument.
Raises:
-
ValueError
–Neither
single_op
normultiple_op
have been provided.
Source code in libmg/compiler/functions.py
make_parametrized
classmethod
¶
make_parametrized(
name: str | None,
single_op: Callable[[str], Callable] | Callable[..., Any] | None = None,
multiple_op: Callable[[str], Callable] | Callable[..., Any] | None = None,
) -> Callable[[str], PsiNonLocal]
Returns a one-argument function that when called with the argument a
returns an instance of cls
initialized with the result of the
application of a
to the function single_op
and/or multiple_op
, and name
.
Calling this method on a PsiNonLocal
class creates a one-argument lambda that returns an instance of such subclass. This way, whenever that
function is used in a mG program, the dictionary automatically regenerates the instance. This is useful when the function has trainable weights,
which are not supposed to be shared with other instances of the function. The functions single_op
and multiple_op
may have the form lambda
x: lambda ... : ...
or lambda x, ... : ...
. The one argument of the lambda returned by this function corresponds to the x
argument of
single_op
and multiple_op
.
Examples:
>>> PsiNonLocal.make_parametrized(name='Add', single_op=lambda y: lambda x: x + y, multiple_op=lambda y: lambda x, i: x + y)
<function PsiNonLocal.make_parametrized.<locals>.<lambda> at 0x...>
>>> PsiNonLocal.make_parametrized( name='Add', single_op=lambda y, x: x + y, multiple_op=lambda y, x, i: x + y)
<function PsiNonLocal.make_parametrized.<locals>.<lambda> at 0x...>
Parameters:
-
name
(str | None
) –The name of the function returned by
single_op
andmultiple_op
. -
single_op
(Callable[[str], Callable] | Callable[..., Any] | None
, default:None
) –The function that when applied to some argument
a
returns the function that will be used to instantiatecls
as thesingle_op
argument. -
multiple_op
(Callable[[str], Callable] | Callable[..., Any] | None
, default:None
) –The function that when applied to some argument
a
returns the function that will be used to instantiatecls
as themultiple_op
argument.
Raises:
-
ValueError
–Neither
single_op
normultiple_op
have been provided.
Source code in libmg/compiler/functions.py
Phi ¶
Phi(f: Callable[[tuple[tf.Tensor, ...], tf.Tensor, tuple[tf.Tensor, ...]], tf.Tensor | tuple[tf.Tensor, ...]] | None = None, name: str | None = None)
Bases: Function
A phi function of the mG language.
A function \(\varphi: T \times U \times T \rightarrow V\) to compute the message sent by a node i to a node j through edge e.
Examples:
Parameters:
-
f
(Callable[[tuple[Tensor, ...], Tensor, tuple[Tensor, ...]], Tensor | tuple[Tensor, ...]] | None
, default:None
) –The function that this object will run when called. The function must be compatible with TensorFlow's broadcasting rules. The function is expected to take in input a tensor X1 with shape
(n_edges, n_node_features)
, containing the labels of all nodes sending a message, a tensor E with shape(n_edges, n_edge_features)
, containing the labels of all edges in the graph, and a tensor X2, containing the labels of all nodes receiving a message. The function is expected to return a tensor with shape(n_edges, n_message_features)
, containing the messages to be sent to the destination nodes. -
name
(str | None
, default:None
) –The name of the function.
Source code in libmg/compiler/functions.py
fname
property
¶
The name of this function. This can be either the name provided during initialization, if it was provided, or the dynamic class name.
make
classmethod
¶
Returns a zero-argument function that when called returns an instance of cls
initialized with the provided function f
and name
.
The class cls
is supposed to be a Function
subclass. Calling this method on a suitable Function
subclass
creates a zero-argument lambda that returns an instance of such subclass. This way, whenever that function is used in a mG program, the dictionary
automatically regenerates the instance. This is useful when the function has trainable weights, which are not supposed to be shared with other instances
of the function.
Examples:
Parameters:
-
name
(str | None
) –The name of the function.
-
f
(Callable
) –The function that will be used to instantiate
cls
.
Source code in libmg/compiler/functions.py
make_parametrized
classmethod
¶
make_parametrized(name: str | None, f: Callable[[str], Callable] | Callable[..., Any]) -> Callable[[str], T_A]
Returns a one-argument function that when called with the argument a
returns an instance of cls
initialized with the result of the application of a
to the function f
and name
.
The class cls
is supposed to be a Function
subclass. Calling this method on a suitable Function
subclass
creates a one-argument lambda that returns an instance of such subclass. This way, whenever that function is used in a mG program, the dictionary
automatically regenerates the instance. This is useful when the function has trainable weights, which are not supposed to be shared with other instances
of the function. The function f
may have the form lambda x: lambda ... : ...
or lambda x, ... : ...
. The one argument of the lambda returned
by this function corresponds to the x
argument of f
.
Examples:
>>> Phi.make_parametrized('Add', lambda y: lambda i, e, j: j + int(y) * e)
<function Function.make_parametrized.<locals>.<lambda> at 0x...>
>>> Phi.make_parametrized('Add', lambda y, i, e, j: j + int(y) * e)
<function Function.make_parametrized.<locals>.<lambda> at 0x...>
Parameters:
-
name
(str | None
) –The name of the function returned by
f
. -
f
(Callable[[str], Callable] | Callable[..., Any]
) –The function that when applied to some argument
a
returns the function that will be used to instantiatecls
.
Source code in libmg/compiler/functions.py
Sigma ¶
Sigma(f: Callable[[tuple[tf.Tensor, ...], tf.Tensor, int, tuple[tf.Tensor, ...]], tf.Tensor | tuple[tf.Tensor, ...]] | None = None, name: str | None = None)
Bases: Function
A sigma function of the mG language.
A function \(\sigma: T^* \times U \rightarrow V\) to aggregate the messages sent to a node, including the current label of the node.
Examples:
Parameters:
-
f
(Callable[[tuple[Tensor, ...], Tensor, int, tuple[Tensor, ...]], Tensor | tuple[Tensor, ...]] | None
, default:None
) –The function that this object will run when called. The function must be compatible with TensorFlow's broadcasting rules. The function is expected to take in input a tensor M with shape
(n_edges, n_message_features)
, containing the generated messages, a tensor IDX with shape(n_edges,)
, containing the ids of the node each message is being sent to, the total number of the nodes involved, and a tensor X, containing the current node labels. The function is expected to return a tensor with shape(n_nodes, n_new_node_features)
, containing the new node labels. -
name
(str | None
, default:None
) –The name of the function.
Source code in libmg/compiler/functions.py
fname
property
¶
The name of this function. This can be either the name provided during initialization, if it was provided, or the dynamic class name.
make
classmethod
¶
Returns a zero-argument function that when called returns an instance of cls
initialized with the provided function f
and name
.
The class cls
is supposed to be a Function
subclass. Calling this method on a suitable Function
subclass
creates a zero-argument lambda that returns an instance of such subclass. This way, whenever that function is used in a mG program, the dictionary
automatically regenerates the instance. This is useful when the function has trainable weights, which are not supposed to be shared with other instances
of the function.
Examples:
Parameters:
-
name
(str | None
) –The name of the function.
-
f
(Callable
) –The function that will be used to instantiate
cls
.
Source code in libmg/compiler/functions.py
make_parametrized
classmethod
¶
make_parametrized(name: str | None, f: Callable[[str], Callable] | Callable[..., Any]) -> Callable[[str], T_A]
Returns a one-argument function that when called with the argument a
returns an instance of cls
initialized with the result of the application of a
to the function f
and name
.
The class cls
is supposed to be a Function
subclass. Calling this method on a suitable Function
subclass
creates a one-argument lambda that returns an instance of such subclass. This way, whenever that function is used in a mG program, the dictionary
automatically regenerates the instance. This is useful when the function has trainable weights, which are not supposed to be shared with other instances
of the function. The function f
may have the form lambda x: lambda ... : ...
or lambda x, ... : ...
. The one argument of the lambda returned
by this function corresponds to the x
argument of f
.
Examples:
>>> Phi.make_parametrized('Add', lambda y: lambda i, e, j: j + int(y) * e)
<function Function.make_parametrized.<locals>.<lambda> at 0x...>
>>> Phi.make_parametrized('Add', lambda y, i, e, j: j + int(y) * e)
<function Function.make_parametrized.<locals>.<lambda> at 0x...>
Parameters:
-
name
(str | None
) –The name of the function returned by
f
. -
f
(Callable[[str], Callable] | Callable[..., Any]
) –The function that when applied to some argument
a
returns the function that will be used to instantiatecls
.
Source code in libmg/compiler/functions.py
Constant ¶
Bases: PsiLocal
A constant psi function of the mG language.
A constant function \(\psi: T \rightarrow U\) that maps every node label to a constant value.
Examples:
Parameters:
-
v
(Tensor
) –A scalar or tensor value that identifies the constant function.
-
name
(str | None
, default:None
) –The name of the function.
Source code in libmg/compiler/functions.py
fname
property
¶
The name of this function. This can be either the name provided during initialization, if it was provided, or the dynamic class name.
make
classmethod
¶
Returns a zero-argument function that when called returns an instance of cls
initialized with the provided function f
and name
.
The class cls
is supposed to be a Function
subclass. Calling this method on a suitable Function
subclass
creates a zero-argument lambda that returns an instance of such subclass. This way, whenever that function is used in a mG program, the dictionary
automatically regenerates the instance. This is useful when the function has trainable weights, which are not supposed to be shared with other instances
of the function.
Examples:
Parameters:
-
name
(str | None
) –The name of the function.
-
f
(Callable
) –The function that will be used to instantiate
cls
.
Source code in libmg/compiler/functions.py
make_parametrized
classmethod
¶
make_parametrized(name: str | None, f: Callable[[str], Callable] | Callable[..., Any]) -> Callable[[str], T_A]
Returns a one-argument function that when called with the argument a
returns an instance of cls
initialized with the result of the application of a
to the function f
and name
.
The class cls
is supposed to be a Function
subclass. Calling this method on a suitable Function
subclass
creates a one-argument lambda that returns an instance of such subclass. This way, whenever that function is used in a mG program, the dictionary
automatically regenerates the instance. This is useful when the function has trainable weights, which are not supposed to be shared with other instances
of the function. The function f
may have the form lambda x: lambda ... : ...
or lambda x, ... : ...
. The one argument of the lambda returned
by this function corresponds to the x
argument of f
.
Examples:
>>> Phi.make_parametrized('Add', lambda y: lambda i, e, j: j + int(y) * e)
<function Function.make_parametrized.<locals>.<lambda> at 0x...>
>>> Phi.make_parametrized('Add', lambda y, i, e, j: j + int(y) * e)
<function Function.make_parametrized.<locals>.<lambda> at 0x...>
Parameters:
-
name
(str | None
) –The name of the function returned by
f
. -
f
(Callable[[str], Callable] | Callable[..., Any]
) –The function that when applied to some argument
a
returns the function that will be used to instantiatecls
.
Source code in libmg/compiler/functions.py
Pi ¶
Bases: PsiLocal
A projection psi function of the mG language.
A projection function \(\psi: T^n \rightarrow T^m\) that maps every node label to a projection of itself.
Examples:
Parameters:
-
i
(int
) –0-based index, start position of the projection function, inclusive.
-
j
(int | None
, default:None
) –end position of the projection function, exclusive. Defaults to i + 1.
-
name
(str | None
, default:None
) –The name of the function.
Raises:
-
ValueError
–start and end index are equal.
Source code in libmg/compiler/functions.py
fname
property
¶
The name of this function. This can be either the name provided during initialization, if it was provided, or the dynamic class name.
make
classmethod
¶
Returns a zero-argument function that when called returns an instance of cls
initialized with the provided function f
and name
.
The class cls
is supposed to be a Function
subclass. Calling this method on a suitable Function
subclass
creates a zero-argument lambda that returns an instance of such subclass. This way, whenever that function is used in a mG program, the dictionary
automatically regenerates the instance. This is useful when the function has trainable weights, which are not supposed to be shared with other instances
of the function.
Examples:
Parameters:
-
name
(str | None
) –The name of the function.
-
f
(Callable
) –The function that will be used to instantiate
cls
.
Source code in libmg/compiler/functions.py
make_parametrized
classmethod
¶
make_parametrized(name: str | None, f: Callable[[str], Callable] | Callable[..., Any]) -> Callable[[str], T_A]
Returns a one-argument function that when called with the argument a
returns an instance of cls
initialized with the result of the application of a
to the function f
and name
.
The class cls
is supposed to be a Function
subclass. Calling this method on a suitable Function
subclass
creates a one-argument lambda that returns an instance of such subclass. This way, whenever that function is used in a mG program, the dictionary
automatically regenerates the instance. This is useful when the function has trainable weights, which are not supposed to be shared with other instances
of the function. The function f
may have the form lambda x: lambda ... : ...
or lambda x, ... : ...
. The one argument of the lambda returned
by this function corresponds to the x
argument of f
.
Examples:
>>> Phi.make_parametrized('Add', lambda y: lambda i, e, j: j + int(y) * e)
<function Function.make_parametrized.<locals>.<lambda> at 0x...>
>>> Phi.make_parametrized('Add', lambda y, i, e, j: j + int(y) * e)
<function Function.make_parametrized.<locals>.<lambda> at 0x...>
Parameters:
-
name
(str | None
) –The name of the function returned by
f
. -
f
(Callable[[str], Callable] | Callable[..., Any]
) –The function that when applied to some argument
a
returns the function that will be used to instantiatecls
.
Source code in libmg/compiler/functions.py
NodeConfig ¶
Bases: LabelConfig
Defines the signature of a node label.
Parameters:
-
node_type
(DType
) –Type of the node labels.
-
node_size
(int
) –Dimension of the node labels.
Source code in libmg/compiler/compiler.py
EdgeConfig ¶
Bases: LabelConfig
Defines the signature of an edge label.
Parameters:
-
edge_type
(DType
) –Type of the edge labels.
-
edge_size
(int
) –Dimension of the edge labels.
Source code in libmg/compiler/compiler.py
CompilerConfig ¶
CompilerConfig(node_config: NodeConfig, edge_config: EdgeConfig | None, matrix_type: tf.DType, tolerance: dict[str, float], multiple_loader: bool)
Defines the configuration for the mG compiler.
It is recommended to use the static constructor methods to instantiate this class.
Parameters:
-
node_config
(NodeConfig
) –The signature of the initial node labels of the graphs.
-
edge_config
(EdgeConfig | None
) –The signature of the initial edge labels of the graphs.
-
matrix_type
(DType
) –The signature of the adjacency matrix of the graphs.
-
tolerance
(dict[str, float]
) –The tolerance values for the data types (typically floats) that require an approximate fixed point solution.
-
multiple_loader
(bool
) –True if the models generated by the mG compiler will receive their inputs by a
MultipleGraphLoader
.
Source code in libmg/compiler/compiler.py
edge_feature_type
property
¶
Returns the type of the edge labels, if present.
edge_feature_size
property
¶
Returns the dimension of the edge labels, if present.
use_edges
property
¶
Returns whether the mG compiler expects edge labels in the graph.
tolerance
property
¶
Returns the mapping between types and tolerance values.
use_multiple_loader
property
¶
Returns whether the mG compiler expects the usage of the MultipleGraphLoader
.
input_spec
property
¶
Returns the input signature that the mG compiler expects for every model it will produce.
xa_config
staticmethod
¶
xa_config(node_config: NodeConfig, matrix_type: tf.DType, tolerance: dict[str, float]) -> CompilerConfig
Returns a CompilationConfig
object that tells the mG compiler to expect node labels but no edge labels in the graph
and the use of the SingleGraphLoader
for the inputs to the model.
Parameters:
-
node_config
(NodeConfig
) –The signature of the initial node labels of the graphs.
-
matrix_type
(DType
) –The signature of the adjacency matrix of the graphs.
-
tolerance
(dict[str, float]
) –The tolerance values for the data types (typically floats) that require an approximate fixed point solution.
Source code in libmg/compiler/compiler.py
xai_config
staticmethod
¶
xai_config(node_config: NodeConfig, matrix_type: tf.DType, tolerance: dict[str, float]) -> CompilerConfig
Returns a CompilationConfig
object that tells the mG compiler to expect nodes labels but no edge labels in the graph
and the use of the MultipleGraphLoader
for the inputs to the model.
Parameters:
-
node_config
(NodeConfig
) –The signature of the initial node labels of the graphs.
-
matrix_type
(DType
) –The signature of the adjacency matrix of the graphs.
-
tolerance
(dict[str, float]
) –The tolerance values for the data types (typically floats) that require an approximate fixed point solution.
Source code in libmg/compiler/compiler.py
xae_config
staticmethod
¶
xae_config(node_config: NodeConfig, edge_config: EdgeConfig, matrix_type: tf.DType, tolerance: dict[str, float]) -> CompilerConfig
Returns a CompilationConfig
object that tells the mG compiler to expect node labels and edge labels in the graph
and the use of the SingleGraphLoader
for the inputs to the model.
Parameters:
-
node_config
(NodeConfig
) –The signature of the initial node labels of the graphs.
-
edge_config
(EdgeConfig
) –The signature of the initial edge labels of the graphs.
-
matrix_type
(DType
) –The signature of the adjacency matrix of the graphs.
-
tolerance
(dict[str, float]
) –The tolerance values for the data types (typically floats) that require an approximate fixed point solution.
Source code in libmg/compiler/compiler.py
xaei_config
staticmethod
¶
xaei_config(node_config: NodeConfig, edge_config: EdgeConfig, matrix_type: tf.DType, tolerance: dict[str, float]) -> CompilerConfig
Returns a CompilationConfig
object that tells the mG compiler to expect node labels and edge labels in the graph
and the use of the MultipleGraphLoader
for the inputs to the model.
Parameters:
-
node_config
(NodeConfig
) –The signature of the initial node labels of the graphs.
-
edge_config
(EdgeConfig
) –The signature of the initial edge labels of the graphs.
-
matrix_type
(DType
) –The signature of the adjacency matrix of the graphs.
-
tolerance
(dict[str, float]
) –The tolerance values for the data types (typically floats) that require an approximate fixed point solution.
Source code in libmg/compiler/compiler.py
MGCompiler ¶
MGCompiler(
psi_functions: dict[str, Psi | Callable[[], Psi] | Callable[[str], Psi]],
sigma_functions: dict[str, Sigma | Callable[[], Sigma] | Callable[[str], Sigma]],
phi_functions: dict[str, Phi | Callable[[], Phi] | Callable[[str], Phi]],
config: CompilerConfig,
)
The compiler for mG programs.
A program is transformed into a TensorFlow model using the compile
method.
Attributes:
-
config
–The configuration for this compiler instance.
-
model_inputs
–The input layers for the models generated by the compiler.
-
model_input_spec
–The input signature for the models generated by the compiler.
-
dummy_dataset
–The dataset used to trace the models generated by the compiler.
-
visitor
–The visitor that traverses an expression tree to construct the model.
Parameters:
-
psi_functions
(dict[str, Psi | Callable[[], Psi] | Callable[[str], Psi]]
) –The psi functions that this compiler will recognize.
-
sigma_functions
(dict[str, Sigma | Callable[[], Sigma] | Callable[[str], Sigma]]
) –The sigma functions that this compiler will recognize.
-
phi_functions
(dict[str, Phi | Callable[[], Phi] | Callable[[str], Phi]]
) –The phi functions that this compiler will recognize.
-
config
(CompilerConfig
) –The configuration for the compiler.
Source code in libmg/compiler/compiler.py
compile ¶
Compiles a mG program into a TensorFlow model.
Parameters:
-
expr
(str | Tree
) –The mG program to compile.
-
verbose
(bool
, default:False
) –If true, prints some debugging information during the compilation step.
-
memoize
(bool
, default:False
) –If true, memoize intermediate outputs during compilation.
Returns:
-
MGModel
–The TensorFlow model that implements
expr
.
Source code in libmg/compiler/compiler.py
trace ¶
trace(model: MGModel, api: Literal['call', 'predict', 'predict_on_batch']) -> Tuple[MGModel | Callable, float]
Performs tracing on the model, and returns it, together with the time in seconds elapsed for tracing.
The predict_on_batch
API cannot be used if the graphs have edge labels as of TF 2.4.
Parameters:
-
model
(MGModel
) –The model to trace.
-
api
(Literal['call', 'predict', 'predict_on_batch']
) –The TensorFlow API intended to be used with the model. Options are
call
for themodel()
API,predict
for themodel.predict()
API andpredict_on_batch
for themodel.predict_on_batch()
API.
Returns:
-
Tuple[MGModel | Callable, float]
–The model and the elapsed time in seconds for tracing.
Raises:
-
ValueError
–The
predict_on_batch
API has been selected and the compiler's configuration is set for graphs with edge labels.
Source code in libmg/compiler/compiler.py
make_uoperator ¶
Returns a unary operator psi function.
A unary operator is equivalent to a PsiLocal
.
Parameters:
-
op
(Callable[[Tensor], Tensor]
) –The unary operator function. The function must be compatible with TensorFlow's broadcasting rules. The function is expected to take in input a tensor X with shape
(n_nodes, n_node_features)
, containing the labels of every node in the graph, and return a tensor of shape(n_nodes, n_new_node_features)
, containing the transformed labels. -
name
(str | None
, default:None
) –The name of the unary operator.
Returns:
-
type
–A subclass of Operator that implements the operator.
Source code in libmg/compiler/functions.py
make_boperator ¶
Returns a binary operator psi function.
A binary operator is a binary local transformation of node labels, psi: (T, T) -> U.
Parameters:
-
op
(Callable[[Tensor, Tensor], Tensor]
) –The binary operator function. The function must be compatible with TensorFlow's broadcasting rules. The function is expected to take in input two tensors X1, X2 with shape
(n_nodes, n_node_features/2)
. The first tensor contains for every node the first half of the node features, while the second tensor contains the second half. The operator returns a tensor of shape(n_nodes, n_new_node_features)
, containing the transformed labels. -
name
(str | None
, default:None
) –The name of the binary operator.
Returns:
-
type
–A subclass of Operator that implements the operator.
Source code in libmg/compiler/functions.py
make_koperator ¶
Returns a k-ary operator psi function.
A k-ary operator is a k-ary local transformation of node labels, psi: (T^k) -> U.
Parameters:
-
op
(Callable[..., Tensor]
) –The k-ary operator function. The function must be compatible with TensorFlow's broadcasting rules. The function is expected to take in input k tensors X1, X2, ..., Xk with shape
(n_nodes, n_node_features/k)
. The first tensor contains for every node the first k of the node features, the second tensor contains the next k features, and so on. The operator returns a tensor of shape(n_nodes, n_new_node_features)
, containing the transformed labels. -
name
(str | None
, default:None
) –The name of the k-ary operator.
Returns:
-
type
–A subclass of Operator that implements the operator.