metatensor-learn 0.2.1__tar.gz → 0.2.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {metatensor-learn-0.2.1/metatensor_learn.egg-info → metatensor_learn-0.2.2}/PKG-INFO +4 -4
- metatensor_learn-0.2.2/metatensor/learn/nn/__init__.py +8 -0
- metatensor_learn-0.2.2/metatensor/learn/nn/_utils.py +53 -0
- metatensor_learn-0.2.2/metatensor/learn/nn/layer_norm.py +349 -0
- metatensor_learn-0.2.2/metatensor/learn/nn/linear.py +181 -0
- {metatensor-learn-0.2.1 → metatensor_learn-0.2.2}/metatensor/learn/nn/module_map.py +6 -0
- metatensor_learn-0.2.2/metatensor/learn/nn/relu.py +114 -0
- metatensor_learn-0.2.2/metatensor/learn/nn/sequential.py +39 -0
- metatensor_learn-0.2.2/metatensor/learn/nn/silu.py +114 -0
- metatensor_learn-0.2.2/metatensor/learn/nn/tanh.py +108 -0
- {metatensor-learn-0.2.1 → metatensor_learn-0.2.2/metatensor_learn.egg-info}/PKG-INFO +4 -4
- {metatensor-learn-0.2.1 → metatensor_learn-0.2.2}/metatensor_learn.egg-info/SOURCES.txt +6 -0
- {metatensor-learn-0.2.1 → metatensor_learn-0.2.2}/pyproject.toml +3 -3
- {metatensor-learn-0.2.1 → metatensor_learn-0.2.2}/setup.py +1 -1
- metatensor-learn-0.2.1/metatensor/learn/nn/__init__.py +0 -5
- metatensor-learn-0.2.1/metatensor/learn/nn/linear.py +0 -138
- {metatensor-learn-0.2.1 → metatensor_learn-0.2.2}/AUTHORS +0 -0
- {metatensor-learn-0.2.1 → metatensor_learn-0.2.2}/LICENSE +0 -0
- {metatensor-learn-0.2.1 → metatensor_learn-0.2.2}/MANIFEST.in +0 -0
- {metatensor-learn-0.2.1 → metatensor_learn-0.2.2}/README.rst +0 -0
- {metatensor-learn-0.2.1 → metatensor_learn-0.2.2}/metatensor/learn/__init__.py +0 -0
- {metatensor-learn-0.2.1 → metatensor_learn-0.2.2}/metatensor/learn/_backend.py +0 -0
- {metatensor-learn-0.2.1 → metatensor_learn-0.2.2}/metatensor/learn/data/__init__.py +0 -0
- {metatensor-learn-0.2.1 → metatensor_learn-0.2.2}/metatensor/learn/data/collate.py +0 -0
- {metatensor-learn-0.2.1 → metatensor_learn-0.2.2}/metatensor/learn/data/dataloader.py +0 -0
- {metatensor-learn-0.2.1 → metatensor_learn-0.2.2}/metatensor/learn/data/dataset.py +0 -0
- {metatensor-learn-0.2.1 → metatensor_learn-0.2.2}/metatensor_learn.egg-info/dependency_links.txt +0 -0
- {metatensor-learn-0.2.1 → metatensor_learn-0.2.2}/metatensor_learn.egg-info/requires.txt +0 -0
- {metatensor-learn-0.2.1 → metatensor_learn-0.2.2}/metatensor_learn.egg-info/top_level.txt +0 -0
- {metatensor-learn-0.2.1 → metatensor_learn-0.2.2}/metatensor_learn.egg-info/zip-safe +0 -0
- {metatensor-learn-0.2.1 → metatensor_learn-0.2.2}/n_commits_since_last_tag +0 -0
- {metatensor-learn-0.2.1 → metatensor_learn-0.2.2}/setup.cfg +0 -0
- {metatensor-learn-0.2.1 → metatensor_learn-0.2.2}/tests/test_dummmy.py +0 -0
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: metatensor-learn
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.2
|
|
4
4
|
Summary: Building blocks for the atomistic machine learning models based on PyTorch and NumPy
|
|
5
5
|
Author: Guillaume Fraux, Davide Tisi, Philip Loche, Joseph W. Abbott, Jigyasa Nigam, Chiheb Ben Mahmoud
|
|
6
6
|
License: BSD-3-Clause
|
|
7
|
-
Project-URL: homepage, https://
|
|
8
|
-
Project-URL: documentation, https://
|
|
7
|
+
Project-URL: homepage, https://docs.metatensor.org/latest/
|
|
8
|
+
Project-URL: documentation, https://docs.metatensor.org/latest/
|
|
9
9
|
Project-URL: repository, https://github.com/lab-cosmo/metatensor
|
|
10
|
-
Project-URL: changelog, https://
|
|
10
|
+
Project-URL: changelog, https://docs.metatensor.org/latest/learn/CHANGELOG.html
|
|
11
11
|
Keywords: machine learning,molecular modeling
|
|
12
12
|
Classifier: Development Status :: 4 - Beta
|
|
13
13
|
Classifier: Intended Audience :: Science/Research
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
from .module_map import ModuleMap # noqa: F401
|
|
2
|
+
|
|
3
|
+
from .layer_norm import InvariantLayerNorm, LayerNorm # noqa: F401
|
|
4
|
+
from .linear import EquivariantLinear, Linear # noqa: F401
|
|
5
|
+
from .relu import InvariantReLU, ReLU # noqa: F401
|
|
6
|
+
from .sequential import Sequential # noqa: F401
|
|
7
|
+
from .silu import InvariantSiLU, SiLU # noqa: F401
|
|
8
|
+
from .tanh import InvariantTanh, Tanh # noqa: F401
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
from typing import List
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def _check_module_map_parameter(
|
|
5
|
+
parameter, name: str, types: type, num_keys: int, keys_param_name: str
|
|
6
|
+
) -> List:
|
|
7
|
+
"""
|
|
8
|
+
A helper function that checks the type of an input parameter passed to classes that
|
|
9
|
+
are children of :py:class:`ModuleMap`, and raises the appropriate errors.
|
|
10
|
+
|
|
11
|
+
Raises a TypeError if not one of the valid `types`. Raises a ValueError if passed as
|
|
12
|
+
a list and not the same length as `num_keys`.
|
|
13
|
+
|
|
14
|
+
:param parameter: the parameter value to check
|
|
15
|
+
:param name: str, the name of the parameter used in the ModuleMap child class
|
|
16
|
+
:param types: the type that is valid for the `parameter`
|
|
17
|
+
:param num_keys: int, the number of relevant keys in the ModuleMap child class for
|
|
18
|
+
which the parameter is used. Used to check the length of `parameter` if passed
|
|
19
|
+
as a list.
|
|
20
|
+
:param keys_param_name: str, the name of the relevant keys parameter for the
|
|
21
|
+
ModuleMap child class. Used to raise a ValueError if the length of `parameter`
|
|
22
|
+
does not match the length of the keys.
|
|
23
|
+
|
|
24
|
+
:return: `parameter` as a list, one for each key.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
# Convert to list if not already
|
|
28
|
+
if isinstance(parameter, types):
|
|
29
|
+
parameter = [parameter] * num_keys
|
|
30
|
+
|
|
31
|
+
# Check if it is a list
|
|
32
|
+
if isinstance(parameter, list):
|
|
33
|
+
if len(parameter) != num_keys:
|
|
34
|
+
raise ValueError(
|
|
35
|
+
f"`{name}` must have same length as `{keys_param_name}`,"
|
|
36
|
+
f" but len({name}) != len({keys_param_name})"
|
|
37
|
+
f" [{len(parameter)} != {num_keys}]"
|
|
38
|
+
)
|
|
39
|
+
# Check each element
|
|
40
|
+
for p in parameter:
|
|
41
|
+
if not isinstance(p, types):
|
|
42
|
+
raise TypeError(
|
|
43
|
+
f"`{name}` must be of type {types} or List of {types}, but not"
|
|
44
|
+
f" {type(p)}."
|
|
45
|
+
)
|
|
46
|
+
# Raise if not a list
|
|
47
|
+
else:
|
|
48
|
+
raise TypeError(
|
|
49
|
+
f"`{name}` must be type {types} or List of {types},"
|
|
50
|
+
f" but not {type(parameter)}."
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
return parameter
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Module containing classes :class:`LayerNorm` and :class:`InvariantLayerNorm`, i.e.
|
|
3
|
+
module maps that apply layer norms in a generic and equivariant way,
|
|
4
|
+
respectively.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from typing import List, Optional, Union
|
|
8
|
+
|
|
9
|
+
import torch
|
|
10
|
+
from torch.nn import Module, init
|
|
11
|
+
from torch.nn.parameter import Parameter
|
|
12
|
+
|
|
13
|
+
from .._backend import Labels, TensorMap
|
|
14
|
+
from ._utils import _check_module_map_parameter
|
|
15
|
+
from .module_map import ModuleMap
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class LayerNorm(Module):
|
|
19
|
+
"""
|
|
20
|
+
Module similar to :py:class:`torch.nn.LayerNorm` that works with
|
|
21
|
+
:py:class:`metatensor.torch.TensorMap` objects.
|
|
22
|
+
|
|
23
|
+
Applies a layer normalization to each block of a :py:class:`TensorMap` passed to its
|
|
24
|
+
:py:meth:`forward` method, indexed by :param in_keys:.
|
|
25
|
+
|
|
26
|
+
The main difference from :py:class:`torch.nn.LayerNorm` is that there is no
|
|
27
|
+
`normalized_shape` parameter. Instead, the standard deviation and mean (if
|
|
28
|
+
applicable) are calculated over the property dimension of each
|
|
29
|
+
:py:class:`TensorBlock`.
|
|
30
|
+
|
|
31
|
+
This module also has an extra parameter :param mean: that controls whether or not
|
|
32
|
+
the mean over the properties dimension is subtracted from the input tensor in the
|
|
33
|
+
transformation.
|
|
34
|
+
|
|
35
|
+
Refer to the :py:class`torch.nn.LayerNorm` documentation for a more detailed
|
|
36
|
+
description of the other parameters.
|
|
37
|
+
|
|
38
|
+
Each parameter can be passed as a single value of its expected type, which is used
|
|
39
|
+
as the parameter for all blocks. Alternatively, they can be passed as a list to
|
|
40
|
+
control the parameters applied to each block indexed by the keys in :param in_keys:.
|
|
41
|
+
|
|
42
|
+
:param in_keys: :py:class:`Labels`, the keys that are assumed to be in the input
|
|
43
|
+
tensor map in the :py:meth:`forward` method.
|
|
44
|
+
:param in_features: list of int, the number of features in the input tensor for each
|
|
45
|
+
block indexed by the keys in :param in_keys:. If passed as a single value, the
|
|
46
|
+
same number of features is assumed for all blocks.
|
|
47
|
+
:param out_properties: list of :py:class`Labels` (optional), the properties labels
|
|
48
|
+
of the output. By default (if none) the output properties are relabeled using
|
|
49
|
+
Labels.range.
|
|
50
|
+
:mean bool: whether or not to subtract the mean over the properties (final)
|
|
51
|
+
dimension of each block of the input passed to :py:meth:`forward`.
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
def __init__(
|
|
55
|
+
self,
|
|
56
|
+
in_keys: Labels,
|
|
57
|
+
in_features: List[int],
|
|
58
|
+
out_properties: Optional[List[Labels]] = None,
|
|
59
|
+
*,
|
|
60
|
+
eps: Union[float, List[float]] = 1e-5,
|
|
61
|
+
elementwise_affine: Union[bool, List[bool]] = True,
|
|
62
|
+
bias: Union[bool, List[bool]] = True,
|
|
63
|
+
mean: Union[bool, List[bool]] = True,
|
|
64
|
+
device: Optional[torch.device] = None,
|
|
65
|
+
dtype: Optional[torch.dtype] = None,
|
|
66
|
+
) -> None:
|
|
67
|
+
super().__init__()
|
|
68
|
+
# Check input parameters, convert to lists (for each key) if necessary
|
|
69
|
+
in_features = _check_module_map_parameter(
|
|
70
|
+
in_features, "in_features", int, len(in_keys), "in_keys"
|
|
71
|
+
)
|
|
72
|
+
eps = _check_module_map_parameter(eps, "eps", float, len(in_keys), "in_keys")
|
|
73
|
+
elementwise_affine = _check_module_map_parameter(
|
|
74
|
+
elementwise_affine, "elementwise_affine", bool, len(in_keys), "in_keys"
|
|
75
|
+
)
|
|
76
|
+
bias = _check_module_map_parameter(bias, "bias", bool, len(in_keys), "in_keys")
|
|
77
|
+
mean = _check_module_map_parameter(mean, "mean", bool, len(in_keys), "in_keys")
|
|
78
|
+
|
|
79
|
+
# Build module list
|
|
80
|
+
modules: List[Module] = []
|
|
81
|
+
for i in range(len(in_keys)):
|
|
82
|
+
module = _LayerNorm(
|
|
83
|
+
in_features=in_features[i],
|
|
84
|
+
eps=eps[i],
|
|
85
|
+
elementwise_affine=elementwise_affine[i],
|
|
86
|
+
bias=bias[i],
|
|
87
|
+
mean=mean[i],
|
|
88
|
+
device=device,
|
|
89
|
+
dtype=dtype,
|
|
90
|
+
)
|
|
91
|
+
modules.append(module)
|
|
92
|
+
self.module_map = ModuleMap(in_keys, modules, out_properties)
|
|
93
|
+
|
|
94
|
+
def forward(self, tensor: TensorMap) -> TensorMap:
|
|
95
|
+
"""
|
|
96
|
+
Apply the transformation to the input tensor map `tensor`.
|
|
97
|
+
|
|
98
|
+
:param tensor: :py:class:`TensorMap` with the input tensor to be transformed.
|
|
99
|
+
:return: :py:class:`TensorMap`
|
|
100
|
+
"""
|
|
101
|
+
# Currently not supporting gradients
|
|
102
|
+
if len(tensor[0].gradients_list()) != 0:
|
|
103
|
+
raise ValueError(
|
|
104
|
+
"Gradients not supported. Please use metatensor.remove_gradients()"
|
|
105
|
+
" before using this module"
|
|
106
|
+
)
|
|
107
|
+
return self.module_map(tensor)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class InvariantLayerNorm(Module):
|
|
111
|
+
"""
|
|
112
|
+
Module similar to :py:class:`torch.nn.LayerNorm` that works with
|
|
113
|
+
:py:class:`metatensor.torch.TensorMap` objects, applying the transformation only to
|
|
114
|
+
the invariant blocks.
|
|
115
|
+
|
|
116
|
+
Applies a layer normalization to each invariant block of a :py:class:`TensorMap`
|
|
117
|
+
passed to :py:meth:`forward` method. These correspond to the blocks indexed by
|
|
118
|
+
:param in_keys: at numeric positions passed in :param invariant_key_idxs:.
|
|
119
|
+
|
|
120
|
+
The main difference from :py:class:`torch.nn.LayerNorm` is that there is no
|
|
121
|
+
`normalized_shape` parameter. Instead, the standard deviation and mean (if
|
|
122
|
+
applicable) are calculated over the property dimension of each
|
|
123
|
+
:py:class:`TensorBlock`.
|
|
124
|
+
|
|
125
|
+
This module also has an extra parameter :param mean: that controls whether or not
|
|
126
|
+
the mean over the properties dimension is subtracted from the input tensor in the
|
|
127
|
+
transformation.
|
|
128
|
+
|
|
129
|
+
Refer to the :py:class`torch.nn.LayerNorm` documentation for a more detailed
|
|
130
|
+
description of the other parameters.
|
|
131
|
+
|
|
132
|
+
Each parameter can be passed as a single value of its expected type, which is used
|
|
133
|
+
as the parameter for all blocks. Alternatively, they can be passed as a list to
|
|
134
|
+
control the parameters applied to each block indexed by the invariant keys in :param
|
|
135
|
+
in_keys: at numeric poositions :param invariant_key_idxs:.
|
|
136
|
+
|
|
137
|
+
:param in_keys: :py:class:`Labels`, the keys that are assumed to be in the input
|
|
138
|
+
tensor map in the :py:meth:`forward` method.
|
|
139
|
+
:param invariant_key_idxs: list of int, the indices of the invariant keys present in
|
|
140
|
+
`in_keys` in the input :py:class:`TensorMap`. Only blocks for these keys will
|
|
141
|
+
have layer norm applied. The other blocks will have the identity operator
|
|
142
|
+
applied.
|
|
143
|
+
:param in_features: list of int, the number of features in the input tensor for each
|
|
144
|
+
block indexed by the keys in :param in_keys:. If passed as a single value, the
|
|
145
|
+
same number of features is assumed for all blocks.
|
|
146
|
+
:param out_properties: list of :py:class`Labels` (optional), the properties labels
|
|
147
|
+
of the output. By default (if none) the output properties are relabeled using
|
|
148
|
+
Labels.range.
|
|
149
|
+
:param mean: bool or list of bool. Whether or not to subtract the mean over the
|
|
150
|
+
properties (final) dimension of each invariant block of the input passed to
|
|
151
|
+
:py:meth:`forward`. If passed as a list, must have the same length as :param
|
|
152
|
+
invariant_key_idxs:.
|
|
153
|
+
"""
|
|
154
|
+
|
|
155
|
+
def __init__(
|
|
156
|
+
self,
|
|
157
|
+
in_keys: Labels,
|
|
158
|
+
invariant_key_idxs: List[int],
|
|
159
|
+
in_features: List[int],
|
|
160
|
+
out_properties: Optional[List[Labels]] = None,
|
|
161
|
+
*,
|
|
162
|
+
eps: Union[float, List[float]] = 1e-5,
|
|
163
|
+
elementwise_affine: Union[bool, List[bool]] = True,
|
|
164
|
+
bias: Union[bool, List[bool]] = True,
|
|
165
|
+
mean: Union[bool, List[bool]] = True,
|
|
166
|
+
device: Optional[torch.device] = None,
|
|
167
|
+
dtype: Optional[torch.dtype] = None,
|
|
168
|
+
) -> None:
|
|
169
|
+
super().__init__()
|
|
170
|
+
# Check input parameters, convert to lists (for each *invariant* key) if
|
|
171
|
+
# necessary.
|
|
172
|
+
in_features = _check_module_map_parameter(
|
|
173
|
+
in_features,
|
|
174
|
+
"in_features",
|
|
175
|
+
int,
|
|
176
|
+
len(invariant_key_idxs),
|
|
177
|
+
"invariant_key_idxs",
|
|
178
|
+
)
|
|
179
|
+
eps = _check_module_map_parameter(
|
|
180
|
+
eps, "eps", float, len(invariant_key_idxs), "invariant_key_idxs"
|
|
181
|
+
)
|
|
182
|
+
elementwise_affine = _check_module_map_parameter(
|
|
183
|
+
elementwise_affine,
|
|
184
|
+
"elementwise_affine",
|
|
185
|
+
bool,
|
|
186
|
+
len(invariant_key_idxs),
|
|
187
|
+
"invariant_key_idxs",
|
|
188
|
+
)
|
|
189
|
+
bias = _check_module_map_parameter(
|
|
190
|
+
bias, "bias", bool, len(invariant_key_idxs), "invariant_key_idxs"
|
|
191
|
+
)
|
|
192
|
+
mean = _check_module_map_parameter(
|
|
193
|
+
mean, "mean", bool, len(invariant_key_idxs), "invariant_key_idxs"
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
# Build module list
|
|
197
|
+
modules: List[Module] = []
|
|
198
|
+
invariant_idx: int = 0
|
|
199
|
+
for i in range(len(in_keys)):
|
|
200
|
+
if i in invariant_key_idxs: # Invariant block: apply LayerNorm
|
|
201
|
+
for j in range(len(invariant_key_idxs)):
|
|
202
|
+
if invariant_key_idxs[j] == i:
|
|
203
|
+
invariant_idx = j
|
|
204
|
+
|
|
205
|
+
module = _LayerNorm(
|
|
206
|
+
in_features=in_features[invariant_idx],
|
|
207
|
+
eps=eps[invariant_idx],
|
|
208
|
+
elementwise_affine=elementwise_affine[invariant_idx],
|
|
209
|
+
bias=bias[invariant_idx],
|
|
210
|
+
mean=mean[invariant_idx],
|
|
211
|
+
device=device,
|
|
212
|
+
dtype=dtype,
|
|
213
|
+
)
|
|
214
|
+
else: # Covariant block: apply the identity operator
|
|
215
|
+
module = torch.nn.Identity()
|
|
216
|
+
modules.append(module)
|
|
217
|
+
self.module_map = ModuleMap(in_keys, modules, out_properties)
|
|
218
|
+
|
|
219
|
+
def forward(self, tensor: TensorMap) -> TensorMap:
|
|
220
|
+
"""
|
|
221
|
+
Apply the layer norm to the input tensor map `tensor`.
|
|
222
|
+
|
|
223
|
+
:param tensor: :py:class:`TensorMap` with the input tensor to be transformed.
|
|
224
|
+
:return: :py:class:`TensorMap`
|
|
225
|
+
"""
|
|
226
|
+
# Currently not supporting gradients
|
|
227
|
+
if len(tensor[0].gradients_list()) != 0:
|
|
228
|
+
raise ValueError(
|
|
229
|
+
"Gradients not supported. Please use metatensor.remove_gradients()"
|
|
230
|
+
" before using this module"
|
|
231
|
+
)
|
|
232
|
+
return self.module_map(tensor)
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
class _LayerNorm(Module):
|
|
236
|
+
"""
|
|
237
|
+
Custom :py:class:`Module` re-implementing :py:class:`torch.nn.LayerNorm`.
|
|
238
|
+
|
|
239
|
+
In this case, `normalized_shape` is not provided as a parameter. Instead, the
|
|
240
|
+
standard deviation and mean (if applicable) are calculated over the final dimension
|
|
241
|
+
of the input tensor.
|
|
242
|
+
|
|
243
|
+
Subtraction of this mean can be switched on or off using the extra :param mean:
|
|
244
|
+
parameter.
|
|
245
|
+
|
|
246
|
+
Refer to :py:class:`torch.nn.LayerNorm` documentation for more information on the
|
|
247
|
+
other parameters.
|
|
248
|
+
|
|
249
|
+
:param mean: bool, whether or not to subtract the mean over the final dimension of
|
|
250
|
+
the input tensor passed to :py:meth:`forward`.
|
|
251
|
+
"""
|
|
252
|
+
|
|
253
|
+
__constants__ = ["in_features", "eps", "elementwise_affine"]
|
|
254
|
+
eps: float
|
|
255
|
+
elementwise_affine: bool
|
|
256
|
+
|
|
257
|
+
def __init__(
|
|
258
|
+
self,
|
|
259
|
+
in_features: int,
|
|
260
|
+
eps: float = 1e-5,
|
|
261
|
+
elementwise_affine: bool = True,
|
|
262
|
+
bias: bool = True,
|
|
263
|
+
device: Optional[torch.device] = None,
|
|
264
|
+
dtype: Optional[torch.dtype] = None,
|
|
265
|
+
mean: bool = True,
|
|
266
|
+
) -> None:
|
|
267
|
+
|
|
268
|
+
super().__init__()
|
|
269
|
+
|
|
270
|
+
self.in_features = in_features
|
|
271
|
+
self.eps = eps
|
|
272
|
+
self.elementwise_affine = elementwise_affine
|
|
273
|
+
self.mean = mean
|
|
274
|
+
if self.elementwise_affine:
|
|
275
|
+
self.weight = Parameter(
|
|
276
|
+
torch.empty(in_features, device=device, dtype=dtype)
|
|
277
|
+
)
|
|
278
|
+
if bias:
|
|
279
|
+
self.bias = Parameter(
|
|
280
|
+
torch.empty(in_features, device=device, dtype=dtype)
|
|
281
|
+
)
|
|
282
|
+
else:
|
|
283
|
+
self.register_parameter("bias", None)
|
|
284
|
+
else:
|
|
285
|
+
self.register_parameter("weight", None)
|
|
286
|
+
self.register_parameter("bias", None)
|
|
287
|
+
|
|
288
|
+
self.reset_parameters()
|
|
289
|
+
|
|
290
|
+
def reset_parameters(self) -> None:
|
|
291
|
+
if self.elementwise_affine:
|
|
292
|
+
init.ones_(self.weight)
|
|
293
|
+
if self.bias is not None:
|
|
294
|
+
init.zeros_(self.bias)
|
|
295
|
+
|
|
296
|
+
def forward(self, input: torch.Tensor) -> torch.Tensor:
|
|
297
|
+
return _layer_norm(
|
|
298
|
+
input, weight=self.weight, bias=self.bias, eps=self.eps, mean=self.mean
|
|
299
|
+
)
|
|
300
|
+
|
|
301
|
+
def extra_repr(self) -> str:
|
|
302
|
+
return "eps={eps}, elementwise_affine={elementwise_affine}".format(
|
|
303
|
+
**self.__dict__
|
|
304
|
+
)
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def _layer_norm(
|
|
308
|
+
tensor: torch.Tensor,
|
|
309
|
+
weight: Optional[torch.Tensor],
|
|
310
|
+
bias: Optional[torch.Tensor],
|
|
311
|
+
eps: float,
|
|
312
|
+
mean: bool,
|
|
313
|
+
) -> torch.Tensor:
|
|
314
|
+
"""
|
|
315
|
+
Apply layer normalization to the input `tensor`.
|
|
316
|
+
|
|
317
|
+
See :py:class:`torch.nn.functional.layer_norm` for more information on the
|
|
318
|
+
other parameters.
|
|
319
|
+
|
|
320
|
+
In addition to base torch implementation, this function has the added control of
|
|
321
|
+
whether or not the mean over the properties (final) dimension is subtracted from the
|
|
322
|
+
input tensor.
|
|
323
|
+
|
|
324
|
+
:param mean: whether or not to subtract from the input :param tensor: the mean over
|
|
325
|
+
the dimensions not included in :param normalized_shape:.
|
|
326
|
+
|
|
327
|
+
:return: :py:class:`torch.Tensor` with layer normalization applied.
|
|
328
|
+
"""
|
|
329
|
+
# Contract over all dimensions except properties
|
|
330
|
+
dim: List[int] = list(range(0, len(tensor.shape) - 1))
|
|
331
|
+
|
|
332
|
+
if mean: # subtract mean over properties dimension
|
|
333
|
+
tensor_out = tensor - torch.mean(tensor, dim=dim)
|
|
334
|
+
else:
|
|
335
|
+
tensor_out = tensor
|
|
336
|
+
|
|
337
|
+
# Divide by standard deviation over properties dimension. `correction=0` for biased
|
|
338
|
+
# estimator, in accordance with the torch implementation.
|
|
339
|
+
tensor_out /= torch.sqrt(
|
|
340
|
+
torch.var(tensor, dim=dim, correction=0, keepdim=True) + eps
|
|
341
|
+
)
|
|
342
|
+
|
|
343
|
+
if weight is not None: # apply affine transformation
|
|
344
|
+
tensor_out *= weight
|
|
345
|
+
|
|
346
|
+
if bias is not None: # apply bias
|
|
347
|
+
tensor_out += bias
|
|
348
|
+
|
|
349
|
+
return tensor_out
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
from typing import List, Optional, Union
|
|
2
|
+
|
|
3
|
+
import torch
|
|
4
|
+
from torch.nn import Module
|
|
5
|
+
|
|
6
|
+
from .._backend import Labels, TensorMap
|
|
7
|
+
from ._utils import _check_module_map_parameter
|
|
8
|
+
from .module_map import ModuleMap
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class Linear(Module):
|
|
12
|
+
"""
|
|
13
|
+
Module similar to :py:class:`torch.nn.Linear` that works with
|
|
14
|
+
:py:class:`metatensor.torch.TensorMap`.
|
|
15
|
+
|
|
16
|
+
Applies a linear transformation to each block of a :py:class:`TensorMap` passed to
|
|
17
|
+
its forward method, indexed by :param in_keys:.
|
|
18
|
+
|
|
19
|
+
Refer to the :py:class`torch.nn.Linear` documentation for a more detailed
|
|
20
|
+
description of the other parameters.
|
|
21
|
+
|
|
22
|
+
Each parameter can be passed as a single value of its expected type, which is used
|
|
23
|
+
as the parameter for all blocks. Alternatively, they can be passed as a list to
|
|
24
|
+
control the parameters applied to each block indexed by the keys in :param in_keys:.
|
|
25
|
+
|
|
26
|
+
:param in_keys: :py:class:`Labels`, the keys that are assumed to be in the input
|
|
27
|
+
tensor map in the :py:meth:`forward` method.
|
|
28
|
+
:param out_properties: list of :py:class`Labels` (optional), the properties labels
|
|
29
|
+
of the output. By default the output properties are relabeled using
|
|
30
|
+
Labels.range. If provided, :param out_features: can be inferred and need not be
|
|
31
|
+
provided.
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
def __init__(
|
|
35
|
+
self,
|
|
36
|
+
in_keys: Labels,
|
|
37
|
+
in_features: Union[int, List[int]],
|
|
38
|
+
out_features: Optional[Union[int, List[int]]] = None,
|
|
39
|
+
out_properties: Optional[List[Labels]] = None,
|
|
40
|
+
*,
|
|
41
|
+
bias: Union[bool, List[bool]] = True,
|
|
42
|
+
device: Optional[torch.device] = None,
|
|
43
|
+
dtype: Optional[torch.dtype] = None,
|
|
44
|
+
) -> None:
|
|
45
|
+
super().__init__()
|
|
46
|
+
# Infer `out_features` if not provided
|
|
47
|
+
if out_features is None:
|
|
48
|
+
if out_properties is None:
|
|
49
|
+
raise ValueError(
|
|
50
|
+
"If `out_features` is not provided,"
|
|
51
|
+
" `out_properties` must be provided."
|
|
52
|
+
)
|
|
53
|
+
out_features = [len(p) for p in out_properties]
|
|
54
|
+
|
|
55
|
+
# Check input parameters, convert to lists (for each key) if necessary
|
|
56
|
+
in_features = _check_module_map_parameter(
|
|
57
|
+
in_features, "in_features", int, len(in_keys), "in_keys"
|
|
58
|
+
)
|
|
59
|
+
out_features = _check_module_map_parameter(
|
|
60
|
+
out_features, "out_features", int, len(in_keys), "in_keys"
|
|
61
|
+
)
|
|
62
|
+
bias = _check_module_map_parameter(bias, "bias", bool, len(in_keys), "in_keys")
|
|
63
|
+
|
|
64
|
+
modules: List[Module] = []
|
|
65
|
+
for i in range(len(in_keys)):
|
|
66
|
+
module = torch.nn.Linear(
|
|
67
|
+
in_features=in_features[i],
|
|
68
|
+
out_features=out_features[i],
|
|
69
|
+
bias=bias[i],
|
|
70
|
+
device=device,
|
|
71
|
+
dtype=dtype,
|
|
72
|
+
)
|
|
73
|
+
modules.append(module)
|
|
74
|
+
self.module_map = ModuleMap(in_keys, modules, out_properties)
|
|
75
|
+
|
|
76
|
+
def forward(self, tensor: TensorMap) -> TensorMap:
|
|
77
|
+
"""
|
|
78
|
+
Apply the transformation to the input tensor map `tensor`.
|
|
79
|
+
|
|
80
|
+
:param tensor: :py:class:`TensorMap` with the input tensor to be transformed.
|
|
81
|
+
:return: :py:class:`TensorMap`
|
|
82
|
+
"""
|
|
83
|
+
return self.module_map(tensor)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class EquivariantLinear(Module):
|
|
87
|
+
"""
|
|
88
|
+
Module similar to :py:class:`torch.nn.Linear` that works with equivariant
|
|
89
|
+
:py:class:`metatensor.torch.TensorMap` objects.
|
|
90
|
+
|
|
91
|
+
Applies a linear transformation to each block of a :py:class:`TensorMap` passed to
|
|
92
|
+
its forward method, indexed by :param in_keys:.
|
|
93
|
+
|
|
94
|
+
Refer to the :py:class`torch.nn.Linear` documentation for a more detailed
|
|
95
|
+
description of the other parameters.
|
|
96
|
+
|
|
97
|
+
Each parameter can be passed as a single value of its expected type, which is used
|
|
98
|
+
as the parameter for all blocks. Alternatively, they can be passed as a list to
|
|
99
|
+
control the parameters applied to each block indexed by the keys in :param in_keys:.
|
|
100
|
+
|
|
101
|
+
For :py:class:`EquivariantLinear`, by contrast to :py:class:`Linear`, the parameter
|
|
102
|
+
:param bias: is only applied to modules corresponding to invariant blocks, i.e.
|
|
103
|
+
those indexed by keys in :param in_keys` at numeric indices :param
|
|
104
|
+
invariant_key_idxs:. If passed as a list, :param bias: must therefore have the same
|
|
105
|
+
length as :param invariant_key_idxs:.
|
|
106
|
+
|
|
107
|
+
:param in_keys: :py:class:`Labels`, the keys that are assumed to be in the input
|
|
108
|
+
tensor map in the :py:meth:`forward` method.
|
|
109
|
+
:param invariant_key_idxs: list of int, the indices of the invariant keys present in
|
|
110
|
+
`in_keys` in the input :py:class:`TensorMap`. Only blocks for these keys will
|
|
111
|
+
have bias applied according to the user choice. Covariant blocks will not have
|
|
112
|
+
bias applied.
|
|
113
|
+
:param out_properties: list of :py:class`Labels` (optional), the properties labels
|
|
114
|
+
of the output. By default the output properties are relabeled using
|
|
115
|
+
Labels.range. If provided, :param out_features: can be inferred and need not be
|
|
116
|
+
provided.
|
|
117
|
+
"""
|
|
118
|
+
|
|
119
|
+
def __init__(
|
|
120
|
+
self,
|
|
121
|
+
in_keys: Labels,
|
|
122
|
+
invariant_key_idxs: List[int],
|
|
123
|
+
in_features: Union[int, List[int]],
|
|
124
|
+
out_features: Optional[Union[int, List[int]]] = None,
|
|
125
|
+
out_properties: Optional[List[Labels]] = None,
|
|
126
|
+
*,
|
|
127
|
+
bias: Union[bool, List[bool]] = True,
|
|
128
|
+
device: Optional[torch.device] = None,
|
|
129
|
+
dtype: Optional[torch.dtype] = None,
|
|
130
|
+
) -> None:
|
|
131
|
+
super().__init__()
|
|
132
|
+
# Infer `out_features` if not provided
|
|
133
|
+
if out_features is None:
|
|
134
|
+
if out_properties is None:
|
|
135
|
+
raise ValueError(
|
|
136
|
+
"If `out_features` is not provided,"
|
|
137
|
+
" `out_properties` must be provided."
|
|
138
|
+
)
|
|
139
|
+
out_features = [len(p) for p in out_properties]
|
|
140
|
+
|
|
141
|
+
# Check input parameters, convert to lists (for each key) if necessary
|
|
142
|
+
in_features = _check_module_map_parameter(
|
|
143
|
+
in_features, "in_features", int, len(in_keys), "in_keys"
|
|
144
|
+
)
|
|
145
|
+
out_features = _check_module_map_parameter(
|
|
146
|
+
out_features, "out_features", int, len(in_keys), "in_keys"
|
|
147
|
+
)
|
|
148
|
+
bias = _check_module_map_parameter(
|
|
149
|
+
bias, "bias", bool, len(invariant_key_idxs), "invariant_key_idxs"
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
modules: List[Module] = []
|
|
153
|
+
for i in range(len(in_keys)):
|
|
154
|
+
if (
|
|
155
|
+
i in invariant_key_idxs
|
|
156
|
+
): # Invariant block: apply bias according to user choice
|
|
157
|
+
for j in range(len(invariant_key_idxs)):
|
|
158
|
+
if invariant_key_idxs[j] == i:
|
|
159
|
+
bias_block = bias[j]
|
|
160
|
+
else: # Covariant block: do not apply bias
|
|
161
|
+
bias_block = False
|
|
162
|
+
|
|
163
|
+
module = torch.nn.Linear(
|
|
164
|
+
in_features=in_features[i],
|
|
165
|
+
out_features=out_features[i],
|
|
166
|
+
bias=bias_block,
|
|
167
|
+
device=device,
|
|
168
|
+
dtype=dtype,
|
|
169
|
+
)
|
|
170
|
+
modules.append(module)
|
|
171
|
+
|
|
172
|
+
self.module_map = ModuleMap(in_keys, modules, out_properties)
|
|
173
|
+
|
|
174
|
+
def forward(self, tensor: TensorMap) -> TensorMap:
|
|
175
|
+
"""
|
|
176
|
+
Apply the transformation to the input tensor map `tensor`.
|
|
177
|
+
|
|
178
|
+
:param tensor: :py:class:`TensorMap` with the input tensor to be transformed.
|
|
179
|
+
:return: :py:class:`TensorMap`
|
|
180
|
+
"""
|
|
181
|
+
return self.module_map(tensor)
|
|
@@ -256,6 +256,12 @@ class ModuleMap(ModuleList):
|
|
|
256
256
|
:param tensor:
|
|
257
257
|
input tensor map
|
|
258
258
|
"""
|
|
259
|
+
self._in_keys = self._in_keys.to(tensor.device)
|
|
260
|
+
if self._out_properties is not None:
|
|
261
|
+
self._out_properties = [
|
|
262
|
+
label.to(tensor.device) for label in self._out_properties
|
|
263
|
+
]
|
|
264
|
+
|
|
259
265
|
out_blocks: List[TensorBlock] = []
|
|
260
266
|
for key, block in tensor.items():
|
|
261
267
|
out_block = self.forward_block(key, block)
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
from typing import List, Optional
|
|
2
|
+
|
|
3
|
+
import torch
|
|
4
|
+
from torch.nn import Module
|
|
5
|
+
|
|
6
|
+
from .._backend import Labels, TensorMap
|
|
7
|
+
from .module_map import ModuleMap
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ReLU(Module):
|
|
11
|
+
"""
|
|
12
|
+
Module similar to :py:class:`torch.nn.ReLU` that works with
|
|
13
|
+
:py:class:`metatensor.torch.TensorMap` objects.
|
|
14
|
+
|
|
15
|
+
Applies a rectified linear unit transformation transformation to each block of a
|
|
16
|
+
:py:class:`TensorMap` passed to its forward method, indexed by :param in_keys:.
|
|
17
|
+
|
|
18
|
+
Refer to the :py:class`torch.nn.ReLU` documentation for a more detailed description
|
|
19
|
+
of the parameters.
|
|
20
|
+
|
|
21
|
+
:param in_keys: :py:class:`Labels`, the keys that are assumed to be in the input
|
|
22
|
+
tensor map in the :py:meth:`forward` method.
|
|
23
|
+
:param out_properties: list of :py:class`Labels` (optional), the properties labels
|
|
24
|
+
of the output. By default the output properties are relabeled using
|
|
25
|
+
Labels.range.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
def __init__(
|
|
29
|
+
self,
|
|
30
|
+
in_keys: Labels,
|
|
31
|
+
out_properties: Optional[Labels] = None,
|
|
32
|
+
*,
|
|
33
|
+
in_place: bool = False,
|
|
34
|
+
) -> None:
|
|
35
|
+
super().__init__()
|
|
36
|
+
modules: List[Module] = [torch.nn.ReLU() for i in range(len(in_keys))]
|
|
37
|
+
self.module_map = ModuleMap(in_keys, modules, out_properties)
|
|
38
|
+
|
|
39
|
+
def forward(self, tensor: TensorMap) -> TensorMap:
|
|
40
|
+
"""
|
|
41
|
+
Apply the transformation to the input tensor map `tensor`.
|
|
42
|
+
|
|
43
|
+
Note: currently not supporting gradients.
|
|
44
|
+
|
|
45
|
+
:param tensor: :py:class:`TensorMap` with the input tensor to be transformed.
|
|
46
|
+
:return: :py:class:`TensorMap`
|
|
47
|
+
"""
|
|
48
|
+
# Currently not supporting gradients
|
|
49
|
+
if len(tensor[0].gradients_list()) != 0:
|
|
50
|
+
raise ValueError(
|
|
51
|
+
"Gradients not supported. Please use metatensor.remove_gradients()"
|
|
52
|
+
" before using this module"
|
|
53
|
+
)
|
|
54
|
+
return self.module_map(tensor)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class InvariantReLU(torch.nn.Module):
|
|
58
|
+
"""
|
|
59
|
+
Module similar to :py:class:`torch.nn.ReLU` that works with
|
|
60
|
+
:py:class:`metatensor.torch.TensorMap` objects, applying the transformation only to
|
|
61
|
+
the invariant blocks.
|
|
62
|
+
|
|
63
|
+
Applies a rectified linear unit transformation to each invariant block of a
|
|
64
|
+
:py:class:`TensorMap` passed to its :py:meth:`forward` method. These are indexed by
|
|
65
|
+
the keys in :param in_keys: at numeric indices passed in :param invariant_key_idxs:.
|
|
66
|
+
|
|
67
|
+
Refer to the :py:class`torch.nn.ReLU` documentation for a more detailed description
|
|
68
|
+
of the parameters.
|
|
69
|
+
|
|
70
|
+
:param in_keys: :py:class:`Labels`, the keys that are assumed to be in the input
|
|
71
|
+
tensor map in the :py:meth:`forward` method.
|
|
72
|
+
:param invariant_key_idxs: list of int, the indices of the invariant keys present in
|
|
73
|
+
`in_keys` in the input :py:class:`TensorMap`. Only blocks for these keys will
|
|
74
|
+
have the ReLU transformation applied. Covariant blocks will have the identity
|
|
75
|
+
operator applied.
|
|
76
|
+
:param out_properties: list of :py:class`Labels` (optional), the properties labels
|
|
77
|
+
of the output. By default the output properties are relabeled using
|
|
78
|
+
Labels.range.
|
|
79
|
+
"""
|
|
80
|
+
|
|
81
|
+
def __init__(
|
|
82
|
+
self,
|
|
83
|
+
in_keys: Labels,
|
|
84
|
+
invariant_key_idxs: List[int],
|
|
85
|
+
out_properties: Optional[Labels] = None,
|
|
86
|
+
*,
|
|
87
|
+
in_place: bool = False,
|
|
88
|
+
) -> None:
|
|
89
|
+
super().__init__()
|
|
90
|
+
modules: List[Module] = []
|
|
91
|
+
for i in range(len(in_keys)):
|
|
92
|
+
if i in invariant_key_idxs: # Invariant block: apply ReLU
|
|
93
|
+
module = torch.nn.ReLU()
|
|
94
|
+
else: # Covariant block: apply identity operator
|
|
95
|
+
module = torch.nn.Identity()
|
|
96
|
+
modules.append(module)
|
|
97
|
+
self.module_map: ModuleMap = ModuleMap(in_keys, modules, out_properties)
|
|
98
|
+
|
|
99
|
+
def forward(self, tensor: TensorMap) -> TensorMap:
|
|
100
|
+
"""
|
|
101
|
+
Apply the transformation to the input tensor map `tensor`.
|
|
102
|
+
|
|
103
|
+
Note: currently not supporting gradients.
|
|
104
|
+
|
|
105
|
+
:param tensor: :py:class:`TensorMap` with the input tensor to be transformed.
|
|
106
|
+
:return: :py:class:`TensorMap`
|
|
107
|
+
"""
|
|
108
|
+
# Currently not supporting gradients
|
|
109
|
+
if len(tensor[0].gradients_list()) != 0:
|
|
110
|
+
raise ValueError(
|
|
111
|
+
"Gradients not supported. Please use metatensor.remove_gradients()"
|
|
112
|
+
" before using this module"
|
|
113
|
+
)
|
|
114
|
+
return self.module_map(tensor)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from typing import List
|
|
2
|
+
|
|
3
|
+
import torch
|
|
4
|
+
from torch.nn import Module
|
|
5
|
+
|
|
6
|
+
from .._backend import Labels, TensorMap, check_isinstance
|
|
7
|
+
from .module_map import ModuleMap
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Sequential(Module):
|
|
11
|
+
"""
|
|
12
|
+
A sequential model that applies a list of ModuleMaps to the input in order.
|
|
13
|
+
|
|
14
|
+
:param in_keys:
|
|
15
|
+
The keys that are assumed to be in the input tensor map in the
|
|
16
|
+
:py:meth:`forward` function.
|
|
17
|
+
:param args:
|
|
18
|
+
A list of :py:class:`ModuleMap` objects that will be applied in order to
|
|
19
|
+
the input tensor map in the :py:meth:`forward` function.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
def __init__(self, in_keys: Labels, *args: List[ModuleMap]):
|
|
23
|
+
|
|
24
|
+
super().__init__()
|
|
25
|
+
if not check_isinstance(in_keys, Labels):
|
|
26
|
+
raise TypeError("`in_keys` must be a `Labels` object.")
|
|
27
|
+
|
|
28
|
+
modules: List[Module] = []
|
|
29
|
+
for i in range(len(in_keys)):
|
|
30
|
+
modules.append(torch.nn.Sequential(*[arg.module_map[i] for arg in args]))
|
|
31
|
+
self.module_map = ModuleMap(
|
|
32
|
+
in_keys, modules, out_properties=args[-1].module_map.out_properties
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
def forward(self, tensor: TensorMap) -> TensorMap:
|
|
36
|
+
"""
|
|
37
|
+
Apply the transformations to the input tensor map `tensor`.
|
|
38
|
+
"""
|
|
39
|
+
return self.module_map(tensor)
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
from typing import List, Optional
|
|
2
|
+
|
|
3
|
+
import torch
|
|
4
|
+
from torch.nn import Module
|
|
5
|
+
|
|
6
|
+
from .._backend import Labels, TensorMap
|
|
7
|
+
from .module_map import ModuleMap
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class SiLU(Module):
|
|
11
|
+
"""
|
|
12
|
+
Module similar to :py:class:`torch.nn.SiLU` that works with
|
|
13
|
+
:py:class:`metatensor.torch.TensorMap` objects.
|
|
14
|
+
|
|
15
|
+
Applies a sigmoid linear unit transformation transformation to each block of a
|
|
16
|
+
:py:class:`TensorMap` passed to its forward method, indexed by :param in_keys:.
|
|
17
|
+
|
|
18
|
+
Refer to the :py:class`torch.nn.SiLU` documentation for a more detailed description
|
|
19
|
+
of the parameters.
|
|
20
|
+
|
|
21
|
+
:param in_keys: :py:class:`Labels`, the keys that are assumed to be in the input
|
|
22
|
+
tensor map in the :py:meth:`forward` method.
|
|
23
|
+
:param out_properties: list of :py:class`Labels` (optional), the properties labels
|
|
24
|
+
of the output. By default the output properties are relabeled using
|
|
25
|
+
Labels.range.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
def __init__(
|
|
29
|
+
self,
|
|
30
|
+
in_keys: Labels,
|
|
31
|
+
out_properties: Optional[Labels] = None,
|
|
32
|
+
*,
|
|
33
|
+
in_place: bool = False,
|
|
34
|
+
) -> None:
|
|
35
|
+
super().__init__()
|
|
36
|
+
modules: List[Module] = [torch.nn.SiLU() for i in range(len(in_keys))]
|
|
37
|
+
self.module_map = ModuleMap(in_keys, modules, out_properties)
|
|
38
|
+
|
|
39
|
+
def forward(self, tensor: TensorMap) -> TensorMap:
|
|
40
|
+
"""
|
|
41
|
+
Apply the transformation to the input tensor map `tensor`.
|
|
42
|
+
|
|
43
|
+
Note: currently not supporting gradients.
|
|
44
|
+
|
|
45
|
+
:param tensor: :py:class:`TensorMap` with the input tensor to be transformed.
|
|
46
|
+
:return: :py:class:`TensorMap`
|
|
47
|
+
"""
|
|
48
|
+
# Currently not supporting gradients
|
|
49
|
+
if len(tensor[0].gradients_list()) != 0:
|
|
50
|
+
raise ValueError(
|
|
51
|
+
"Gradients not supported. Please use metatensor.remove_gradients()"
|
|
52
|
+
" before using this module"
|
|
53
|
+
)
|
|
54
|
+
return self.module_map(tensor)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class InvariantSiLU(torch.nn.Module):
|
|
58
|
+
"""
|
|
59
|
+
Module similar to :py:class:`torch.nn.SiLU` that works with
|
|
60
|
+
:py:class:`metatensor.torch.TensorMap` objects, applying the transformation only to
|
|
61
|
+
the invariant blocks.
|
|
62
|
+
|
|
63
|
+
Applies a sigmoid linear unit transformation to each invariant block of a
|
|
64
|
+
:py:class:`TensorMap` passed to its :py:meth:`forward` method. These are indexed by
|
|
65
|
+
the keys in :param in_keys: at numeric indices passed in :param invariant_key_idxs:.
|
|
66
|
+
|
|
67
|
+
Refer to the :py:class`torch.nn.SiLU` documentation for a more detailed description
|
|
68
|
+
of the parameters.
|
|
69
|
+
|
|
70
|
+
:param in_keys: :py:class:`Labels`, the keys that are assumed to be in the input
|
|
71
|
+
tensor map in the :py:meth:`forward` method.
|
|
72
|
+
:param invariant_key_idxs: list of int, the indices of the invariant keys present in
|
|
73
|
+
`in_keys` in the input :py:class:`TensorMap`. Only blocks for these keys will
|
|
74
|
+
have the SiLU transformation applied. Covariant blocks will have the identity
|
|
75
|
+
operator applied.
|
|
76
|
+
:param out_properties: list of :py:class`Labels` (optional), the properties labels
|
|
77
|
+
of the output. By default the output properties are relabeled using
|
|
78
|
+
Labels.range.
|
|
79
|
+
"""
|
|
80
|
+
|
|
81
|
+
def __init__(
|
|
82
|
+
self,
|
|
83
|
+
in_keys: Labels,
|
|
84
|
+
invariant_key_idxs: List[int],
|
|
85
|
+
out_properties: Optional[Labels] = None,
|
|
86
|
+
*,
|
|
87
|
+
in_place: bool = False,
|
|
88
|
+
) -> None:
|
|
89
|
+
super().__init__()
|
|
90
|
+
modules: List[Module] = []
|
|
91
|
+
for i in range(len(in_keys)):
|
|
92
|
+
if i in invariant_key_idxs: # Invariant block: apply SiLU
|
|
93
|
+
module = torch.nn.SiLU()
|
|
94
|
+
else: # Covariant block: apply identity operator
|
|
95
|
+
module = torch.nn.Identity()
|
|
96
|
+
modules.append(module)
|
|
97
|
+
self.module_map: ModuleMap = ModuleMap(in_keys, modules, out_properties)
|
|
98
|
+
|
|
99
|
+
def forward(self, tensor: TensorMap) -> TensorMap:
|
|
100
|
+
"""
|
|
101
|
+
Apply the transformation to the input tensor map `tensor`.
|
|
102
|
+
|
|
103
|
+
Note: currently not supporting gradients.
|
|
104
|
+
|
|
105
|
+
:param tensor: :py:class:`TensorMap` with the input tensor to be transformed.
|
|
106
|
+
:return: :py:class:`TensorMap`
|
|
107
|
+
"""
|
|
108
|
+
# Currently not supporting gradients
|
|
109
|
+
if len(tensor[0].gradients_list()) != 0:
|
|
110
|
+
raise ValueError(
|
|
111
|
+
"Gradients not supported. Please use metatensor.remove_gradients()"
|
|
112
|
+
" before using this module"
|
|
113
|
+
)
|
|
114
|
+
return self.module_map(tensor)
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
from typing import List, Optional
|
|
2
|
+
|
|
3
|
+
import torch
|
|
4
|
+
from torch.nn import Module
|
|
5
|
+
|
|
6
|
+
from .._backend import Labels, TensorMap
|
|
7
|
+
from .module_map import ModuleMap
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Tanh(Module):
|
|
11
|
+
"""
|
|
12
|
+
Module similar to :py:class:`torch.nn.Tanh` that works with
|
|
13
|
+
:py:class:`metatensor.torch.TensorMap` objects.
|
|
14
|
+
|
|
15
|
+
Applies a hyperbolic tangent transformation to each block of a :py:class:`TensorMap`
|
|
16
|
+
passed to its forward method, indexed by :param in_keys:.
|
|
17
|
+
|
|
18
|
+
Refer to the :py:class`torch.nn.Tanh` documentation for a more detailed description
|
|
19
|
+
of the parameters.
|
|
20
|
+
|
|
21
|
+
:param in_keys: :py:class:`Labels`, the keys that are assumed to be in the input
|
|
22
|
+
tensor map in the :py:meth:`forward` method.
|
|
23
|
+
:param out_properties: list of :py:class`Labels` (optional), the properties labels
|
|
24
|
+
of the output. By default the output properties are relabeled using
|
|
25
|
+
Labels.range.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
def __init__(
|
|
29
|
+
self, in_keys: Labels, out_properties: Optional[Labels] = None
|
|
30
|
+
) -> None:
|
|
31
|
+
super().__init__()
|
|
32
|
+
modules: List[Module] = [torch.nn.Tanh() for i in range(len(in_keys))]
|
|
33
|
+
self.module_map = ModuleMap(in_keys, modules, out_properties)
|
|
34
|
+
|
|
35
|
+
def forward(self, tensor: TensorMap) -> TensorMap:
|
|
36
|
+
"""
|
|
37
|
+
Apply the transformation to the input tensor map `tensor`.
|
|
38
|
+
|
|
39
|
+
Note: currently not supporting gradients.
|
|
40
|
+
|
|
41
|
+
:param tensor: :py:class:`TensorMap` with the input tensor to be transformed.
|
|
42
|
+
:return: :py:class:`TensorMap`
|
|
43
|
+
"""
|
|
44
|
+
# Currently not supporting gradients
|
|
45
|
+
if len(tensor[0].gradients_list()) != 0:
|
|
46
|
+
raise ValueError(
|
|
47
|
+
"Gradients not supported. Please use metatensor.remove_gradients()"
|
|
48
|
+
" before using this module"
|
|
49
|
+
)
|
|
50
|
+
return self.module_map(tensor)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class InvariantTanh(torch.nn.Module):
|
|
54
|
+
"""
|
|
55
|
+
Module similar to :py:class:`torch.nn.Tanh` that works with
|
|
56
|
+
:py:class:`metatensor.torch.TensorMap` objects, applying the transformation only to
|
|
57
|
+
the invariant blocks.
|
|
58
|
+
|
|
59
|
+
Applies a hyperbolic tangent transformation to each invariant block of a
|
|
60
|
+
:py:class:`TensorMap` passed to its :py:meth:`forward` method. These are indexed by
|
|
61
|
+
the keys in :param in_keys: at numeric indices passed in :param invariant_key_idxs:.
|
|
62
|
+
|
|
63
|
+
Refer to the :py:class`torch.nn.Tanh` documentation for a more detailed description
|
|
64
|
+
of the parameters.
|
|
65
|
+
|
|
66
|
+
:param in_keys: :py:class:`Labels`, the keys that are assumed to be in the input
|
|
67
|
+
tensor map in the :py:meth:`forward` method.
|
|
68
|
+
:param invariant_key_idxs: list of int, the indices of the invariant keys present in
|
|
69
|
+
`in_keys` in the input :py:class:`TensorMap`. Only blocks for these keys will
|
|
70
|
+
have the tanh transformation applied. Covariant blocks will have the identity
|
|
71
|
+
operator applied.
|
|
72
|
+
:param out_properties: list of :py:class`Labels` (optional), the properties labels
|
|
73
|
+
of the output. By default the output properties are relabeled using
|
|
74
|
+
Labels.range.
|
|
75
|
+
"""
|
|
76
|
+
|
|
77
|
+
def __init__(
|
|
78
|
+
self,
|
|
79
|
+
in_keys: Labels,
|
|
80
|
+
invariant_key_idxs: List[int],
|
|
81
|
+
out_properties: Optional[Labels] = None,
|
|
82
|
+
) -> None:
|
|
83
|
+
super().__init__()
|
|
84
|
+
modules: List[Module] = []
|
|
85
|
+
for i in range(len(in_keys)):
|
|
86
|
+
if i in invariant_key_idxs: # Invariant block: apply tanh
|
|
87
|
+
module = torch.nn.Tanh()
|
|
88
|
+
else: # Covariant block: apply identity operator
|
|
89
|
+
module = torch.nn.Identity()
|
|
90
|
+
modules.append(module)
|
|
91
|
+
self.module_map: ModuleMap = ModuleMap(in_keys, modules, out_properties)
|
|
92
|
+
|
|
93
|
+
def forward(self, tensor: TensorMap) -> TensorMap:
|
|
94
|
+
"""
|
|
95
|
+
Apply the transformation to the input tensor map `tensor`.
|
|
96
|
+
|
|
97
|
+
Note: currently not supporting gradients.
|
|
98
|
+
|
|
99
|
+
:param tensor: :py:class:`TensorMap` with the input tensor to be transformed.
|
|
100
|
+
:return: :py:class:`TensorMap`
|
|
101
|
+
"""
|
|
102
|
+
# Currently not supporting gradients
|
|
103
|
+
if len(tensor[0].gradients_list()) != 0:
|
|
104
|
+
raise ValueError(
|
|
105
|
+
"Gradients not supported. Please use metatensor.remove_gradients()"
|
|
106
|
+
" before using this module"
|
|
107
|
+
)
|
|
108
|
+
return self.module_map(tensor)
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: metatensor-learn
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.2
|
|
4
4
|
Summary: Building blocks for the atomistic machine learning models based on PyTorch and NumPy
|
|
5
5
|
Author: Guillaume Fraux, Davide Tisi, Philip Loche, Joseph W. Abbott, Jigyasa Nigam, Chiheb Ben Mahmoud
|
|
6
6
|
License: BSD-3-Clause
|
|
7
|
-
Project-URL: homepage, https://
|
|
8
|
-
Project-URL: documentation, https://
|
|
7
|
+
Project-URL: homepage, https://docs.metatensor.org/latest/
|
|
8
|
+
Project-URL: documentation, https://docs.metatensor.org/latest/
|
|
9
9
|
Project-URL: repository, https://github.com/lab-cosmo/metatensor
|
|
10
|
-
Project-URL: changelog, https://
|
|
10
|
+
Project-URL: changelog, https://docs.metatensor.org/latest/learn/CHANGELOG.html
|
|
11
11
|
Keywords: machine learning,molecular modeling
|
|
12
12
|
Classifier: Development Status :: 4 - Beta
|
|
13
13
|
Classifier: Intended Audience :: Science/Research
|
|
@@ -12,8 +12,14 @@ metatensor/learn/data/collate.py
|
|
|
12
12
|
metatensor/learn/data/dataloader.py
|
|
13
13
|
metatensor/learn/data/dataset.py
|
|
14
14
|
metatensor/learn/nn/__init__.py
|
|
15
|
+
metatensor/learn/nn/_utils.py
|
|
16
|
+
metatensor/learn/nn/layer_norm.py
|
|
15
17
|
metatensor/learn/nn/linear.py
|
|
16
18
|
metatensor/learn/nn/module_map.py
|
|
19
|
+
metatensor/learn/nn/relu.py
|
|
20
|
+
metatensor/learn/nn/sequential.py
|
|
21
|
+
metatensor/learn/nn/silu.py
|
|
22
|
+
metatensor/learn/nn/tanh.py
|
|
17
23
|
metatensor_learn.egg-info/PKG-INFO
|
|
18
24
|
metatensor_learn.egg-info/SOURCES.txt
|
|
19
25
|
metatensor_learn.egg-info/dependency_links.txt
|
|
@@ -26,10 +26,10 @@ classifiers = [
|
|
|
26
26
|
]
|
|
27
27
|
|
|
28
28
|
[project.urls]
|
|
29
|
-
homepage = "https://
|
|
30
|
-
documentation = "https://
|
|
29
|
+
homepage = "https://docs.metatensor.org/latest/"
|
|
30
|
+
documentation = "https://docs.metatensor.org/latest/"
|
|
31
31
|
repository = "https://github.com/lab-cosmo/metatensor"
|
|
32
|
-
changelog = "https://
|
|
32
|
+
changelog = "https://docs.metatensor.org/latest/learn/CHANGELOG.html"
|
|
33
33
|
|
|
34
34
|
### ======================================================================== ###
|
|
35
35
|
[build-system]
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
from typing import List, Optional, Union
|
|
2
|
-
|
|
3
|
-
import torch
|
|
4
|
-
|
|
5
|
-
from .._backend import Labels, TensorMap
|
|
6
|
-
from .module_map import ModuleMap
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
class Linear(ModuleMap):
|
|
10
|
-
"""
|
|
11
|
-
Construct a module map with only linear modules.
|
|
12
|
-
|
|
13
|
-
:param in_keys:
|
|
14
|
-
The keys that are assumed to be in the input tensor map in the
|
|
15
|
-
:py:meth:`forward` function.
|
|
16
|
-
:param in_features:
|
|
17
|
-
Specifies the dimensionality of the input after it has been applied on the input
|
|
18
|
-
tensor map. If a list of integers is given, it specifies the input dimension for
|
|
19
|
-
each block, therefore it should have the same length as :param in_keys:. If
|
|
20
|
-
only one integer is given, it is assumed that the same be applied on each block.
|
|
21
|
-
:param out_features:
|
|
22
|
-
Specifies the dimensionality of the output after it has been applied on the
|
|
23
|
-
input tensor map. If a list of integers is given, it specifies the output
|
|
24
|
-
dimension for each block, therefore it should have the same length as :param
|
|
25
|
-
in_keys:. If only one integer is given, it is assumed that the same be applied
|
|
26
|
-
on each block.
|
|
27
|
-
:param bias:
|
|
28
|
-
Specifies if a bias term (offset) should be applied on the input tensor map in
|
|
29
|
-
the forward function. If a list of bools is given, it specifies the bias term
|
|
30
|
-
for each block, therefore it should have the same length as :param in_keys:. If
|
|
31
|
-
only one bool value is given, it is assumed that the same be applied on each
|
|
32
|
-
block.
|
|
33
|
-
:param device:
|
|
34
|
-
Specifies the torch device of the values. If None the default torch device is
|
|
35
|
-
taken.
|
|
36
|
-
:param dtype:
|
|
37
|
-
Specifies the torch dtype of the values. If None the default torch dtype is
|
|
38
|
-
taken.
|
|
39
|
-
:param out_properties:
|
|
40
|
-
A list of labels that is used to determine the properties labels of the
|
|
41
|
-
output. Because a module could change the number of properties, the labels of
|
|
42
|
-
the properties cannot be persevered. By default the output properties are
|
|
43
|
-
relabeled using Labels.range.
|
|
44
|
-
"""
|
|
45
|
-
|
|
46
|
-
def __init__(
|
|
47
|
-
self,
|
|
48
|
-
in_keys: Labels,
|
|
49
|
-
in_features: Union[int, List[int]],
|
|
50
|
-
out_features: Union[int, List[int]],
|
|
51
|
-
bias: Union[bool, List[bool]] = True,
|
|
52
|
-
device: Optional[torch.device] = None,
|
|
53
|
-
dtype: Optional[torch.dtype] = None,
|
|
54
|
-
out_properties: Optional[List[Labels]] = None,
|
|
55
|
-
):
|
|
56
|
-
if isinstance(in_features, int):
|
|
57
|
-
in_features = [in_features] * len(in_keys)
|
|
58
|
-
elif not (isinstance(in_features, List)):
|
|
59
|
-
raise TypeError(
|
|
60
|
-
"`in_features` must be integer or List of integers, but not"
|
|
61
|
-
f" {type(in_features)}."
|
|
62
|
-
)
|
|
63
|
-
elif len(in_keys) != len(in_features):
|
|
64
|
-
raise ValueError(
|
|
65
|
-
"`in_features` must have same length as `in_keys`, but"
|
|
66
|
-
f" len(in_features) != len(in_keys) [{len(in_features)} !="
|
|
67
|
-
f" {len(in_keys)}]"
|
|
68
|
-
)
|
|
69
|
-
|
|
70
|
-
if isinstance(out_features, int):
|
|
71
|
-
out_features = [out_features] * len(in_keys)
|
|
72
|
-
elif not (isinstance(out_features, List)):
|
|
73
|
-
raise TypeError(
|
|
74
|
-
"`out_features` must be integer or List of integers, but not"
|
|
75
|
-
f" {type(out_features)}."
|
|
76
|
-
)
|
|
77
|
-
elif len(in_keys) != len(out_features):
|
|
78
|
-
raise ValueError(
|
|
79
|
-
"`out_features` must have same length as `in_keys`, but"
|
|
80
|
-
f" len(out_features) != len(in_keys) [{len(out_features)} !="
|
|
81
|
-
f" {len(in_keys)}]"
|
|
82
|
-
)
|
|
83
|
-
|
|
84
|
-
if isinstance(bias, bool):
|
|
85
|
-
bias = [bias] * len(in_keys)
|
|
86
|
-
elif not (isinstance(bias, List)):
|
|
87
|
-
raise TypeError(
|
|
88
|
-
f"`bias` must be bool or List of bools, but not {type(bias)}."
|
|
89
|
-
)
|
|
90
|
-
elif len(in_keys) != len(bias):
|
|
91
|
-
raise ValueError(
|
|
92
|
-
"`bias` must have same length as `in_keys`, but len(bias) !="
|
|
93
|
-
f" len(in_keys) [{len(bias)} != {len(in_keys)}]"
|
|
94
|
-
)
|
|
95
|
-
|
|
96
|
-
modules = []
|
|
97
|
-
for i in range(len(in_keys)):
|
|
98
|
-
module = torch.nn.Linear(
|
|
99
|
-
in_features[i],
|
|
100
|
-
out_features[i],
|
|
101
|
-
bias[i],
|
|
102
|
-
device,
|
|
103
|
-
dtype,
|
|
104
|
-
)
|
|
105
|
-
modules.append(module)
|
|
106
|
-
|
|
107
|
-
super().__init__(in_keys, modules, out_properties)
|
|
108
|
-
|
|
109
|
-
@classmethod
|
|
110
|
-
def from_weights(cls, weights: TensorMap, bias: Optional[TensorMap] = None):
|
|
111
|
-
"""
|
|
112
|
-
Construct a linear module map from a tensor map for the weights and bias.
|
|
113
|
-
|
|
114
|
-
:param weights:
|
|
115
|
-
The weight tensor map from which we create the linear modules. The
|
|
116
|
-
properties of the tensor map describe the input dimension and the samples
|
|
117
|
-
describe the output dimension.
|
|
118
|
-
|
|
119
|
-
:param bias:
|
|
120
|
-
The weight tensor map from which we create the linear layers.
|
|
121
|
-
"""
|
|
122
|
-
linear = cls(
|
|
123
|
-
weights.keys,
|
|
124
|
-
in_features=[len(weights[i].samples) for i in range(len(weights))],
|
|
125
|
-
out_features=[len(weights[i].properties) for i in range(len(weights))],
|
|
126
|
-
bias=False,
|
|
127
|
-
device=weights.device,
|
|
128
|
-
dtype=weights[0].values.dtype, # dtype is consistent over blocks in map
|
|
129
|
-
out_properties=[weights[i].properties for i in range(len(weights))],
|
|
130
|
-
)
|
|
131
|
-
for i in range(len(weights)):
|
|
132
|
-
key = weights.keys[i]
|
|
133
|
-
weights_block = weights[i]
|
|
134
|
-
linear[i].weight = torch.nn.Parameter(weights_block.values.T)
|
|
135
|
-
if bias is not None:
|
|
136
|
-
linear[i].bias = torch.nn.Parameter(bias.block(key).values)
|
|
137
|
-
|
|
138
|
-
return linear
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{metatensor-learn-0.2.1 → metatensor_learn-0.2.2}/metatensor_learn.egg-info/dependency_links.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|