forked from ITMO-NSS-team/EPDE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluators.py
More file actions
318 lines (251 loc) · 14.4 KB
/
Copy pathevaluators.py
File metadata and controls
318 lines (251 loc) · 14.4 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 5 13:41:07 2021
@author: mike_ubuntu
"""
import numpy as np
import torch
# device = torch.device('cpu')
from abc import ABC, abstractmethod
from typing import Callable, Union, List
import epde.globals as global_var
from epde.supplementary import factor_params_to_str
class EvaluatorTemplate(ABC):
def __init__(self):
pass
@abstractmethod
def __call__(self, factor, structural: bool = False, grids: list = None,
torch_mode: bool = False, **kwargs):
raise NotImplementedError(
'Trying to call the method of an abstract class')
class CustomEvaluator(EvaluatorTemplate):
def __init__(self, evaluation_functions_np: Union[Callable, dict] = None,
evaluation_functions_torch: Union[Callable, dict] = None,
eval_fun_params_labels: Union[list, tuple, set] = ['power'],
native_vectorized: bool = False):
"""Wrap one or many evaluation functions for use as a factor evaluator.
``native_vectorized=True`` skips the per-element ``np.vectorize``
dispatch on the hot path: the func is called ONCE with the full
grid arrays. The built-in evaluators in this module all set this
flag because their numpy ops (``np.cos``, ``np.sin``, ``np.power``,
``np.full_like``, etc.) vectorize natively. User code passing a
non-vectorising callable should leave the default ``False``.
"""
self._evaluation_functions_np = evaluation_functions_np
self._evaluation_functions_torch = evaluation_functions_torch
if (evaluation_functions_np is None) and (evaluation_functions_torch is None):
raise ValueError('No evaluation function set in the initialization of CustomEvaluator.')
if isinstance(evaluation_functions_np, dict):
self._single_function_token = False
else:
self._single_function_token = True
self.eval_fun_params_labels = eval_fun_params_labels
self.native_vectorized = native_vectorized
def __call__(self, factor, structural: bool = False, func_args: List[Union[torch.Tensor, np.ndarray]] = None,
torch_mode: bool = False, **kwargs): # s
if torch_mode: # TODO: rewrite
torch_mode_explicit = True
if not self._single_function_token and factor.label not in self._evaluation_functions_np.keys():
raise KeyError(
'The label of the token function does not match keys of the evaluator functions')
if func_args is not None:
if isinstance(func_args[0], np.ndarray) or self._evaluation_functions_torch is None:
funcs = self._evaluation_functions_np if self._single_function_token else self._evaluation_functions_np[factor.label]
elif isinstance(func_args[0], torch.Tensor) or self._evaluation_functions_np is None or torch_mode_explicit:
funcs = self._evaluation_functions_torch if self._single_function_token else self._evaluation_functions_torch[factor.label]
elif torch_mode:
funcs = self._evaluation_functions_torch if self._single_function_token else self._evaluation_functions_torch[factor.label]
else:
funcs = self._evaluation_functions_np if self._single_function_token else self._evaluation_functions_np[factor.label]
eval_fun_kwargs = dict()
for key in self.eval_fun_params_labels:
for param_idx, param_descr in factor.params_description.items():
if param_descr['name'] == key:
eval_fun_kwargs[key] = factor.params[param_idx]
if func_args is None:
new_grid = False
func_args = factor.grids
else:
new_grid = True
if self.native_vectorized:
# Fast path: call funcs once with the full grid arrays. The
# built-in numpy evaluators (trig, sign, grid, inverse,
# const, velocity) all return an array of shape
# ``func_args[0].shape``. This skips an N-element
# ``np.vectorize`` loop that on Wave (65k samples)
# dominated evaluator self-time at ~35 s per run.
value = funcs(*func_args, **eval_fun_kwargs)
else:
grid_function = np.vectorize(lambda args: funcs(*args, **eval_fun_kwargs))
try:
if new_grid:
raise AttributeError
self.indexes_vect
except AttributeError:
self.indexes_vect = np.empty_like(func_args[0], dtype=object)
for tensor_idx, _ in np.ndenumerate(func_args[0]):
self.indexes_vect[tensor_idx] = tuple([subarg[tensor_idx]
for subarg in func_args])
value = grid_function(self.indexes_vect)
value = value[global_var.grid_cache.g_func != 0]
value = value.reshape(-1)
return value
def simple_function_evaluator(factor, structural: bool = False, grids=None,
torch_mode: bool = False, **kwargs):
'''
Example of the evaluator of token values, that can be used for uploading values of stored functions from cache. Cases, when
this approach can be used, include evaluating derivatives, coordinates, etc.
Parameters
----------
factor : epde.factor.Factor object,
Object, that represents a factor from the equation terms, for that we want to calculate the values.
structural : bool,
Mark, if the evaluated value will be used for discovering equation structure (True), or calculating coefficients (False)
Returns
----------
value : numpy.ndarray
Vector of the evaluation of the token values, that can be used as target, or feature during the LASSO regression.
'''
for param_idx, param_descr in factor.params_description.items():
if param_descr['name'] == 'power':
power_param_idx = param_idx
if grids is not None:
value = factor.predict_with_ann(grids)
value = value**(factor.params[power_param_idx])
return value
else:
if factor.params[power_param_idx] == 1:
# Same bucketed key Factor.evaluate uses so trig factors with
# within-tolerance freq share a single cached evaluation.
value = global_var.tensor_cache.get(factor.structural_label, structural = structural, torch_mode = torch_mode)
return value
else:
value = global_var.tensor_cache.get(factor_params_to_str(factor, set_default_power = True,
power_idx = power_param_idx),
structural = structural, torch_mode = torch_mode)
value = value**(factor.params[power_param_idx])
return value
sign_eval_fun_np = lambda *args, **kwargs: np.sign(args[0]) # If dim argument is needed here: int(kwargs['dim'])
sign_eval_fun_torch = lambda *args, **kwargs: torch.sign(args[0])
trig_eval_fun_np = {'cos': lambda *grids, **kwargs: np.cos(kwargs['freq'] * grids[int(kwargs['dim'])]) ** kwargs['power'],
'sin': lambda *grids, **kwargs: np.sin(kwargs['freq'] * grids[int(kwargs['dim'])]) ** kwargs['power']}
trig_eval_fun_torch = {'cos': lambda *grids, **kwargs: torch.cos(kwargs['freq'] * grids[int(kwargs['dim'])]) ** kwargs['power'],
'sin': lambda *grids, **kwargs: torch.sin(kwargs['freq'] * grids[int(kwargs['dim'])]) ** kwargs['power']}
inverse_eval_fun_np = lambda *grids, **kwargs: np.power(grids[int(kwargs['dim'])], - kwargs['power'])
inverse_eval_fun_torch = lambda *grids, **kwargs: torch.pow(grids[int(kwargs['dim'])], - kwargs['power'])
grid_eval_fun_np = lambda *grids, **kwargs: np.power(grids[int(kwargs['dim'])], kwargs['power'])
grid_eval_fun_torch = lambda *grids, **kwargs: torch.pow(grids[int(kwargs['dim'])], kwargs['power'])
def phased_sine_np(*grids, **kwargs):
coordwise_elems = [kwargs['freq'][dim] * 2*np.pi*(grids[dim] + kwargs['phase'][dim])
for dim in range(len(grids))]
return np.power(np.sin(np.sum(coordwise_elems, axis = 0)), kwargs['power'])
def phased_sine_torch(*grids, **kwargs):
coordwise_elems = [kwargs['freq'][dim] * 2*torch.pi*(grids[dim] + kwargs['phase'][dim])
for dim in range(len(grids))]
return torch.pow(torch.sin(torch.sum(coordwise_elems, axis = 0)), kwargs['power'])
def phased_sine_1d_np(*grids, **kwargs):
coordwise_elems = kwargs['freq'] * 2*np.pi*(grids[0] + kwargs['phase']/kwargs['freq'])
return np.power(np.sin(coordwise_elems), kwargs['power'])
def phased_sine_1d_torch(*grids, **kwargs):
coordwise_elems = kwargs['freq'] * 2*torch.pi*(grids[0] + kwargs['phase']/kwargs['freq'])
return torch.pow(torch.sin(coordwise_elems), kwargs['power'])
def const_eval_fun_np(*grids, **kwargs):
return np.full_like(a=grids[0], fill_value=kwargs['value'])
def const_eval_fun_torch(*grids, **kwargs):
return torch.full_like(a=grids[0], fill_value=kwargs['value'])
def const_grad_fun_np(*grids, **kwargs):
return np.zeros_like(a=grids[0])
def const_grad_fun_torch(*grids, **kwargs):
return torch.zeros_like(a=grids[0])
def get_velocity_common(*grids, **kwargs):
a = [kwargs['p' + str(idx*3+1)] * grids[0]**2 + kwargs['p' + str(idx*3 + 2)] * grids[0] + kwargs['p' + str(idx*3 + 3)] for idx in range(5)]
alpha = np.exp(a[0] * grids[1] + a[1]); beta = a[2] * grids[1]**2 + a[3] * grids[1] + a[4]
return alpha, beta
def velocity_heating_eval_fun(*grids, **kwargs):
'''
Assumption of the velocity field for two-dimensional heat equation with convetion.
'''
alpha, beta = get_velocity_common(*grids, **kwargs)
return alpha * beta
def vhef_grad_1(*grids, **kwargs):
alpha, beta = get_velocity_common(*grids, **kwargs)
return grids[0]**2 * grids[1] * alpha * beta
def vhef_grad_2(*grids, **kwargs):
alpha, beta = get_velocity_common(*grids, **kwargs)
return grids[0] * grids[1] * alpha * beta
def vhef_grad_3(*grids, **kwargs):
alpha, beta = get_velocity_common(*grids, **kwargs)
return grids[1] * alpha * beta
def vhef_grad_4(*grids, **kwargs):
alpha, beta = get_velocity_common(*grids, **kwargs)
return grids[0]**2 * alpha * beta
def vhef_grad_5(*grids, **kwargs):
alpha, beta = get_velocity_common(*grids, **kwargs)
return grids[0] * alpha * beta
def vhef_grad_6(*grids, **kwargs):
alpha, beta = get_velocity_common(*grids, **kwargs)
return alpha * beta
def vhef_grad_7(*grids, **kwargs):
alpha, beta = get_velocity_common(*grids, **kwargs)
return grids[0]**2 * grids[1]**2 * alpha
def vhef_grad_8(*grids, **kwargs):
alpha, beta = get_velocity_common(*grids, **kwargs)
return grids[0] * grids[1]**2 * alpha
def vhef_grad_9(*grids, **kwargs):
alpha, beta = get_velocity_common(*grids, **kwargs)
return grids[1]**2 * alpha
def vhef_grad_10(*grids, **kwargs):
alpha, beta = get_velocity_common(*grids, **kwargs)
return grids[0]**2 * grids[1] * alpha
def vhef_grad_11(*grids, **kwargs):
alpha, beta = get_velocity_common(*grids, **kwargs)
return grids[0] * grids[1] * alpha
def vhef_grad_12(*grids, **kwargs):
alpha, beta = get_velocity_common(*grids, **kwargs)
return grids[1] * alpha
def vhef_grad_13(*grids, **kwargs):
alpha, beta = get_velocity_common(*grids, **kwargs)
return grids[0]**2 * alpha
def vhef_grad_14(*grids, **kwargs):
alpha, beta = get_velocity_common(*grids, **kwargs)
return grids[0] * alpha
def vhef_grad_15(*grids, **kwargs):
alpha, beta = get_velocity_common(*grids, **kwargs)
return alpha
vhef_grad = [vhef_grad_1, vhef_grad_2, vhef_grad_3,
vhef_grad_4, vhef_grad_5, vhef_grad_6,
vhef_grad_7, vhef_grad_8, vhef_grad_9,
vhef_grad_10, vhef_grad_11, vhef_grad_12,
vhef_grad_13, vhef_grad_14, vhef_grad_15]
sign_evaluator = CustomEvaluator(evaluation_functions_np=sign_eval_fun_np,
evaluation_functions_torch=sign_eval_fun_torch,
eval_fun_params_labels = ['power', 'dim'],
native_vectorized=True)
phased_sine_evaluator = CustomEvaluator(evaluation_functions_np = phased_sine_1d_np,
evaluation_functions_torch = phased_sine_1d_torch,
eval_fun_params_labels = ['power', 'freq', 'phase'],
native_vectorized=True) # , use_factors_grids = True
trigonometric_evaluator = CustomEvaluator(evaluation_functions_np = trig_eval_fun_np,
evaluation_functions_torch = trig_eval_fun_torch,
eval_fun_params_labels=['freq', 'dim', 'power'],
native_vectorized=True) # , use_factors_grids = True
grid_evaluator = CustomEvaluator(evaluation_functions_np = grid_eval_fun_np,
evaluation_functions_torch = grid_eval_fun_torch,
eval_fun_params_labels=['dim', 'power'],
native_vectorized=True) # , use_factors_grids=True
inverse_function_evaluator = CustomEvaluator(evaluation_functions_np = inverse_eval_fun_np,
evaluation_functions_torch = inverse_eval_fun_torch,
eval_fun_params_labels=['dim', 'power'],
native_vectorized=True) # , use_factors_grids=True
const_evaluator = CustomEvaluator(evaluation_functions_np = const_eval_fun_np,
evaluation_functions_torch = const_eval_fun_torch,
eval_fun_params_labels = ['power', 'value'],
native_vectorized=True)
const_grad_evaluator = CustomEvaluator(evaluation_functions_np = const_grad_fun_np,
evaluation_functions_torch = const_grad_fun_np,
eval_fun_params_labels = ['power', 'value'],
native_vectorized=True)
velocity_evaluator = CustomEvaluator(velocity_heating_eval_fun, ['p' + str(idx+1) for idx in range(15)])
velocity_grad_evaluators = [CustomEvaluator(component, ['p' + str(idx+1) for idx in range(15)])
for component in vhef_grad]