Skip to content

Commit 26961ec

Browse files
committed
Fixed minor issues with fitness functions
1 parent 799bc66 commit 26961ec

5 files changed

Lines changed: 15 additions & 19 deletions

File tree

epde/globals.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def reset_data_repr_nn(data: List[np.ndarray], grids: List[np.ndarray], train: b
171171
t = 0
172172
min_loss = np.inf
173173
loss_mean = np.inf
174-
print(f'Training NN to represent data for {epochs_max} epochs')
174+
print(f'Training ANN to represent input data on {epochs_max} epochs:')
175175
while loss_mean > 1e-6 and t < epochs_max:
176176

177177
permutation = torch.randperm(grids_tr.size()[0])
@@ -184,8 +184,6 @@ def reset_data_repr_nn(data: List[np.ndarray], grids: List[np.ndarray], train: b
184184
indices = permutation[i:i+batch_size]
185185
batch_x, batch_y = grids_tr[indices], data_tr[indices]
186186

187-
# print(f'batch_y {batch_y.get_device()}, batch_x {batch_x.get_device()},, {next(model.parameters()).is_cuda}')
188-
# print(f'model(batch_x) {model(batch_x)}')
189187
loss = torch.mean(torch.abs(batch_y - model(batch_x)))
190188
if derivs is not None:
191189
for var_idx, deriv_axes, deriv_tensor in derivs:
@@ -194,7 +192,6 @@ def reset_data_repr_nn(data: List[np.ndarray], grids: List[np.ndarray], train: b
194192
deriv_tensor.shape)].reshape_as(deriv_autograd).to(device)
195193

196194
loss_add = 1e2 * torch.mean(torch.abs(batch_derivs - deriv_autograd))
197-
# print(loss, loss_add)
198195
loss += loss_add
199196

200197
if penalised_derivs is not None:
@@ -216,7 +213,7 @@ def reset_data_repr_nn(data: List[np.ndarray], grids: List[np.ndarray], train: b
216213
min_loss = loss_mean
217214
t += 1
218215
model = best_model
219-
print(f'min loss is {min_loss}, in last epoch: {loss_list}, ')
216+
print(f'min loss is {min_loss}, in last epoch: {loss_list}.')
220217
solution_guess_nn = best_model
221218
else:
222219
solution_guess_nn = model

epde/integrate/bop.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ def get_boundary_ind(tensor_shape, axis, rel_loc):
261261
operator.set_grid(grid=coords)
262262
operator.values = bc_values
263263
bconds.append(operator)
264-
print('Types of conds:', [type(cond) for cond in bconds])
264+
# print('Types of conds:', [type(cond) for cond in bconds])
265265
self.conditions = bconds
266266

267267

epde/integrate/pinn_integration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ def solve(self, equations: Union[List, SoEq, SolverEquation], domain: Domain,
377377
else:
378378
equations_prepared = SolverEquation()
379379
for form in equations:
380-
print(f'form is solve has a type of {type(form)}: {form}')
380+
# print(f'form is solve has a type of {type(form)}: {form}')
381381
if isinstance(form, dict):
382382
equations_prepared.add(form)
383383
elif (isinstance(form, list) or isinstance(form, tuple)) and len(form) == 2:

epde/interface/interface.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ def create_pool(self, data: Union[np.ndarray, list, tuple], variable_names=['u',
646646
derivs=None, max_deriv_order=1, additional_tokens=[],
647647
data_fun_pow: int = 1, deriv_fun_pow: int = 1, grid: list = None,
648648
data_nn: torch.nn.Sequential = None, fourier_layers: bool = True,
649-
fourier_params: dict = {'L' : [4,], 'M' : [3,]}):
649+
fourier_params: dict = {'L' : [4,], 'M' : [3,]}, ann_epochs_max = 1e5):
650650
'''
651651
Create pool of tokens to represent elementary functions, that can be included in equations.
652652
@@ -699,8 +699,8 @@ def create_pool(self, data: Union[np.ndarray, list, tuple], variable_names=['u',
699699
global_var.reset_data_repr_nn(data = data, derivs = base_derivs, train = False,
700700
grids = grid, predefined_ann = data_nn, device = self._device)
701701
else:
702-
epochs_max = 1e4 # 1e4
703-
global_var.reset_data_repr_nn(data = data, derivs = base_derivs, epochs_max=epochs_max,
702+
# epochs_max = 1e5 # 1e4
703+
global_var.reset_data_repr_nn(data = data, derivs = base_derivs, epochs_max=ann_epochs_max,
704704
grids = grid, predefined_ann = None, device = self._device,
705705
use_fourier = fourier_layers, fourier_params = fourier_params)
706706

@@ -754,7 +754,7 @@ def fit(self, data: Union[np.ndarray, list, tuple] = None, equation_terms_max_nu
754754
equation_factors_max_number=1, variable_names=['u',], eq_sparsity_interval=(1e-4, 2.5),
755755
derivs=None, max_deriv_order=1, additional_tokens = None, data_fun_pow: int = 1, deriv_fun_pow: int = 1,
756756
optimizer: Union[SimpleOptimizer, MOEADDOptimizer] = None, pool: TFPool = None,
757-
population: List[SoEq] = None, data_nn = None,
757+
population: List[SoEq] = None, data_nn = None, ann_epochs_max = 1e5,
758758
fourier_layers: bool = False, fourier_params: dict = {'L' : [4,], 'M' : [3,]}):
759759
"""
760760
Fit epde search algorithm to obtain differential equations, describing passed data.
@@ -827,7 +827,8 @@ def fit(self, data: Union[np.ndarray, list, tuple] = None, equation_terms_max_nu
827827
derivs=derivs, max_deriv_order=max_deriv_order,
828828
additional_tokens=additional_tokens,
829829
data_fun_pow = data_fun_pow, deriv_fun_pow = deriv_fun_pow,
830-
data_nn = data_nn, fourier_layers=fourier_layers, fourier_params=fourier_params)
830+
data_nn = data_nn, ann_epochs_max = ann_epochs_max,
831+
fourier_layers=fourier_layers, fourier_params=fourier_params)
831832
else:
832833
self.pool = pool; self.pool_params = cur_params
833834

epde/operators/common/fitness.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def set_adapter(self, net = None):
194194
if self.adapter is None or net is not None:
195195
compiling_params = {'mode': 'autograd', 'tol':0.01, 'lambda_bound': 100} # 'h': 1e-1
196196
optimizer_params = {}
197-
training_params = {'epochs': 4e3, 'info_string_every' : 1e3}
197+
training_params = {'epochs': 1e3, 'info_string_every' : 1e3}
198198
early_stopping_params = {'patience': 4, 'no_improvement_patience' : 250}
199199

200200
explicit_cpu = False
@@ -272,7 +272,7 @@ def set_adapter(self, net=None):
272272
if self.adapter is None or net is not None:
273273
compiling_params = {'mode': 'autograd', 'tol': 0.01, 'lambda_bound': 100} # 'h': 1e-1
274274
optimizer_params = {}
275-
training_params = {'epochs': 4e3, 'info_string_every': 1e3}
275+
training_params = {'epochs': 1e3, 'info_string_every': 1e3}
276276
early_stopping_params = {'patience': 4, 'no_improvement_patience': 250}
277277

278278
explicit_cpu = False
@@ -306,10 +306,10 @@ def apply(self, objective: SoEq, arguments: dict, force_out_of_place: bool = Fal
306306
boundary_conditions=None, use_fourier=True)
307307

308308
_, grids = global_var.grid_cache.get_all(mode='torch')
309-
309+
grids = [grid[global_var.grid_cache.g_func != 0] for grid in grids]
310310
grids = torch.stack([grid.reshape(-1) for grid in grids], dim=1).float()
311311
solution = solution_nn(grids).detach().cpu().numpy()
312-
self.g_fun_vals = global_var.grid_cache.g_func
312+
self.g_fun_vals = global_var.grid_cache.g_func[global_var.grid_cache.g_func != 0]
313313

314314
if force_out_of_place:
315315
sum_err = 0
@@ -319,8 +319,6 @@ def apply(self, objective: SoEq, arguments: dict, force_out_of_place: bool = Fal
319319
if torch.isnan(loss_add):
320320
lp = 2 * LOSS_NAN_VAL
321321
else:
322-
print(f'solution shape {solution.shape}')
323-
print(f'solution[..., eq_idx] {solution[..., eq_idx].shape}, eq_idx {eq_idx}')
324322
referential_data = global_var.tensor_cache.get((eq.main_var_to_explain, (1.0,)))
325323
discr = solution[..., eq_idx] - referential_data.reshape(solution[..., eq_idx].shape)
326324
discr = np.multiply(discr, self.g_fun_vals.reshape(discr.shape))
@@ -339,7 +337,7 @@ def apply(self, objective: SoEq, arguments: dict, force_out_of_place: bool = Fal
339337

340338
# Calculate r-loss
341339
_, target, features = eq.evaluate(normalize=False, return_val=False)
342-
data_shape = global_var.grid_cache.g_func.shape
340+
data_shape = global_var.grid_cache.g_func[global_var.grid_cache.g_func != 0].shape
343341
target_vals = target.reshape(*data_shape)
344342

345343
if target_vals.ndim == 1:

0 commit comments

Comments
 (0)