I am trying to learn the nevergrad library because it's used in our codebase and am following these docs.
import nevergrad as ng
def square(x, y=12):
return sum((x - 0.5) ** 2) + abs(y)
# optimization on x as an array of shape (2,)
optimizer = ng.optimizers.NGOpt(parametrization=2, budget=100)
recommendation = optimizer.minimize(square) # best value
print(recommendation.value)
# >>> [0.49971112 0.5002944 ]
The code itself works (in a little script I made that I run interactively). it spits out a value, not exactly to what they show but rounded down to [0.5, 0.5] (edit: which is clearly the min, at 0).
I can't call the square function though.
if I call square(4)
, I get the 'float object is not iterable' error because sum requires a list (oh duck typing!)
If I do square([4])
, I get unsupported operand type(s) for -: 'list' and 'float'
I thought it would do some matrix algebra - e.g. [4 -.5, 3-.5] but it just throws the above error.
I don't understand what their intentions are, for example why a square function subtracts .5 from x, but most especially how their optimization works when the underlying function doesn't work and what they want us to understand from this.
If I can't understand the most basic example, I figure I'm going to have a great deal of trouble understanding the rest.