Skip to content

Commit

Permalink
change sparsity in gym (#1328)
Browse files Browse the repository at this point in the history
* change sparsity in gym

* fix
  • Loading branch information
teytaud authored Dec 23, 2021
1 parent 31fc166 commit 5c1c284
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions nevergrad/functions/gym/multigym.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class SmallActionSpaceLlvmEnv(gym.ActionWrapper):
]

def __init__(self, env) -> None:
"""Creating a counterpart of a compiler gym environement with a reduced action space."""
"""Creating a counterpart of a compiler gym environment with a reduced action space."""
super().__init__(env=env)
# Array for translating from this tiny action space to the action space of
# the wrapped environment.
Expand Down Expand Up @@ -643,12 +643,7 @@ def evaluation_function(self, *recommendations) -> float:
for compiler_gym_pb_index in range(23)
]
loss = -np.exp(sum(rewards) / len(rewards))
sparse_penalty = 0
if self.sparse_limit is not None: # Then we penalize the weights above the threshold "sparse_limit".
sparse_penalty = (1 + np.abs(loss)) * max(
0, np.sum(recommendations[0].value["weights"]) - self.sparse_limit
)
return loss + sparse_penalty
return loss

def forked_env(self):
assert "compiler" in self.name
Expand Down Expand Up @@ -740,9 +735,13 @@ def sparse_gym_multi_function(
) -> float:
assert all(x_ in [0, 1] for x_ in enablers)
x = weights * enablers
return self.gym_multi_function(
loss = self.gym_multi_function(
x, limited_fidelity=limited_fidelity, compiler_gym_pb_index=compiler_gym_pb_index
)
sparse_penalty = 0
if self.sparse_limit is not None: # Then we penalize the weights above the threshold "sparse_limit".
sparse_penalty = (1 + np.abs(loss)) * max(0, np.sum(enablers) - self.sparse_limit)
return loss + sparse_penalty

def gym_multi_function(
self, x: np.ndarray, limited_fidelity: bool = False, compiler_gym_pb_index: tp.Optional[int] = None
Expand Down

0 comments on commit 5c1c284

Please sign in to comment.