-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathscale.py
173 lines (136 loc) · 6.05 KB
/
scale.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import keras as ks
from typing import Union
from kgcnn.layers.pooling import PoolingNodes
import numpy as np
from keras import ops
class StandardLabelScaler(ks.layers.Layer): # noqa
def __init__(self, scaling_shape: tuple = None, dtype_scale: str = "float64", trainable: bool = False,
name="StandardLabelScaler", **kwargs):
r"""Initialize layer instance of :obj:`StandardLabelScaler` .
Args:
scaling_shape (tuple): Shape
"""
super(StandardLabelScaler, self).__init__(**kwargs)
self._scaling_shape = scaling_shape
self.name = name
self._weights_trainable = trainable
self.dtype_scale = dtype_scale
self.extensive = False
if self._scaling_shape is not None:
self._add_weights_for_scaling()
def _add_weights_for_scaling(self):
self.scale_ = self.add_weight(
shape=self._scaling_shape,
initializer="ones", trainable=self._weights_trainable, dtype=self.dtype_scale
)
self.mean_ = self.add_weight(
shape=self._scaling_shape,
initializer="zeros", trainable=self._weights_trainable, dtype=self.dtype_scale
)
def build(self, input_shape):
if self._scaling_shape is None:
if input_shape is None:
raise ValueError("Can not build scale and mean weights if `input_shape` not known.")
self._scaling_shape = tuple([1 if i is None else i for i in input_shape])
self._add_weights_for_scaling()
self.built = True
def compute_output_shape(self, input_shape):
return input_shape
def call(self, inputs, **kwargs):
return ops.cast(inputs, dtype=self.dtype_scale)*self.scale_ + self.mean_
def get_config(self):
config = super(StandardLabelScaler, self).get_config()
config.update({})
return config
def set_scale(self, scaler):
self.set_weights([scaler.get_scaling(), scaler.get_mean_shift()])
class ExtensiveMolecularLabelScaler(ks.layers.Layer): # noqa
max_atomic_number = 95
def __init__(self, scaling_shape: tuple = None, dtype_scale: str = "float64", trainable: bool = False,
name="ExtensiveMolecularLabelScaler", **kwargs):
r"""Initialize layer instance of :obj:`StandardLabelScaler` .
Args:
scaling_shape (tuple): Shape
"""
super(ExtensiveMolecularLabelScaler, self).__init__(**kwargs)
self._scaling_shape = scaling_shape
self.name = name
self._weights_trainable = trainable
self.dtype_scale = dtype_scale
self.extensive = True
self.layer_pool = PoolingNodes(pooling_method="scatter_sum")
self._fit_atom_selection_mask = self.add_weight(
shape=(self.max_atomic_number, ), trainable=False, dtype="bool", initializer="zeros")
if self._scaling_shape is not None:
self._add_weights_for_scaling()
def _add_weights_for_scaling(self):
self.scale_ = self.add_weight(
shape=self._scaling_shape,
initializer="ones", trainable=self._weights_trainable, dtype=self.dtype_scale
)
self.ridge_kernel_ = self.add_weight(
shape=tuple([self.max_atomic_number] + list(self._scaling_shape[1:])),
initializer="zeros", trainable=self._weights_trainable, dtype=self.dtype_scale
)
def build(self, input_shape):
if self._scaling_shape is None:
if input_shape is None:
raise ValueError("Can not build scale and mean weights if `input_shape` and `scaling_shape` not known.")
self._scaling_shape = tuple([1 if i is None else i for i in input_shape[0]])
self._add_weights_for_scaling()
self.built = True
def compute_output_shape(self, input_shape):
return input_shape[0]
def call(self, inputs, **kwargs):
graph, nodes, batch_id = inputs
energy_per_node = ops.take(self.ridge_kernel_, nodes, axis=0)
extensive_energies = self.layer_pool([graph, energy_per_node, batch_id])
return ops.cast(graph, dtype=self.dtype_scale)*self.scale_ + extensive_energies
def get_config(self):
config = super(ExtensiveMolecularLabelScaler, self).get_config()
config.update({})
return config
def set_scale(self, scaler):
ridge_kernel = np.transpose(np.array(scaler.ridge.coef_))
pos = np.sort(np.array(scaler._fit_atom_selection))
mask = np.array(scaler._fit_atom_selection_mask)
shape = tuple([int(self.max_atomic_number)] + list(ridge_kernel.shape[1:]))
layer_kernel = np.zeros(shape)
layer_kernel[pos] = ridge_kernel
layer_kernel[0] = 0. # Make sure 0 is always 0.
self.set_weights([mask, scaler.get_scaling(), layer_kernel])
class QMGraphLabelScaler(ks.layers.Layer): # noqa
max_atomic_number = 95
def __init__(self, scaler_list: list = None, name="QMGraphLabelScaler", **kwargs):
r"""Initialize layer instance of :obj:`StandardLabelScaler` .
Args:
scaler_list (list): List of scaler
"""
super(QMGraphLabelScaler, self).__init__(**kwargs)
self._scaler_list = scaler_list
self.name = name
self.extensive = True
def build(self, input_shape):
for scaler in self._scaler_list:
if scaler.extensive:
scaler.build(input_shape)
else:
scaler.build(input_shape[0])
self.built = True
def compute_output_shape(self, input_shape):
return input_shape[0]
def call(self, inputs, **kwargs):
return inputs
def get_config(self):
config = super(QMGraphLabelScaler, self).get_config()
config.update({})
return config
def set_scale(self, scaler):
for s in self._scaler_list:
s.set_scale(scaler)
def get(scale_name: str):
scaler_reference = {
"StandardLabelScaler": StandardLabelScaler,
"ExtensiveMolecularLabelScaler": ExtensiveMolecularLabelScaler
}
return scaler_reference[scale_name]