Getting `UnparsedFlagAccessError` with example code
jstmn opened this issue · comments
Hi there,
I'm trying to run the demo code from the README, but am getting an UnparsedFlagAccessError
error.
Code:
from vizier.service import clients
from vizier.service import pyvizier as vz
# Objective function to maximize.
def evaluate(w: float, x: int, y: float, z: str) -> float:
return w**2 - y**2 + x * ord(z)
# Algorithm, search space, and metrics.
study_config = vz.StudyConfig(algorithm='GAUSSIAN_PROCESS_BANDIT')
study_config.search_space.root.add_float_param('w', 0.0, 5.0)
study_config.search_space.root.add_int_param('x', -2, 2)
study_config.search_space.root.add_discrete_param('y', [0.3, 7.2])
study_config.search_space.root.add_categorical_param('z', ['a', 'g', 'k'])
study_config.metric_information.append(vz.MetricInformation('metric_name', goal=vz.ObjectiveMetricGoal.MAXIMIZE))
# Setup client and begin optimization. Vizier Service will be implicitly created.
study = clients.Study.from_study_config(study_config, owner='my_name', study_id='example')
for i in range(10):
suggestions = study.suggest(count=1)
for suggestion in suggestions:
params = suggestion.parameters
objective = evaluate(params['w'], params['x'], params['y'], params['z'])
suggestion.complete(vz.Measurement({'metric_name': objective}))
Error:
---------------------------------------------------------------------------
UnparsedFlagAccessError Traceback (most recent call last)
Cell In[21], line 20
18 study = clients.Study.from_study_config(study_config, owner='my_name', study_id='example')
19 for i in range(10):
---> 20 suggestions = study.suggest(count=1)
21 for suggestion in suggestions:
22 params = suggestion.parameters
File ~/Projects/cppflow/venv/lib/python3.8/site-packages/vizier/_src/service/clients.py:143, in Study.suggest(self, count, client_id)
138 def suggest(
139 self, *, count: Optional[int] = None, client_id: str = 'default_client_id'
140 ) -> List[Trial]:
141 return [
142 self._trial_client(t)
--> 143 for t in self._client.get_suggestions(
144 count, client_id_override=client_id
145 )
146 ]
File ~/Projects/cppflow/venv/lib/python3.8/site-packages/vizier/_src/service/vizier_client.py:168, in VizierClient.get_suggestions(self, suggestion_count, client_id_override)
165 num_attempts = 0
166 while not operation.done:
167 sleep_time = PollingDelay(
--> 168 num_attempts, FLAGS.vizier_new_suggestion_polling_secs
169 )
170 num_attempts += 1
171 logging.info(
172 'Waiting for operation with name %s to be done', operation.name
173 )
File ~/Projects/cppflow/venv/lib/python3.8/site-packages/absl/flags/_flagvalues.py:481, in FlagValues.__getattr__(self, name)
479 return fl[name].value
480 else:
--> 481 raise _exceptions.UnparsedFlagAccessError(
482 'Trying to access flag --%s before flags were parsed.' % name)
UnparsedFlagAccessError: Trying to access flag --vizier_new_suggestion_polling_secs before flags were parsed.
Specs:
- Ubuntu 20, python3.8, google-vizier=0.1.5
Any ideas on how to fix this? I looked around online but didn't find anything helpful.
Thanks,
Jeremy
The project has some hidden CLI flags that need to be parsed, setting them to a default value.
Adding the following at the top of my script worked for me.
from absl.flags import FLAGS
import sys
FLAGS(sys.argv)
Would be good to see the project either document the step or make it redundant.
Thanks it's working now. I was getting an error UnrecognizedFlagError: Unknown command line flag 'f'
when running FLAGS(sys.argv)
. My sys.argv
is ['/home/.../venv/lib/python3.8/site-packages/ipykernel_launcher.py', '-f', '/home/.../.local/share/jupyter/runtime/kernel-ead04449-484c-47c8-9f40-a6ded4d1e08a.json']
. I changed the code to the following and it worked:
from absl.flags import FLAGS
import sys
FLAGS([sys.argv[0]])
Should this change by submitted as a PR to the readme? happy to do so
Another thing, this call suggestions = study.suggest(count=1)
is hanging. Any idea why that is? Do I need to update the client_id
parameter or something?
thanks
I recall running into this as well. It fixed itself when I followed the read-the-docs documentation rather than the readme https://oss-vizier.readthedocs.io/en/latest/guides/user/running_vizier.html. Sorry, I can't remember what exactly fixed it.
This code works for me
from vizier.service import clients
from vizier.service import pyvizier as vz
from absl.flags import FLAGS
FLAGS([""])
problem = vz.ProblemStatement()
problem.search_space.root.add_float_param("x", 0.0, 1.0)
problem.search_space.root.add_float_param("y", 0.0, 1.0)
problem.metric_information.append(
vz.MetricInformation(name="maximize_metric", goal=vz.ObjectiveMetricGoal.MAXIMIZE)
)
def evaluate(x: float, y: float) -> float:
return x**2 - y**2
study_config = vz.StudyConfig.from_problem(problem)
study_config.algorithm = "QUASI_RANDOM_SEARCH"
study_client = clients.Study.from_study_config(
study_config, owner="owner", study_id="example_study_id"
)
for i in range(10):
suggestions = study_client.suggest(count=1)
for suggestion in suggestions:
x = suggestion.parameters["x"]
y = suggestion.parameters["y"]
objective = evaluate(x, y)
print(
f"Iteration {i}, suggestion ({x},{y}) led to objective value {objective}."
)
final_measurement = vz.Measurement({"maximize_metric": objective})
suggestion.complete(final_measurement)
Hi, this should be fixed now in v0.1.6. Thanks!