reflectorch 1.5.1__py3-none-any.whl

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.
Files changed (96) hide show
  1. reflectorch/__init__.py +17 -0
  2. reflectorch/data_generation/__init__.py +128 -0
  3. reflectorch/data_generation/dataset.py +216 -0
  4. reflectorch/data_generation/likelihoods.py +80 -0
  5. reflectorch/data_generation/noise.py +471 -0
  6. reflectorch/data_generation/priors/__init__.py +60 -0
  7. reflectorch/data_generation/priors/base.py +55 -0
  8. reflectorch/data_generation/priors/exp_subprior_sampler.py +298 -0
  9. reflectorch/data_generation/priors/independent_priors.py +195 -0
  10. reflectorch/data_generation/priors/multilayer_models.py +311 -0
  11. reflectorch/data_generation/priors/multilayer_structures.py +104 -0
  12. reflectorch/data_generation/priors/no_constraints.py +206 -0
  13. reflectorch/data_generation/priors/parametric_models.py +842 -0
  14. reflectorch/data_generation/priors/parametric_subpriors.py +369 -0
  15. reflectorch/data_generation/priors/params.py +252 -0
  16. reflectorch/data_generation/priors/sampler_strategies.py +370 -0
  17. reflectorch/data_generation/priors/scaler_mixin.py +65 -0
  18. reflectorch/data_generation/priors/subprior_sampler.py +371 -0
  19. reflectorch/data_generation/priors/utils.py +118 -0
  20. reflectorch/data_generation/process_data.py +41 -0
  21. reflectorch/data_generation/q_generator.py +280 -0
  22. reflectorch/data_generation/reflectivity/__init__.py +102 -0
  23. reflectorch/data_generation/reflectivity/abeles.py +97 -0
  24. reflectorch/data_generation/reflectivity/kinematical.py +71 -0
  25. reflectorch/data_generation/reflectivity/memory_eff.py +105 -0
  26. reflectorch/data_generation/reflectivity/numpy_implementations.py +120 -0
  27. reflectorch/data_generation/reflectivity/smearing.py +138 -0
  28. reflectorch/data_generation/reflectivity/smearing_pointwise.py +110 -0
  29. reflectorch/data_generation/scale_curves.py +112 -0
  30. reflectorch/data_generation/smearing.py +99 -0
  31. reflectorch/data_generation/utils.py +223 -0
  32. reflectorch/extensions/__init__.py +0 -0
  33. reflectorch/extensions/jupyter/__init__.py +11 -0
  34. reflectorch/extensions/jupyter/api.py +85 -0
  35. reflectorch/extensions/jupyter/callbacks.py +34 -0
  36. reflectorch/extensions/jupyter/components.py +758 -0
  37. reflectorch/extensions/jupyter/custom_select.py +268 -0
  38. reflectorch/extensions/jupyter/log_widget.py +241 -0
  39. reflectorch/extensions/jupyter/model_selection.py +495 -0
  40. reflectorch/extensions/jupyter/plotly_plot_manager.py +329 -0
  41. reflectorch/extensions/jupyter/widget.py +625 -0
  42. reflectorch/extensions/matplotlib/__init__.py +5 -0
  43. reflectorch/extensions/matplotlib/losses.py +32 -0
  44. reflectorch/extensions/refnx/refnx_conversion.py +77 -0
  45. reflectorch/inference/__init__.py +28 -0
  46. reflectorch/inference/inference_model.py +848 -0
  47. reflectorch/inference/input_interface.py +239 -0
  48. reflectorch/inference/loading_data.py +55 -0
  49. reflectorch/inference/multilayer_fitter.py +171 -0
  50. reflectorch/inference/multilayer_inference_model.py +193 -0
  51. reflectorch/inference/plotting.py +524 -0
  52. reflectorch/inference/preprocess_exp/__init__.py +7 -0
  53. reflectorch/inference/preprocess_exp/attenuation.py +36 -0
  54. reflectorch/inference/preprocess_exp/cut_with_q_ratio.py +31 -0
  55. reflectorch/inference/preprocess_exp/footprint.py +81 -0
  56. reflectorch/inference/preprocess_exp/interpolation.py +19 -0
  57. reflectorch/inference/preprocess_exp/normalize.py +21 -0
  58. reflectorch/inference/preprocess_exp/preprocess.py +121 -0
  59. reflectorch/inference/query_matcher.py +82 -0
  60. reflectorch/inference/record_time.py +43 -0
  61. reflectorch/inference/sampler_solution.py +56 -0
  62. reflectorch/inference/scipy_fitter.py +364 -0
  63. reflectorch/inference/torch_fitter.py +87 -0
  64. reflectorch/ml/__init__.py +32 -0
  65. reflectorch/ml/basic_trainer.py +292 -0
  66. reflectorch/ml/callbacks.py +81 -0
  67. reflectorch/ml/dataloaders.py +27 -0
  68. reflectorch/ml/loggers.py +56 -0
  69. reflectorch/ml/schedulers.py +356 -0
  70. reflectorch/ml/trainers.py +201 -0
  71. reflectorch/ml/utils.py +2 -0
  72. reflectorch/models/__init__.py +16 -0
  73. reflectorch/models/activations.py +50 -0
  74. reflectorch/models/encoders/__init__.py +19 -0
  75. reflectorch/models/encoders/conv_encoder.py +219 -0
  76. reflectorch/models/encoders/conv_res_net.py +115 -0
  77. reflectorch/models/encoders/fno.py +134 -0
  78. reflectorch/models/encoders/integral_kernel_embedding.py +390 -0
  79. reflectorch/models/networks/__init__.py +14 -0
  80. reflectorch/models/networks/mlp_networks.py +434 -0
  81. reflectorch/models/networks/residual_net.py +157 -0
  82. reflectorch/paths.py +29 -0
  83. reflectorch/runs/__init__.py +31 -0
  84. reflectorch/runs/config.py +25 -0
  85. reflectorch/runs/slurm_utils.py +93 -0
  86. reflectorch/runs/train.py +78 -0
  87. reflectorch/runs/utils.py +405 -0
  88. reflectorch/test_config.py +4 -0
  89. reflectorch/train.py +4 -0
  90. reflectorch/train_on_cluster.py +4 -0
  91. reflectorch/utils.py +98 -0
  92. reflectorch-1.5.1.dist-info/METADATA +151 -0
  93. reflectorch-1.5.1.dist-info/RECORD +96 -0
  94. reflectorch-1.5.1.dist-info/WHEEL +5 -0
  95. reflectorch-1.5.1.dist-info/licenses/LICENSE.txt +21 -0
  96. reflectorch-1.5.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,298 @@
1
+ from typing import Tuple, Union, List
2
+
3
+ import torch
4
+ from torch import Tensor
5
+
6
+ from reflectorch.data_generation.utils import (
7
+ uniform_sampler,
8
+ logdist_sampler,
9
+ )
10
+
11
+ from reflectorch.data_generation.priors.scaler_mixin import ScalerMixin
12
+ from reflectorch.data_generation.priors.base import PriorSampler
13
+ from reflectorch.data_generation.priors.subprior_sampler import UniformSubPriorParams
14
+ from reflectorch.data_generation.priors.params import Params
15
+ from reflectorch.data_generation.priors.utils import get_max_allowed_roughness
16
+ from reflectorch.data_generation.priors.no_constraints import (
17
+ DEFAULT_DEVICE,
18
+ DEFAULT_DTYPE,
19
+ )
20
+
21
+
22
+ class ExpUniformSubPriorSampler(PriorSampler, ScalerMixin):
23
+ PARAM_CLS = UniformSubPriorParams
24
+
25
+ def __init__(self,
26
+ params: List[Union[float, Tuple[float, float], Tuple[float, float, float, float]]],
27
+ device: torch.device = DEFAULT_DEVICE,
28
+ dtype: torch.dtype = DEFAULT_DTYPE,
29
+ scaled_range: Tuple[float, float] = (-1, 1),
30
+ logdist: bool = False,
31
+ relative_min_bound_width: float = 1e-4,
32
+ smaller_roughnesses: bool = True,
33
+ ):
34
+ self.device = device
35
+ self.dtype = dtype
36
+ self.scaled_range = scaled_range
37
+ self.relative_min_bound_width = relative_min_bound_width
38
+ self.logdist = logdist
39
+ self.smaller_roughnesses = smaller_roughnesses
40
+ self._init_params(*params)
41
+
42
+ @property
43
+ def max_num_layers(self) -> int:
44
+ return self.num_layers
45
+
46
+ def _init_params(self, *params: Union[float, Tuple[float, float], Tuple[float, float, float, float]]):
47
+ self.num_layers = (len(params) - 2) // 3
48
+ self._total_num_params = len(params)
49
+
50
+ fixed_mask = []
51
+ bounds = []
52
+ delta_bounds = []
53
+ param_dim = 0
54
+
55
+ for param in params:
56
+ if isinstance(param, (float, int)):
57
+ deltas = (0, 0)
58
+ param = (param, param)
59
+ fixed_mask.append(True)
60
+ else:
61
+ param_dim += 1
62
+ fixed_mask.append(False)
63
+
64
+ if len(param) == 4:
65
+ param, deltas = param[:2], param[2:]
66
+ else:
67
+ max_delta = param[1] - param[0]
68
+ deltas = (max_delta * self.relative_min_bound_width, max_delta)
69
+
70
+ bounds.append(param)
71
+ delta_bounds.append(deltas)
72
+
73
+ self.fixed_mask = torch.tensor(fixed_mask).to(self.device)
74
+ self.fitted_mask = ~self.fixed_mask
75
+ self.roughnesses_mask = torch.zeros_like(self.fitted_mask)
76
+ self.roughnesses_mask[self.num_layers: self.num_layers * 2 + 1] = True
77
+
78
+ self.min_bounds, self.max_bounds = torch.tensor(bounds).to(self.device).to(self.dtype).T
79
+ self.min_deltas, self.max_deltas = map(torch.atleast_2d, torch.tensor(delta_bounds).to(self.min_bounds).T)
80
+ self._param_dim = param_dim
81
+ self._num_fixed = self.fixed_mask.sum()
82
+
83
+ self.fixed_params = self.min_bounds[self.fixed_mask]
84
+
85
+ @property
86
+ def param_dim(self) -> int:
87
+ return self._param_dim
88
+
89
+ def sample(self, batch_size: int) -> UniformSubPriorParams:
90
+ min_bounds, max_bounds = self.sample_bounds(batch_size)
91
+
92
+ params = torch.rand(
93
+ *min_bounds.shape,
94
+ device=self.device,
95
+ dtype=self.dtype
96
+ ) * (max_bounds - min_bounds) + min_bounds
97
+
98
+ thicknesses, roughnesses, slds = torch.split(
99
+ params, [self.max_num_layers, self.max_num_layers + 1, self.max_num_layers + 1], dim=-1
100
+ )
101
+
102
+ if self.smaller_roughnesses:
103
+ fitted_r_mask = self.fitted_mask.clone()
104
+ fitted_r_mask[~self.roughnesses_mask] = False
105
+
106
+ min_roughness = self.min_bounds[fitted_r_mask]
107
+ max_roughness = torch.clamp(
108
+ get_max_allowed_roughness(thicknesses)[..., self.fitted_mask[self.roughnesses_mask]],
109
+ min_roughness,
110
+ self.max_bounds[fitted_r_mask]
111
+ )
112
+
113
+ min_vector = self.min_bounds.clone()[None].repeat(batch_size, 1)
114
+ max_vector = self.max_bounds.clone()[None].repeat(batch_size, 1)
115
+
116
+ max_vector[..., fitted_r_mask] = max_roughness
117
+
118
+ assert torch.all(max_vector[..., fitted_r_mask] == max_roughness)
119
+
120
+ min_deltas = self.min_deltas.clone().repeat(batch_size, 1)
121
+ max_deltas = self.max_deltas.clone().repeat(batch_size, 1)
122
+
123
+ max_deltas[..., fitted_r_mask] = torch.clamp_max(
124
+ max_deltas[..., fitted_r_mask],
125
+ max_roughness - min_roughness,
126
+ )
127
+
128
+ fitted_mask = torch.zeros_like(self.fitted_mask)
129
+ fitted_mask[fitted_r_mask] = True
130
+
131
+ updated_min_bounds, updated_max_bounds = self._sample_bounds(
132
+ batch_size, min_vector, max_vector, min_deltas, max_deltas, fitted_mask
133
+ )
134
+
135
+ min_bounds[..., fitted_mask], max_bounds[..., fitted_mask] = (
136
+ updated_min_bounds[..., fitted_mask], updated_max_bounds[..., fitted_mask]
137
+ )
138
+
139
+ params[..., fitted_mask] = torch.rand(
140
+ batch_size, fitted_mask.sum().item(),
141
+ device=self.device,
142
+ dtype=self.dtype
143
+ ) * (max_bounds[..., fitted_mask] - min_bounds[..., fitted_mask]) + min_bounds[..., fitted_mask]
144
+
145
+ thicknesses, roughnesses, slds = torch.split(
146
+ params, [self.max_num_layers, self.max_num_layers + 1, self.max_num_layers + 1], dim=-1
147
+ )
148
+
149
+ params = UniformSubPriorParams(thicknesses, roughnesses, slds, min_bounds, max_bounds)
150
+
151
+ return params
152
+
153
+ def scale_params(self, params: UniformSubPriorParams) -> Tensor:
154
+ params_t = params.as_tensor(add_bounds=False)
155
+
156
+ scaled_params = self._scale(params_t, params.min_bounds, params.max_bounds)[..., self.fitted_mask]
157
+
158
+ scaled_min_bounds = self._scale(params.min_bounds, self.min_bounds, self.max_bounds)[..., self.fitted_mask]
159
+
160
+ scaled_max_bounds = self._scale(params.max_bounds, self.min_bounds, self.max_bounds)[..., self.fitted_mask]
161
+
162
+ scaled_params = torch.cat([scaled_params, scaled_min_bounds, scaled_max_bounds], -1)
163
+
164
+ return scaled_params
165
+
166
+ def scale_bounds(self, bounds: Tensor) -> Tensor:
167
+ return self._scale(bounds, self.min_bounds, self.max_bounds)[..., self.fitted_mask]
168
+
169
+ def restore_params(self, scaled_params: Tensor) -> UniformSubPriorParams:
170
+ scaled_params, scaled_min_bounds, scaled_max_bounds = torch.split(
171
+ scaled_params, [self.param_dim, self.param_dim, self.param_dim], dim=1
172
+ )
173
+
174
+ min_bounds = self._restore(
175
+ scaled_min_bounds, self.min_bounds[self.fitted_mask], self.max_bounds[self.fitted_mask]
176
+ )
177
+ max_bounds = self._restore(
178
+ scaled_max_bounds, self.min_bounds[self.fitted_mask], self.max_bounds[self.fitted_mask]
179
+ )
180
+
181
+ restored_params = self._restore(scaled_params, min_bounds, max_bounds)
182
+
183
+ params_t = torch.cat(
184
+ [
185
+ self._cat_restored_with_fixed_vector(restored_params),
186
+ self._cat_restored_with_fixed_vector(min_bounds),
187
+ self._cat_restored_with_fixed_vector(max_bounds),
188
+ ], -1
189
+ )
190
+
191
+ params = UniformSubPriorParams.from_tensor(params_t)
192
+
193
+ return params
194
+
195
+ def _cat_restored_with_fixed_vector(self, restored_t: Tensor) -> Tensor:
196
+ return self._cat_fitted_fixed_t(restored_t, self.fixed_params)
197
+
198
+ def _cat_fitted_fixed_t(self, fitted_t: Tensor, fixed_t: Tensor, fitted_mask: Tensor = None) -> Tensor:
199
+ if fitted_mask is None:
200
+ fitted_mask = self.fitted_mask
201
+ fixed_mask = self.fixed_mask
202
+ else:
203
+ fixed_mask = ~fitted_mask
204
+
205
+ total_num_params = self.fitted_mask.sum().item() + self.fixed_mask.sum().item()
206
+
207
+ batch_size = fitted_t.shape[0]
208
+
209
+ concat_t = torch.empty(
210
+ batch_size, total_num_params, device=fitted_t.device, dtype=fitted_t.dtype
211
+ )
212
+ concat_t[:, fitted_mask] = fitted_t
213
+ concat_t[:, fixed_mask] = fixed_t[None].expand(batch_size, -1)
214
+
215
+ return concat_t
216
+
217
+ def log_prob(self, params: UniformSubPriorParams) -> Tensor:
218
+ log_prob = torch.zeros(params.batch_size, device=params.device, dtype=params.dtype)
219
+ indices = self.get_indices_within_bounds(params)
220
+ log_prob[~indices] = float('-inf')
221
+ return log_prob
222
+
223
+ def get_indices_within_bounds(self, params: UniformSubPriorParams) -> Tensor:
224
+ t_params = torch.cat([
225
+ params.thicknesses,
226
+ params.roughnesses,
227
+ params.slds
228
+ ], dim=-1)
229
+
230
+ indices = (
231
+ torch.all(t_params >= params.min_bounds, dim=-1) &
232
+ torch.all(t_params <= params.max_bounds, dim=-1)
233
+ )
234
+
235
+ return indices
236
+
237
+ def clamp_params(self, params: UniformSubPriorParams) -> UniformSubPriorParams:
238
+ params = UniformSubPriorParams.from_tensor(
239
+ torch.cat([
240
+ torch.clamp(
241
+ params.as_tensor(add_bounds=False),
242
+ params.min_bounds, params.max_bounds
243
+ ),
244
+ params.min_bounds, params.max_bounds
245
+ ], dim=1)
246
+ )
247
+ return params
248
+
249
+ def get_indices_within_domain(self, params: UniformSubPriorParams) -> Tensor:
250
+ return self.get_indices_within_bounds(params)
251
+
252
+ def sample_bounds(self, batch_size: int):
253
+ return self._sample_bounds(
254
+ batch_size,
255
+ self.min_bounds,
256
+ self.max_bounds,
257
+ self.min_deltas,
258
+ self.max_deltas,
259
+ self.fitted_mask,
260
+ )
261
+
262
+ def _sample_bounds(self, batch_size, min_vector, max_vector, min_deltas, max_deltas, fitted_mask):
263
+ if self.logdist:
264
+ widths_sampler_func = logdist_sampler
265
+ else:
266
+ widths_sampler_func = uniform_sampler
267
+
268
+ num_fitted = fitted_mask.sum().item()
269
+ num_fixed = fitted_mask.numel() - num_fitted
270
+
271
+ prior_widths = widths_sampler_func(
272
+ min_deltas[..., fitted_mask], max_deltas[..., fitted_mask],
273
+ batch_size, num_fitted,
274
+ device=self.device, dtype=self.dtype
275
+ )
276
+
277
+ prior_widths = self._cat_fitted_fixed_t(prior_widths, torch.zeros(num_fixed).to(prior_widths), fitted_mask)
278
+
279
+ prior_centers = uniform_sampler(
280
+ min_vector + prior_widths / 2, max_vector - prior_widths / 2,
281
+ *prior_widths.shape,
282
+ device=self.device, dtype=self.dtype
283
+ )
284
+
285
+ min_bounds, max_bounds = prior_centers - prior_widths / 2, prior_centers + prior_widths / 2
286
+
287
+ return min_bounds, max_bounds
288
+
289
+ @staticmethod
290
+ def scale_bounds_with_q(bounds: Tensor, q_ratio: float) -> Tensor:
291
+ params = Params.from_tensor(torch.atleast_2d(bounds).clone())
292
+ params.scale_with_q(q_ratio)
293
+ return params.as_tensor().squeeze()
294
+
295
+ def clamp_bounds(self, bounds: Tensor) -> Tensor:
296
+ return torch.clamp(
297
+ torch.atleast_2d(bounds), torch.atleast_2d(self.min_bounds), torch.atleast_2d(self.max_bounds)
298
+ )
@@ -0,0 +1,195 @@
1
+ from typing import Union, Tuple
2
+ from math import log
3
+
4
+ import torch
5
+ from torch import Tensor
6
+
7
+ from reflectorch.data_generation.priors.base import PriorSampler
8
+ from reflectorch.data_generation.priors.params import Params
9
+ from reflectorch.data_generation.utils import get_param_labels
10
+
11
+ __all__ = [
12
+ 'SingleParamPrior',
13
+ 'UniformParamPrior',
14
+ 'GaussianParamPrior',
15
+ 'TruncatedGaussianParamPrior',
16
+ 'SimplePriorSampler',
17
+
18
+ ]
19
+
20
+
21
+ class SingleParamPrior(object):
22
+ def sample(self, batch_num: int, device=None, dtype=None):
23
+ raise NotImplementedError
24
+
25
+ def log_prob(self, params: Tensor):
26
+ raise NotImplementedError
27
+
28
+ def to_conf(self):
29
+ vars_dict = {k: v for k, v in vars(self).items() if not k.startswith('_')}
30
+ return {
31
+ 'cls': self.__class__.__name__,
32
+ 'kwargs': vars_dict
33
+ }
34
+
35
+ def scale(self, params: Tensor) -> Tensor:
36
+ raise NotImplementedError
37
+
38
+ def restore(self, params: Tensor) -> Tensor:
39
+ raise NotImplementedError
40
+
41
+
42
+ class SimplePriorSampler(PriorSampler):
43
+ def __init__(self,
44
+ *params: Union[Tuple[float, float], SingleParamPrior],
45
+ device=None, dtype=None, param_cls=None, scaling_prior: PriorSampler = None,
46
+ ):
47
+ if param_cls is not None:
48
+ self.PARAM_CLS = param_cls
49
+ elif scaling_prior is not None:
50
+ self.PARAM_CLS = scaling_prior.PARAM_CLS
51
+ self.param_priors: Tuple[SingleParamPrior, ...] = self._init_params(params)
52
+ self._num_layers = self.PARAM_CLS.size2layers_num(len(params))
53
+ self.device = device
54
+ self.dtype = dtype
55
+ self.scaling_prior = scaling_prior
56
+
57
+ @property
58
+ def max_num_layers(self) -> int:
59
+ return self._num_layers
60
+
61
+ def _init_params(self, params: Tuple[Union[Tuple[float, float], SingleParamPrior], ...]):
62
+ assert len(params) == self.PARAM_CLS.layers_num2size(self.PARAM_CLS.size2layers_num(len(params)))
63
+ params = tuple(
64
+ param if isinstance(param, SingleParamPrior) else UniformParamPrior(*param)
65
+ for param in params
66
+ )
67
+ return params
68
+
69
+ def sample(self, batch_size: int) -> Params:
70
+ t_params = torch.stack([
71
+ param.sample(batch_size, device=self.device, dtype=self.dtype) for param in self.param_priors
72
+ ], -1)
73
+ params = self.PARAM_CLS.from_tensor(t_params)
74
+ return params
75
+
76
+ def scale_params(self, params: Params) -> Tensor:
77
+ if self.scaling_prior:
78
+ return self.scaling_prior.scale_params(params)
79
+
80
+ t_params = params.as_tensor()
81
+
82
+ scaled_params = torch.stack(
83
+ [param_prior.scale(param) for param, param_prior in zip(t_params.T, self.param_priors)], -1
84
+ )
85
+
86
+ return scaled_params
87
+
88
+ def restore_params(self, scaled_params: Tensor) -> Params:
89
+ if self.scaling_prior:
90
+ return self.scaling_prior.restore_params(scaled_params)
91
+ t_params = torch.stack(
92
+ [param_prior.restore(param) for param, param_prior in zip(scaled_params.T, self.param_priors)], -1
93
+ )
94
+ return self.PARAM_CLS.from_tensor(t_params)
95
+
96
+ def log_prob(self, params: Params) -> Tensor:
97
+ t_params = params.as_tensor()
98
+ log_probs = torch.stack(
99
+ [param_prior.log_prob(param) for param, param_prior in zip(t_params.T, self.param_priors)], -1
100
+ )
101
+ return log_probs.sum(1)
102
+
103
+ def get_indices_within_domain(self, params: Params) -> Tensor:
104
+ log_probs = self.log_prob(params)
105
+ return torch.isfinite(log_probs)
106
+
107
+ def get_indices_within_bounds(self, params: Params) -> Tensor:
108
+ return self.get_indices_within_domain(params)
109
+
110
+ def __repr__(self):
111
+ layers_num = self.PARAM_CLS.size2layers_num(len(self.param_priors))
112
+ labels = get_param_labels(layers_num)
113
+ prior_str = '\n\t'.join(f'{label}: {param_prior}' for label, param_prior in zip(labels, self.param_priors))
114
+ return f'SimplePriorSampler(\n\t{prior_str}\n)'
115
+
116
+
117
+ class UniformParamPrior(SingleParamPrior):
118
+ def __init__(self, min_value: float, max_value: float, device=None, dtype=None):
119
+ assert min_value < max_value
120
+ self.min_value, self.max_value, self.delta = min_value, max_value, max_value - min_value
121
+ self._lob_prob_const = - log(self.delta)
122
+ self.device = device
123
+ self.dtype = dtype
124
+
125
+ def sample(self, batch_num: int, device=None, dtype=None):
126
+ params = torch.rand(
127
+ batch_num, device=(device or self.device), dtype=(dtype or self.dtype)
128
+ ) * self.delta + self.min_value
129
+ return params
130
+
131
+ def log_prob(self, params: Tensor):
132
+ log_probs = torch.fill_(torch.ones_like(params), self._lob_prob_const)
133
+ log_probs[(params < self.min_value) | (params > self.max_value)] = - float('inf')
134
+ return log_probs
135
+
136
+ def scale(self, params: Tensor) -> Tensor:
137
+ return (params - self.min_value) / self.delta
138
+
139
+ def restore(self, params: Tensor) -> Tensor:
140
+ return params * self.delta + self.min_value
141
+
142
+ def __repr__(self):
143
+ return f'UniformParamPrior(min={self.min_value}, max={self.max_value})'
144
+
145
+
146
+ class GaussianParamPrior(SingleParamPrior):
147
+ _GAUSS_SCALE_CONST: float = 4.
148
+
149
+ def __init__(self, mean: float, std: float, device=None, dtype=None):
150
+ assert std > 0
151
+ self.mean = mean
152
+ self.std = std
153
+ self.device = device
154
+ self.dtype = dtype
155
+
156
+ def sample(self, batch_num: int, device=None, dtype=None):
157
+ params = torch.normal(
158
+ self.mean, self.std, (batch_num,), device=(device or self.device), dtype=(dtype or self.dtype)
159
+ )
160
+ return params
161
+
162
+ def log_prob(self, params: Tensor):
163
+ # ignore constant
164
+ log_probs = - ((params - self.mean) / self.std) ** 2 / 2
165
+ return log_probs
166
+
167
+ def scale(self, params: Tensor) -> Tensor:
168
+ return (params - self.mean) / (self.std * self._GAUSS_SCALE_CONST)
169
+
170
+ def restore(self, params: Tensor) -> Tensor:
171
+ return params * self.std * self._GAUSS_SCALE_CONST + self.mean
172
+
173
+ def __repr__(self):
174
+ return f'GaussianParamPrior(mean={self.mean}, std={self.std})'
175
+
176
+
177
+ class TruncatedGaussianParamPrior(GaussianParamPrior):
178
+ def sample(self, batch_num: int, device=None, dtype=None) -> Tensor:
179
+ params = torch.normal(
180
+ self.mean, self.std, (batch_num,), device=(device or self.device), dtype=(dtype or self.dtype)
181
+ )
182
+ negative_params = params < 0.
183
+ num_negative_params = negative_params.sum().item()
184
+ if num_negative_params:
185
+ params[negative_params] = self.sample(num_negative_params, device=device, dtype=dtype)
186
+ return params
187
+
188
+ def log_prob(self, params: Tensor) -> Tensor:
189
+ # ignore constant
190
+ log_probs: Tensor = - ((params - self.mean) / self.std) ** 2 / 2
191
+ log_probs[params < 0.] = - float('inf')
192
+ return log_probs
193
+
194
+ def __repr__(self):
195
+ return f'TruncatedGaussianParamPrior(mean={self.mean}, std={self.std})'