reflectorch 1.2.1__py3-none-any.whl → 1.4.0__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.

Potentially problematic release.


This version of reflectorch might be problematic. Click here for more details.

Files changed (41) hide show
  1. reflectorch/data_generation/__init__.py +4 -0
  2. reflectorch/data_generation/dataset.py +27 -7
  3. reflectorch/data_generation/noise.py +115 -9
  4. reflectorch/data_generation/priors/parametric_models.py +91 -16
  5. reflectorch/data_generation/priors/parametric_subpriors.py +28 -7
  6. reflectorch/data_generation/priors/sampler_strategies.py +67 -3
  7. reflectorch/data_generation/q_generator.py +97 -43
  8. reflectorch/data_generation/reflectivity/__init__.py +53 -11
  9. reflectorch/data_generation/reflectivity/kinematical.py +4 -5
  10. reflectorch/data_generation/reflectivity/smearing.py +25 -10
  11. reflectorch/data_generation/reflectivity/smearing_pointwise.py +110 -0
  12. reflectorch/data_generation/smearing.py +42 -11
  13. reflectorch/data_generation/utils.py +93 -18
  14. reflectorch/extensions/refnx/refnx_conversion.py +77 -0
  15. reflectorch/inference/inference_model.py +795 -159
  16. reflectorch/inference/loading_data.py +37 -0
  17. reflectorch/inference/plotting.py +517 -0
  18. reflectorch/inference/preprocess_exp/interpolation.py +5 -2
  19. reflectorch/inference/scipy_fitter.py +98 -7
  20. reflectorch/ml/__init__.py +2 -0
  21. reflectorch/ml/basic_trainer.py +18 -6
  22. reflectorch/ml/callbacks.py +5 -4
  23. reflectorch/ml/loggers.py +25 -0
  24. reflectorch/ml/schedulers.py +116 -0
  25. reflectorch/ml/trainers.py +131 -23
  26. reflectorch/models/__init__.py +2 -1
  27. reflectorch/models/encoders/__init__.py +2 -2
  28. reflectorch/models/encoders/conv_encoder.py +54 -40
  29. reflectorch/models/encoders/fno.py +23 -16
  30. reflectorch/models/encoders/integral_kernel_embedding.py +390 -0
  31. reflectorch/models/networks/__init__.py +2 -0
  32. reflectorch/models/networks/mlp_networks.py +331 -153
  33. reflectorch/models/networks/residual_net.py +31 -5
  34. reflectorch/runs/train.py +0 -1
  35. reflectorch/runs/utils.py +48 -11
  36. reflectorch/utils.py +30 -0
  37. {reflectorch-1.2.1.dist-info → reflectorch-1.4.0.dist-info}/METADATA +20 -17
  38. {reflectorch-1.2.1.dist-info → reflectorch-1.4.0.dist-info}/RECORD +41 -36
  39. {reflectorch-1.2.1.dist-info → reflectorch-1.4.0.dist-info}/WHEEL +1 -1
  40. {reflectorch-1.2.1.dist-info → reflectorch-1.4.0.dist-info/licenses}/LICENSE.txt +0 -0
  41. {reflectorch-1.2.1.dist-info → reflectorch-1.4.0.dist-info}/top_level.txt +0 -0
@@ -7,78 +7,83 @@ from torch import nn, cat, split, Tensor
7
7
 
8
8
  from reflectorch.models.networks.residual_net import ResidualMLP
9
9
  from reflectorch.models.encoders.conv_encoder import ConvEncoder
10
+ from reflectorch.models.encoders.integral_kernel_embedding import IntegralConvEmbedding
10
11
  from reflectorch.models.encoders.fno import FnoEncoder
11
12
  from reflectorch.models.activations import activation_by_name
12
13
 
13
- class NetworkWithPriorsConvEmb(nn.Module):
14
- """MLP network with 1D CNN embedding network
14
+ class NetworkWithPriors(nn.Module):
15
+ """MLP network with an embedding network
15
16
 
16
17
  .. image:: ../documentation/FigureReflectometryNetwork.png
17
18
  :width: 800px
18
19
  :align: center
19
20
 
20
21
  Args:
21
- in_channels (int, optional): the number of input channels of the 1D CNN. Defaults to 1.
22
- hidden_channels (tuple, optional): list with the number of channels for each layer of the 1D CNN. Defaults to (32, 64, 128, 256, 512).
23
- dim_embedding (int, optional): the dimension of the embedding produced by the 1D CNN. Defaults to 128.
24
- dim_avpool (int, optional): the type of activation function in the 1D CNN. Defaults to 1.
25
- embedding_net_activation (str, optional): the type of activation function in the 1D CNN. Defaults to 'gelu'.
26
- use_batch_norm (bool, optional): whether to use batch normalization (in both the 1D CNN and the MLP). Defaults to False.
22
+ embedding_net_type (str): the type of embedding network, either 'conv', 'fno' or 'integral_conv'.
23
+ embedding_net_kwargs (dict): dictionary containing the keyword arguments for the embedding network.
27
24
  dim_out (int, optional): the dimension of the output produced by the MLP. Defaults to 8.
25
+ dim_conditioning_params (int, optional): the dimension of other parameters the network is conditioned on (e.g. for the smearing coefficient dq/q)
28
26
  layer_width (int, optional): the width of a linear layer in the MLP. Defaults to 512.
29
27
  num_blocks (int, optional): the number of residual blocks in the MLP. Defaults to 4.
30
28
  repeats_per_block (int, optional): the number of normalization/activation/linear repeats in a block. Defaults to 2.
31
29
  mlp_activation (str, optional): the type of activation function in the MLP. Defaults to 'gelu'.
30
+ use_batch_norm (bool, optional): whether to use batch normalization in the MLP. Defaults to True.
31
+ use_layer_norm (bool, optional): whether to use layer normalization in the MLP (if use_batch_norm is False). Defaults to False.
32
32
  dropout_rate (float, optional): dropout rate for each block. Defaults to 0.0.
33
+ tanh_output (bool, optional): whether to apply a tanh function to the output. Defaults to False.
33
34
  use_selu_init (bool, optional): whether to use the special weights initialization for the 'selu' activation function. Defaults to False.
34
35
  pretrained_embedding_net (str, optional): the path to the weights of a pretrained embedding network. Defaults to None.
35
36
  residual (bool, optional): whether the blocks have a residual skip connection. Defaults to True.
36
37
  adaptive_activation (bool, optional): must be set to ``True`` if the activation function is adaptive. Defaults to False.
37
38
  conditioning (str, optional): the manner in which the prior bounds are provided as input to the network. Defaults to 'concat'.
38
- """
39
+ """
39
40
  def __init__(self,
40
- in_channels: int = 1,
41
- hidden_channels: tuple = (32, 64, 128, 256, 512),
42
- dim_embedding: int = 128,
43
- dim_avpool: int = 1,
44
- embedding_net_activation: str = 'gelu',
45
- use_batch_norm: bool = False,
41
+ embedding_net_type: str, # 'conv', 'fno'
42
+ embedding_net_kwargs: dict,
43
+ pretrained_embedding_net: str = None,
46
44
  dim_out: int = 8,
45
+ dim_conditioning_params: int = 0,
47
46
  layer_width: int = 512,
48
47
  num_blocks: int = 4,
49
48
  repeats_per_block: int = 2,
50
49
  mlp_activation: str = 'gelu',
50
+ use_batch_norm: bool = True,
51
+ use_layer_norm: bool = False,
51
52
  dropout_rate: float = 0.0,
53
+ tanh_output: bool = False,
52
54
  use_selu_init: bool = False,
53
- pretrained_embedding_net: str = None,
54
55
  residual: bool = True,
55
56
  adaptive_activation: bool = False,
56
57
  conditioning: str = 'concat',
57
- ):
58
+ concat_condition_first_layer: bool = True):
58
59
  super().__init__()
59
60
 
60
- self.in_channels = in_channels
61
61
  self.conditioning = conditioning
62
-
63
- self.embedding_net = ConvEncoder(
64
- in_channels=in_channels,
65
- hidden_channels=hidden_channels,
66
- dim_latent=dim_embedding,
67
- dim_avpool=dim_avpool,
68
- use_batch_norm=use_batch_norm,
69
- activation=embedding_net_activation
70
- )
71
-
72
62
  self.dim_prior_bounds = 2 * dim_out
63
+ self.dim_conditioning_params = dim_conditioning_params
64
+ self.tanh_output = tanh_output
65
+
66
+ if embedding_net_type == 'conv':
67
+ self.embedding_net = ConvEncoder(**embedding_net_kwargs)
68
+ elif embedding_net_type == 'fno':
69
+ self.embedding_net = FnoEncoder(**embedding_net_kwargs)
70
+ elif embedding_net_type == 'integral_conv':
71
+ self.embedding_net = IntegralConvEmbedding(**embedding_net_kwargs)
72
+ elif embedding_net_type == 'no_embedding_net':
73
+ self.embedding_net = nn.Identity()
74
+ else:
75
+ raise ValueError(f"Unsupported embedding_net_type: {embedding_net_type}")
76
+
77
+ self.dim_embedding = embedding_net_kwargs['dim_embedding']
73
78
 
74
79
  if conditioning == 'concat':
75
- dim_mlp_in = dim_embedding + self.dim_prior_bounds
80
+ dim_mlp_in = self.dim_embedding + self.dim_prior_bounds + self.dim_conditioning_params
76
81
  dim_condition = 0
77
82
  elif conditioning == 'glu' or conditioning == 'film':
78
- dim_mlp_in = dim_embedding
79
- dim_condition = self.dim_prior_bounds
83
+ dim_mlp_in = self.dim_embedding
84
+ dim_condition = self.dim_prior_bounds + self.dim_conditioning_params
80
85
  else:
81
- raise NotImplementedError
86
+ raise NotImplementedError(f"Conditioning type '{conditioning}' is not supported.")
82
87
 
83
88
  self.mlp = ResidualMLP(
84
89
  dim_in=dim_mlp_in,
@@ -89,15 +94,16 @@ class NetworkWithPriorsConvEmb(nn.Module):
89
94
  repeats_per_block=repeats_per_block,
90
95
  activation=mlp_activation,
91
96
  use_batch_norm=use_batch_norm,
97
+ use_layer_norm=use_layer_norm,
92
98
  dropout_rate=dropout_rate,
93
99
  residual=residual,
94
100
  adaptive_activation=adaptive_activation,
95
101
  conditioning=conditioning,
102
+ concat_condition_first_layer=concat_condition_first_layer,
96
103
  )
97
104
 
98
- if use_selu_init and embedding_net_activation == 'selu':
105
+ if use_selu_init and embedding_net_kwargs.get('activation', None) == 'selu':
99
106
  self.embedding_net.apply(selu_init)
100
-
101
107
  if use_selu_init and mlp_activation == 'selu':
102
108
  self.mlp.apply(selu_init)
103
109
 
@@ -105,142 +111,314 @@ class NetworkWithPriorsConvEmb(nn.Module):
105
111
  self.embedding_net.load_weights(pretrained_embedding_net)
106
112
 
107
113
 
108
- def forward(self, curves: Tensor, bounds: Tensor, q_values: Optional[Tensor] = None):
114
+ def forward(self, curves, bounds, q_values=None, conditioning_params=None, key_padding_mask=None, unscaled_q_values=None):
109
115
  """
110
116
  Args:
111
- curves (Tensor): reflectivity curves
112
- bounds (Tensor): prior bounds
113
- q_values (Tensor, optional): q values. Defaults to None.
114
-
115
- Returns:
116
- Tensor: prediction
117
+ scaled_curves (torch.Tensor): Input tensor of shape [batch_size, n_points] or [batch_size, n_channels, n_points].
118
+ scaled_bounds (torch.Tensor): Tensor representing prior bounds, shape [batch_size, 2*n_params].
119
+ scaled_q_values (torch.Tensor, optional): Tensor of shape [batch_size, n_points].
120
+ scaled_conditioning_params (torch.Tensor, optional): Additional parameters for conditioning, shape [batch_size, ...].
117
121
  """
118
- if q_values is not None:
119
- curves = torch.cat([curves[:, None, :], q_values[:, None, :]], dim=1)
120
-
121
- if self.conditioning == 'concat':
122
- x = torch.cat([self.embedding_net(curves), bounds], dim=-1)
123
- x = self.mlp(x)
124
122
 
125
- elif self.conditioning == 'glu' or self.conditioning == 'film':
126
- x = self.mlp(self.embedding_net(curves), condition=bounds)
123
+ if curves.dim() == 2:
124
+ curves = curves.unsqueeze(1)
127
125
 
128
- return x
129
-
130
-
131
- class NetworkWithPriorsFnoEmb(nn.Module):
132
- """MLP network with FNO embedding network
126
+ additional_channels = []
127
+ if q_values is not None and not isinstance(self.embedding_net, IntegralConvEmbedding):
128
+ additional_channels.append(q_values.unsqueeze(1))
133
129
 
134
- Args:
135
- in_channels (int, optional): the number of input channels to the FNO-based embedding network. Defaults to 2.
136
- dim_embedding (int, optional): the dimension of the embedding produced by the FNO. Defaults to 128.
137
- modes (int, optional): the number of Fourier modes that are utilized. Defaults to 16.
138
- width_fno (int, optional): the number of channels in the FNO blocks. Defaults to 64.
139
- embedding_net_activation (str, optional): the type of activation function in the embedding network. Defaults to 'gelu'.
140
- n_fno_blocks (int, optional): the number of FNO blocks. Defaults to 6.
141
- fusion_self_attention (bool, optional): if ``True`` a fusion layer is used after the FNO blocks to produce the final output. Defaults to False.
142
- dim_out (int, optional): the dimension of the output produced by the MLP. Defaults to 8.
143
- layer_width (int, optional): the width of a linear layer in the MLP. Defaults to 512.
144
- num_blocks (int, optional): the number of residual blocks in the MLP. Defaults to 4.
145
- repeats_per_block (int, optional): the number of normalization/activation/linear repeats in a block. Defaults to 2.
146
- use_batch_norm (bool, optional): whether to use batch normalization (only in the MLP). Defaults to False.
147
- mlp_activation (str, optional): the type of activation function in the MLP. Defaults to 'gelu'.
148
- dropout_rate (float, optional): dropout rate for each block. Defaults to 0.0.
149
- use_selu_init (bool, optional): whether to use the special weights initialization for the 'selu' activation function. Defaults to False.
150
- residual (bool, optional): whether the blocks have a residual skip connection. Defaults to True.
151
- adaptive_activation (bool, optional): must be set to ``True`` if the activation function is adaptive. Defaults to False.
152
- conditioning (str, optional): the manner in which the prior bounds are provided as input to the network. Defaults to 'concat'.
153
- """
154
- def __init__(self,
155
- in_channels: int = 2,
156
- dim_embedding: int = 128,
157
- modes: int = 16,
158
- width_fno: int = 64,
159
- embedding_net_activation: str = 'gelu',
160
- n_fno_blocks : int = 6,
161
- fusion_self_attention: bool = False,
162
- dim_out: int = 8,
163
- layer_width: int = 512,
164
- num_blocks: int = 4,
165
- repeats_per_block: int = 2,
166
- use_batch_norm: bool = False,
167
- mlp_activation: str = 'gelu',
168
- dropout_rate: float = 0.0,
169
- use_selu_init: bool = False,
170
- residual: bool = True,
171
- adaptive_activation: bool = False,
172
- conditioning: str = 'concat',
173
- ):
174
- super().__init__()
130
+ if additional_channels:
131
+ curves = torch.cat([curves] + additional_channels, dim=1) # [batch_size, n_channels, n_points]
175
132
 
176
- self.conditioning = conditioning
133
+ if isinstance(self.embedding_net, IntegralConvEmbedding):
134
+ x = self.embedding_net(q=unscaled_q_values.float(), y=curves.permute(0, 2, 1), drop_mask=key_padding_mask)
135
+ else:
136
+ x = self.embedding_net(curves)
177
137
 
178
- self.embedding_net = FnoEncoder(
179
- ch_in=in_channels,
180
- dim_embedding=dim_embedding,
181
- modes=modes,
182
- width_fno=width_fno,
183
- n_fno_blocks=n_fno_blocks,
184
- activation=embedding_net_activation,
185
- fusion_self_attention=fusion_self_attention
186
- )
138
+ if self.conditioning == 'concat':
139
+ x = torch.cat([x, bounds] + ([conditioning_params] if conditioning_params is not None else []), dim=-1)
140
+ x = self.mlp(x)
187
141
 
188
- self.dim_prior_bounds = 2 * dim_out
142
+ elif self.conditioning in ['glu', 'film']:
143
+ condition = torch.cat([bounds] + ([conditioning_params] if conditioning_params is not None else []), dim=-1)
144
+ x = self.mlp(x, condition=condition)
189
145
 
190
- if conditioning == 'concat':
191
- dim_mlp_in = dim_embedding + self.dim_prior_bounds
192
- dim_condition = 0
193
- elif conditioning == 'glu' or conditioning == 'film':
194
- dim_mlp_in = dim_embedding
195
- dim_condition = self.dim_prior_bounds
196
146
  else:
197
- raise NotImplementedError
147
+ raise NotImplementedError(f"Conditioning type {self.conditioning} not recognized.")
198
148
 
199
- self.mlp = ResidualMLP(
200
- dim_in=dim_mlp_in,
201
- dim_out=dim_out,
202
- dim_condition=dim_condition,
203
- layer_width=layer_width,
204
- num_blocks=num_blocks,
205
- repeats_per_block=repeats_per_block,
206
- activation=mlp_activation,
207
- use_batch_norm=use_batch_norm,
208
- dropout_rate=dropout_rate,
209
- residual=residual,
210
- adaptive_activation=adaptive_activation,
211
- conditioning=conditioning,
212
- )
213
-
214
- if use_selu_init and embedding_net_activation == 'selu':
215
- self.FnoEncoder.apply(selu_init)
216
-
217
- if use_selu_init and mlp_activation == 'selu':
218
- self.mlp.apply(selu_init)
149
+ if self.tanh_output:
150
+ x = torch.tanh(x)
219
151
 
220
-
221
- def forward(self, curves: Tensor, bounds: Tensor, q_values: Optional[Tensor] =None):
222
- """
223
- Args:
224
- curves (Tensor): reflectivity curves
225
- bounds (Tensor): prior bounds
226
- q_values (Tensor, optional): q values. Defaults to None.
152
+ return x
227
153
 
228
- Returns:
229
- Tensor: prediction
230
- """
231
- if curves.dim() < 3:
232
- curves = curves[:, None, :]
233
- if q_values is not None:
234
- curves = torch.cat([curves, q_values[:, None, :]], dim=1)
154
+ class NetworkWithPriorsConvEmb(NetworkWithPriors):
155
+ """Wrapper for back-compatibility with previous versions of the package"""
156
+ def __init__(self, **kwargs):
157
+ embedding_net_kwargs = {
158
+ 'in_channels': kwargs.pop('in_channels', 1),
159
+ 'hidden_channels': kwargs.pop('hidden_channels', [32, 64, 128, 256, 512]),
160
+ 'dim_embedding': kwargs.pop('dim_embedding', 128),
161
+ 'dim_avpool': kwargs.pop('dim_avpool', 1),
162
+ 'activation': kwargs.pop('embedding_net_activation', 'gelu'),
163
+ 'use_batch_norm': kwargs.pop('use_batch_norm', False),
164
+ }
165
+
166
+ super().__init__(
167
+ embedding_net_type='conv',
168
+ embedding_net_kwargs=embedding_net_kwargs,
169
+ **kwargs
170
+ )
235
171
 
236
- if self.conditioning == 'concat':
237
- x = torch.cat([self.embedding_net(curves), bounds], dim=-1)
238
- x = self.mlp(x)
172
+ class NetworkWithPriorsFnoEmb(NetworkWithPriors):
173
+ """Wrapper for back-compatibility with previous versions of the package"""
174
+ def __init__(self, **kwargs):
175
+ embedding_net_kwargs = {
176
+ 'in_channels': kwargs.pop('in_channels', 2),
177
+ 'dim_embedding': kwargs.pop('dim_embedding', 128),
178
+ 'modes': kwargs.pop('modes', 16),
179
+ 'width_fno': kwargs.pop('width_fno', 64),
180
+ 'n_fno_blocks': kwargs.pop('n_fno_blocks', 6),
181
+ 'activation': kwargs.pop('embedding_net_activation', 'gelu'),
182
+ 'fusion_self_attention': kwargs.pop('fusion_self_attention', False),
183
+ }
184
+
185
+ super().__init__(
186
+ embedding_net_type='fno',
187
+ embedding_net_kwargs=embedding_net_kwargs,
188
+ **kwargs
189
+ )
239
190
 
240
- elif self.conditioning == 'glu' or self.conditioning == 'film':
241
- x = self.mlp(self.embedding_net(curves), condition=bounds)
191
+ # class NetworkWithPriorsConvEmb(nn.Module):
192
+ # """MLP network with 1D CNN embedding network
193
+
194
+ # .. image:: ../documentation/FigureReflectometryNetwork.png
195
+ # :width: 800px
196
+ # :align: center
197
+
198
+ # Args:
199
+ # in_channels (int, optional): the number of input channels of the 1D CNN. Defaults to 1.
200
+ # hidden_channels (tuple, optional): list with the number of channels for each layer of the 1D CNN. Defaults to (32, 64, 128, 256, 512).
201
+ # dim_embedding (int, optional): the dimension of the embedding produced by the 1D CNN. Defaults to 128.
202
+ # dim_avpool (int, optional): the type of activation function in the 1D CNN. Defaults to 1.
203
+ # embedding_net_activation (str, optional): the type of activation function in the 1D CNN. Defaults to 'gelu'.
204
+ # use_batch_norm (bool, optional): whether to use batch normalization (in both the 1D CNN and the MLP). Defaults to False.
205
+ # dim_out (int, optional): the dimension of the output produced by the MLP. Defaults to 8.
206
+ # layer_width (int, optional): the width of a linear layer in the MLP. Defaults to 512.
207
+ # num_blocks (int, optional): the number of residual blocks in the MLP. Defaults to 4.
208
+ # repeats_per_block (int, optional): the number of normalization/activation/linear repeats in a block. Defaults to 2.
209
+ # mlp_activation (str, optional): the type of activation function in the MLP. Defaults to 'gelu'.
210
+ # dropout_rate (float, optional): dropout rate for each block. Defaults to 0.0.
211
+ # use_selu_init (bool, optional): whether to use the special weights initialization for the 'selu' activation function. Defaults to False.
212
+ # pretrained_embedding_net (str, optional): the path to the weights of a pretrained embedding network. Defaults to None.
213
+ # residual (bool, optional): whether the blocks have a residual skip connection. Defaults to True.
214
+ # adaptive_activation (bool, optional): must be set to ``True`` if the activation function is adaptive. Defaults to False.
215
+ # conditioning (str, optional): the manner in which the prior bounds are provided as input to the network. Defaults to 'concat'.
216
+ # """
217
+ # def __init__(self,
218
+ # in_channels: int = 1,
219
+ # hidden_channels: tuple = (32, 64, 128, 256, 512),
220
+ # dim_embedding: int = 128,
221
+ # dim_avpool: int = 1,
222
+ # embedding_net_activation: str = 'gelu',
223
+ # use_batch_norm: bool = False,
224
+ # dim_out: int = 8,
225
+ # layer_width: int = 512,
226
+ # num_blocks: int = 4,
227
+ # repeats_per_block: int = 2,
228
+ # mlp_activation: str = 'gelu',
229
+ # dropout_rate: float = 0.0,
230
+ # use_selu_init: bool = False,
231
+ # pretrained_embedding_net: str = None,
232
+ # residual: bool = True,
233
+ # adaptive_activation: bool = False,
234
+ # conditioning: str = 'concat',
235
+ # ):
236
+ # super().__init__()
237
+
238
+ # self.in_channels = in_channels
239
+ # self.conditioning = conditioning
240
+
241
+ # self.embedding_net = ConvEncoder(
242
+ # in_channels=in_channels,
243
+ # hidden_channels=hidden_channels,
244
+ # dim_latent=dim_embedding,
245
+ # dim_avpool=dim_avpool,
246
+ # use_batch_norm=use_batch_norm,
247
+ # activation=embedding_net_activation
248
+ # )
249
+
250
+ # self.dim_prior_bounds = 2 * dim_out
251
+
252
+ # if conditioning == 'concat':
253
+ # dim_mlp_in = dim_embedding + self.dim_prior_bounds
254
+ # dim_condition = 0
255
+ # elif conditioning == 'glu' or conditioning == 'film':
256
+ # dim_mlp_in = dim_embedding
257
+ # dim_condition = self.dim_prior_bounds
258
+ # else:
259
+ # raise NotImplementedError
260
+
261
+ # self.mlp = ResidualMLP(
262
+ # dim_in=dim_mlp_in,
263
+ # dim_out=dim_out,
264
+ # dim_condition=dim_condition,
265
+ # layer_width=layer_width,
266
+ # num_blocks=num_blocks,
267
+ # repeats_per_block=repeats_per_block,
268
+ # activation=mlp_activation,
269
+ # use_batch_norm=use_batch_norm,
270
+ # dropout_rate=dropout_rate,
271
+ # residual=residual,
272
+ # adaptive_activation=adaptive_activation,
273
+ # conditioning=conditioning,
274
+ # )
275
+
276
+ # if use_selu_init and embedding_net_activation == 'selu':
277
+ # self.embedding_net.apply(selu_init)
278
+
279
+ # if use_selu_init and mlp_activation == 'selu':
280
+ # self.mlp.apply(selu_init)
281
+
282
+ # if pretrained_embedding_net:
283
+ # self.embedding_net.load_weights(pretrained_embedding_net)
284
+
285
+
286
+ # def forward(self, curves: Tensor, bounds: Tensor, q_values: Optional[Tensor] = None):
287
+ # """
288
+ # Args:
289
+ # curves (Tensor): reflectivity curves
290
+ # bounds (Tensor): prior bounds
291
+ # q_values (Tensor, optional): q values. Defaults to None.
292
+
293
+ # Returns:
294
+ # Tensor: prediction
295
+ # """
296
+ # if q_values is not None:
297
+ # curves = torch.cat([curves[:, None, :], q_values[:, None, :]], dim=1)
298
+
299
+ # if self.conditioning == 'concat':
300
+ # x = torch.cat([self.embedding_net(curves), bounds], dim=-1)
301
+ # x = self.mlp(x)
302
+
303
+ # elif self.conditioning == 'glu' or self.conditioning == 'film':
304
+ # x = self.mlp(self.embedding_net(curves), condition=bounds)
305
+
306
+ # return x
307
+
308
+
309
+ # class NetworkWithPriorsFnoEmb(nn.Module):
310
+ # """MLP network with FNO embedding network
311
+
312
+ # Args:
313
+ # in_channels (int, optional): the number of input channels to the FNO-based embedding network. Defaults to 2.
314
+ # dim_embedding (int, optional): the dimension of the embedding produced by the FNO. Defaults to 128.
315
+ # modes (int, optional): the number of Fourier modes that are utilized. Defaults to 16.
316
+ # width_fno (int, optional): the number of channels in the FNO blocks. Defaults to 64.
317
+ # embedding_net_activation (str, optional): the type of activation function in the embedding network. Defaults to 'gelu'.
318
+ # n_fno_blocks (int, optional): the number of FNO blocks. Defaults to 6.
319
+ # fusion_self_attention (bool, optional): if ``True`` a fusion layer is used after the FNO blocks to produce the final output. Defaults to False.
320
+ # dim_out (int, optional): the dimension of the output produced by the MLP. Defaults to 8.
321
+ # layer_width (int, optional): the width of a linear layer in the MLP. Defaults to 512.
322
+ # num_blocks (int, optional): the number of residual blocks in the MLP. Defaults to 4.
323
+ # repeats_per_block (int, optional): the number of normalization/activation/linear repeats in a block. Defaults to 2.
324
+ # use_batch_norm (bool, optional): whether to use batch normalization (only in the MLP). Defaults to False.
325
+ # mlp_activation (str, optional): the type of activation function in the MLP. Defaults to 'gelu'.
326
+ # dropout_rate (float, optional): dropout rate for each block. Defaults to 0.0.
327
+ # use_selu_init (bool, optional): whether to use the special weights initialization for the 'selu' activation function. Defaults to False.
328
+ # residual (bool, optional): whether the blocks have a residual skip connection. Defaults to True.
329
+ # adaptive_activation (bool, optional): must be set to ``True`` if the activation function is adaptive. Defaults to False.
330
+ # conditioning (str, optional): the manner in which the prior bounds are provided as input to the network. Defaults to 'concat'.
331
+ # """
332
+ # def __init__(self,
333
+ # in_channels: int = 2,
334
+ # dim_embedding: int = 128,
335
+ # modes: int = 16,
336
+ # width_fno: int = 64,
337
+ # embedding_net_activation: str = 'gelu',
338
+ # n_fno_blocks : int = 6,
339
+ # fusion_self_attention: bool = False,
340
+ # dim_out: int = 8,
341
+ # layer_width: int = 512,
342
+ # num_blocks: int = 4,
343
+ # repeats_per_block: int = 2,
344
+ # use_batch_norm: bool = False,
345
+ # mlp_activation: str = 'gelu',
346
+ # dropout_rate: float = 0.0,
347
+ # use_selu_init: bool = False,
348
+ # residual: bool = True,
349
+ # adaptive_activation: bool = False,
350
+ # conditioning: str = 'concat',
351
+ # ):
352
+ # super().__init__()
353
+
354
+ # self.conditioning = conditioning
355
+
356
+ # self.embedding_net = FnoEncoder(
357
+ # ch_in=in_channels,
358
+ # dim_embedding=dim_embedding,
359
+ # modes=modes,
360
+ # width_fno=width_fno,
361
+ # n_fno_blocks=n_fno_blocks,
362
+ # activation=embedding_net_activation,
363
+ # fusion_self_attention=fusion_self_attention
364
+ # )
365
+
366
+ # self.dim_prior_bounds = 2 * dim_out
367
+
368
+ # if conditioning == 'concat':
369
+ # dim_mlp_in = dim_embedding + self.dim_prior_bounds
370
+ # dim_condition = 0
371
+ # elif conditioning == 'glu' or conditioning == 'film':
372
+ # dim_mlp_in = dim_embedding
373
+ # dim_condition = self.dim_prior_bounds
374
+ # else:
375
+ # raise NotImplementedError
376
+
377
+ # self.mlp = ResidualMLP(
378
+ # dim_in=dim_mlp_in,
379
+ # dim_out=dim_out,
380
+ # dim_condition=dim_condition,
381
+ # layer_width=layer_width,
382
+ # num_blocks=num_blocks,
383
+ # repeats_per_block=repeats_per_block,
384
+ # activation=mlp_activation,
385
+ # use_batch_norm=use_batch_norm,
386
+ # dropout_rate=dropout_rate,
387
+ # residual=residual,
388
+ # adaptive_activation=adaptive_activation,
389
+ # conditioning=conditioning,
390
+ # )
391
+
392
+ # if use_selu_init and embedding_net_activation == 'selu':
393
+ # self.FnoEncoder.apply(selu_init)
394
+
395
+ # if use_selu_init and mlp_activation == 'selu':
396
+ # self.mlp.apply(selu_init)
242
397
 
243
- return x
398
+
399
+ # def forward(self, curves: Tensor, bounds: Tensor, q_values: Optional[Tensor] =None):
400
+ # """
401
+ # Args:
402
+ # curves (Tensor): reflectivity curves
403
+ # bounds (Tensor): prior bounds
404
+ # q_values (Tensor, optional): q values. Defaults to None.
405
+
406
+ # Returns:
407
+ # Tensor: prediction
408
+ # """
409
+ # if curves.dim() < 3:
410
+ # curves = curves[:, None, :]
411
+ # if q_values is not None:
412
+ # curves = torch.cat([curves, q_values[:, None, :]], dim=1)
413
+
414
+ # if self.conditioning == 'concat':
415
+ # x = torch.cat([self.embedding_net(curves), bounds], dim=-1)
416
+ # x = self.mlp(x)
417
+
418
+ # elif self.conditioning == 'glu' or self.conditioning == 'film':
419
+ # x = self.mlp(self.embedding_net(curves), condition=bounds)
420
+
421
+ # return x
244
422
 
245
423
 
246
424