Skip to content

Missing raise in gradf path yields confusing shape error #343

@fcotizelati

Description

@fcotizelati

While using gradf I hit a confusing ValueError (shape mismatch) instead of the intended NotImplementedError. The code appears to intend to stop early when fixed_variables is set or the gradient length doesn’t match N, but it instantiates NotImplementedError(...) without raising it, and then fails later during the matrix multiply.

As an example:

import cma, numpy as np

def grad(x): return 2.0 * (x - 1.0)

opts = {"fixed_variables": {0: 0.0}, "verbose": -9}
es = cma.CMAEvolutionStrategy([0.1, 0.1], 0.5, opts)

es.ask(gradf=grad)  

Traceback (most recent call last):
  File "<python-input-15>", line 5, in <module>
    es.ask(gradf=grad)  # currently raises ValueError later, not
    ~~~~~~^^^^^^^^^^^^
  File "/Users/francesco/Downloads/pycma/cma/evolution_strategy.py", line 1759, in ask
    v = self.sm.D * np.dot(self.sm.B.T, self.sigma_vec * grad_at_mean)
                    ~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: shapes (1,1) and (2,) not aligned: 1 (dim 1) != 2 (dim 0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions