0

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.

1 Answers1

0

The main issue is that python does not interpret how to do list - scalar. For example:

[0.5, 0.5] - 0.5

will give you the unsupported operand type(s) for -: 'list' and 'float' error.

However, numpy arrays do understand numpy.array - scalar, so

numpy.array([0.5, 0.5]) - 0.5

will return [0, 0].

Most likely what's happening is that nevergrad is entering in a numpy array of values for the x argument.

Here's a code snippet demonstrating the above:

import nevergrad as ng
import numpy as np

def square(x):
    return sum((x - 0.5) ** 2)

# 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.49999998 0.50000004]

# x = list fails
try:
    square([0.5, 0.5])
except Exception as e:
    print(e)

# x = list fails because list - scalar isn't supported
try:
    [0.5, 0.5] - 0.5
except Exception as e:
    print(e)

print('Now we use a numpy array instead')
print(square(np.array([0.5, 0.5])))
Michael Cao
  • 2,278
  • 1
  • 1
  • 13