@@ -65,22 +65,20 @@ def apply(self, objective: Equation, arguments: dict, force_out_of_place: bool =
6565 """
6666 self_args , subop_args = self .parse_suboperator_args (arguments = arguments )
6767
68- self .suboperators ['sparsity' ].apply (objective , subop_args ['sparsity' ])
68+ if force_out_of_place :
69+ self .suboperators ['sparsity' ].apply (objective , subop_args ['sparsity' ])
6970 self .suboperators ['coeff_calc' ].apply (objective , subop_args ['coeff_calc' ])
7071
7172 _ , target , features = objective .evaluate (normalize = False , return_val = False )
72- try :
73- if features is None :
74- discr_feats = 0
75- else :
76- discr_feats = np .dot (features , objective .weights_final [:- 1 ][objective .weights_internal != 0 ])
73+ if features is None :
74+ discr_feats = 0
75+ else :
76+ discr_feats = np .dot (features , objective .weights_final [:- 1 ][objective .weights_internal != 0 ])
7777
78- discr = (discr_feats + np .full (target .shape , objective .weights_final [- 1 ]) - target )
79- self .g_fun_vals = global_var .grid_cache .g_func .reshape (- 1 )
80- discr = np .multiply (discr , self .g_fun_vals )
81- rl_error = np .linalg .norm (discr , ord = 2 )
82- except ValueError :
83- raise ValueError ('An error in getting weights ' )
78+ discr = (discr_feats + np .full (target .shape , objective .weights_final [- 1 ]) - target )
79+ self .g_fun_vals = global_var .grid_cache .g_func .reshape (- 1 )
80+ discr = np .multiply (discr , self .g_fun_vals )
81+ rl_error = np .linalg .norm (discr , ord = 2 )
8482
8583 if not (self .params ['penalty_coeff' ] > 0. and self .params ['penalty_coeff' ] < 1. ):
8684 raise ValueError ('Incorrect penalty coefficient set, value shall be in (0, 1).' )
@@ -124,20 +122,17 @@ def apply(self, objective: Equation, arguments: dict, force_out_of_place: bool =
124122 _ , target , features = objective .evaluate (normalize = False , return_val = False )
125123
126124 self .get_g_fun_vals ()
125+ data_shape = global_var .grid_cache .g_func .shape
127126
128- try :
129- if features is None :
130- discr = target - objective .weights_final [- 1 ]
131- else :
132- discr_feats = np .dot (features , objective .weights_final [:- 1 ][objective .weights_internal != 0 ])
133- discr_feats = discr_feats + objective .weights_final [- 1 ]
134- discr = discr_feats - target
127+ if features is None :
128+ discr = target - objective .weights_final [- 1 ]
129+ else :
130+ discr_feats = np .dot (features , objective .weights_final [:- 1 ][objective .weights_internal != 0 ])
131+ discr_feats = discr_feats + objective .weights_final [- 1 ]
132+ discr = discr_feats - target
135133
136- discr = np .multiply (discr , self .g_fun_vals ) / np .std (target )
137- # discr = np.multiply(discr, self.g_fun_vals) / np.linalg.norm(target, 2)
138- rl_error = np .linalg .norm (discr , 2 )
139- except ValueError :
140- raise ValueError ('An error in getting weights ' )
134+ discr = np .multiply (discr , self .g_fun_vals ) / np .std (target )
135+ rl_error = np .sqrt (np .mean (discr ** 2 ))
141136
142137 if not (self .params ['penalty_coeff' ] > 0. and self .params ['penalty_coeff' ] < 1. ):
143138 raise ValueError ('Incorrect penalty coefficient set, value shall be in (0, 1).' )
@@ -153,17 +148,17 @@ def apply(self, objective: Equation, arguments: dict, force_out_of_place: bool =
153148 # assert objective.simplified, 'Trying to evaluate not simplified equation.'
154149
155150 # Calculate r-loss
156- data_shape = global_var .grid_cache .g_func .shape
157151 target_vals = target .reshape (* data_shape )
158152 features_vals = []
159153
160154 if target_vals .ndim == 1 :
155+ horizons_default = 30
161156 window_size = len (target_vals ) // 2
162157 num_horizons = len (target_vals ) - window_size + 1
163- if num_horizons < 30 :
158+ if num_horizons < horizons_default :
164159 step_size = 1
165160 else :
166- step_size = num_horizons // 30
161+ step_size = num_horizons // horizons_default
167162 eq_window_weights = []
168163 # Compute coefficients and collect statistics over horizons
169164 if features is None :
@@ -187,21 +182,23 @@ def apply(self, objective: Equation, arguments: dict, force_out_of_place: bool =
187182 eq_window_weights .append (valuable_weights )
188183 eq_cv = np .array ([
189184 np .abs (np .std (_ )) if np .isclose (np .mean (_ ), 0 )
185+ # else np.abs(np.std(_) / np.sqrt(np.mean(np.pow(_, 2))))
190186 else np .abs (np .std (_ ) / np .mean (_ ))
191187 for _ in zip (* eq_window_weights )
192188 ])
193- lr = eq_cv .mean ()
189+ lr = eq_cv .sum ()
194190
195191 elif target_vals .ndim == 2 :
196192 lr = 0
197193 for dim in range (target_vals .ndim ):
194+ horizons_default = 30
198195 eq_window_weights = []
199196 window_size = target_vals .shape [dim ] // 2
200197 num_horizons = target_vals .shape [dim ] - window_size + 1
201- if num_horizons < 30 :
198+ if num_horizons < horizons_default :
202199 step_size = 1
203200 else :
204- step_size = num_horizons // 30
201+ step_size = num_horizons // horizons_default
205202 # Compute coefficients and collect statistics over horizons
206203 if features is None :
207204 for start_idx in range (0 , num_horizons , step_size ):
@@ -235,7 +232,7 @@ def apply(self, objective: Equation, arguments: dict, force_out_of_place: bool =
235232 else np .abs (np .std (_ ) / np .mean (_ ))
236233 for _ in zip (* eq_window_weights )
237234 ])
238- lr += eq_cv .mean ()
235+ lr += eq_cv .sum ()
239236
240237 elif target_vals .ndim == 3 :
241238 lr = 0
@@ -282,7 +279,7 @@ def apply(self, objective: Equation, arguments: dict, force_out_of_place: bool =
282279 else np .abs (np .std (_ ) / np .mean (_ ))
283280 for _ in zip (* eq_window_weights )
284281 ])
285- lr += eq_cv .mean ()
282+ lr += eq_cv .sum ()
286283
287284 objective .fitness_calculated = True
288285 objective .fitness_value = fitness_value
@@ -334,8 +331,8 @@ def apply(self, objective : SoEq, arguments : dict, force_out_of_place: bool = F
334331 net = None
335332
336333 self .set_adapter (net = net )
337-
338- self .suboperators ['sparsity' ].apply (objective , subop_args ['sparsity' ])
334+ if force_out_of_place :
335+ self .suboperators ['sparsity' ].apply (objective , subop_args ['sparsity' ])
339336 self .suboperators ['coeff_calc' ].apply (objective , subop_args ['coeff_calc' ])
340337
341338 print ('solving equation:' )
@@ -415,7 +412,8 @@ def apply(self, objective: SoEq, arguments: dict, force_out_of_place: bool = Fal
415412
416413 self .set_adapter (net = net )
417414
418- self .suboperators ['sparsity' ].apply (objective , subop_args ['sparsity' ])
415+ if force_out_of_place :
416+ self .suboperators ['sparsity' ].apply (objective , subop_args ['sparsity' ])
419417 self .suboperators ['coeff_calc' ].apply (objective , subop_args ['coeff_calc' ])
420418
421419 print ('solving equation:' )
@@ -443,7 +441,7 @@ def apply(self, objective: SoEq, arguments: dict, force_out_of_place: bool = Fal
443441 referential_data = global_var .tensor_cache .get ((eq .main_var_to_explain , (1.0 ,)))
444442 discr = solution [..., eq_idx ] - referential_data .reshape (solution [..., eq_idx ].shape )
445443 discr = np .multiply (discr , self .g_fun_vals .reshape (discr .shape )) / np .std (discr )
446- rl_error = np .linalg . norm (discr , ord = 2 )
444+ rl_error = np .sqrt ( np . mean (discr ** 2 ) )
447445
448446 print (f'fitness error is { rl_error } , while loss addition is { float (loss_add )} ' )
449447 lp = rl_error + self .params ['pinn_loss_mult' ] * float (
@@ -493,7 +491,7 @@ def apply(self, objective: SoEq, arguments: dict, force_out_of_place: bool = Fal
493491 else np .abs (np .std (_ ) / np .mean (_ ))
494492 for _ in zip (* eq_window_weights )
495493 ])
496- lr = eq_cv .mean ()
494+ lr = eq_cv .sum ()
497495
498496 elif target_vals .ndim == 2 :
499497 lr = 0
@@ -540,7 +538,7 @@ def apply(self, objective: SoEq, arguments: dict, force_out_of_place: bool = Fal
540538 else np .abs (np .std (_ ) / np .mean (_ ))
541539 for _ in zip (* eq_window_weights )
542540 ])
543- lr += eq_cv .mean ()
541+ lr += eq_cv .sum ()
544542
545543 elif target_vals .ndim == 3 :
546544 lr = 0
@@ -590,7 +588,7 @@ def apply(self, objective: SoEq, arguments: dict, force_out_of_place: bool = Fal
590588 else np .abs (np .std (_ ) / np .mean (_ ))
591589 for _ in zip (* eq_window_weights )
592590 ])
593- lr += eq_cv .mean ()
591+ lr += eq_cv .sum ()
594592
595593 eq .fitness_calculated = True
596594 eq .fitness_value = lp
0 commit comments