Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions epde/optimizers/moeadd/moeadd.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,20 @@ def delete_point(self, point):
Returns:
None
"""
duplcates_count = {i: self.population.count(i) for i in self.population}

new_levels = []
history = []
for level in self.levels:
temp = []
for element in level:
if element != point or duplcates_count.get(element) == 2 and element not in temp:
if element != point or element not in history:
temp.append(element)
history.append(element)
if not len(temp) == 0:
new_levels.append(temp)

population_cleared = []

for elem in self.population:
if elem != point or duplcates_count.get(elem) == 2 and elem not in population_cleared:
if elem != point or elem not in population_cleared:
population_cleared.append(elem)

if len(population_cleared) != sum([len(level) for level in new_levels]):
Expand Down Expand Up @@ -346,13 +345,10 @@ def __init__(self, population_instruct, weights_num, pop_size, solution_params,

self.weights = []; weights_size = len(population[0].obj_funs) #np.empty((pop_size, len(optimized_functionals)))
for weights_idx in range(weights_num):
while True:
temp_weights = self.weights_generation(weights_size, delta)
while temp_weights in self.weights:
temp_weights = self.weights_generation(weights_size, delta)
if temp_weights not in self.weights:
self.weights.append(temp_weights)
break
else:
print(temp_weights, self.weights) # Ошибка в задании obj_fun для системы уравнений
self.weights.append(temp_weights)
self.weights = np.array(self.weights)

self.neighborhood_lists = []
Expand Down Expand Up @@ -405,9 +401,9 @@ def weights_generation(weights_num, delta) -> list:
assert 1./delta == round(1./delta) # check, if 1/delta is integer number
m = np.zeros(weights_num)
for weight_idx in np.arange(weights_num):
weights[weight_idx] = np.random.choice([div_idx * delta for div_idx in np.arange(1./delta + 1e-8 - np.sum(m[:weight_idx + 1]))])
weights[weight_idx] = np.around(np.random.choice([div_idx * delta for div_idx in np.arange(1./delta + 1e-8 - np.sum(m[:weight_idx + 1]))]), 2)
m[weight_idx] = weights[weight_idx]/delta
weights[-1] = 1 - np.sum(weights[:-1])
weights[-1] = np.around(1 - np.sum(weights[:-1]), 2)

weights = np.abs(weights)
return list(weights)
Expand Down
10 changes: 6 additions & 4 deletions epde/preprocessing/smoothers.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def __call__(self, data, grid, epochs_max=1000, loss_mean=1000, loss_threshold=1
loss_fn = torch.nn.MSELoss()

# Batch size
batch_size = max(1, int(data.size * batch_frac))
batch_size = max(1, int(train_size))

# Training loop
min_val_loss = np.inf
Expand All @@ -296,8 +296,8 @@ def __call__(self, data, grid, epochs_max=1000, loss_mean=1000, loss_threshold=1

optimizer.zero_grad()
pred = model(batch_x)
loss = loss_fn(pred, batch_y)
# loss = torch.mean(torch.abs(batch_y - pred))
# loss = loss_fn(pred, batch_y)
loss = torch.mean(torch.abs(batch_y - pred))
loss.backward()
optimizer.step()
train_loss_list.append(loss.item())
Expand All @@ -307,7 +307,8 @@ def __call__(self, data, grid, epochs_max=1000, loss_mean=1000, loss_threshold=1

with torch.no_grad():
val_pred = model(val_x)
val_loss = loss_fn(val_pred, val_y).item()
val_loss = torch.mean(torch.abs(val_y - val_pred))
# val_loss = loss_fn(val_pred, val_y).item()

if epoch % 100 == 0:
print(f"Epoch {epoch:4d} | Loss: {val_loss:.6e}")
Expand All @@ -323,6 +324,7 @@ def __call__(self, data, grid, epochs_max=1000, loss_mean=1000, loss_threshold=1
# Load best model and evaluate
model.load_state_dict(best_model_state)
model.eval()
self.model = model

with torch.no_grad():
prediction = model(grid_flattened).cpu().numpy().reshape(original_shape)
Expand Down
20 changes: 14 additions & 6 deletions epde/structure/main_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,20 @@ def evaluate(self, structural, grids=None):
else:
self.prev_normalized = normalize
value = super().evaluate(structural)
if normalize and np.ndim(value) != 1:
value = normalize_ts(value)
elif normalize and np.ndim(value) == 1 and np.std(value) != 0:
value = (value - np.mean(value))/np.std(value)
elif normalize and np.ndim(value) == 1 and np.std(value) == 0:
value = (value - np.mean(value))
if normalize:
if np.ndim(value) != 1:
if len(self.structure) > 1:
value = np.ones_like(value)
for factor in self.structure:
temp = factor.evaluate()
value *= normalize_ts(temp)
else:
value = normalize_ts(value)
else:
if np.std(value) != 0:
value = (value - np.mean(value)) / np.std(value)
else:
value = (value - np.mean(value))
if np.all([len(factor.params) == 1 for factor in self.structure]) and grids is None:
# Место возможных проблем: сохранение/загрузка нормализованных данных
self.saved[normalize] = global_var.tensor_cache.add(self.cache_label, value, normalized=normalize)
Expand Down