Constructs the Experiment config from Flags or equivalent object. Most of the cases, users only need to call the `parse()` function: ``` builder = ExperimentParser(FLAGS) params = builder.parse() ``` The advanced users can modify the flow by calling the parse_*() functions separately
| 298 | |
| 299 | |
| 300 | class ExperimentParser: |
| 301 | """Constructs the Experiment config from Flags or equivalent object. |
| 302 | |
| 303 | Most of the cases, users only need to call the `parse()` function: |
| 304 | ``` |
| 305 | builder = ExperimentParser(FLAGS) |
| 306 | params = builder.parse() |
| 307 | ``` |
| 308 | |
| 309 | The advanced users can modify the flow by calling the parse_*() functions |
| 310 | separately. |
| 311 | """ |
| 312 | |
| 313 | def __init__(self, flags_obj): |
| 314 | self._flags_obj = flags_obj |
| 315 | |
| 316 | def parse(self): |
| 317 | """Overrall process of constructing Experiment config.""" |
| 318 | params = self.base_experiment() |
| 319 | params = self.parse_config_file(params) |
| 320 | params = self.parse_runtime(params) |
| 321 | params = self.parse_data_service(params) |
| 322 | params = self.parse_params_override(params) |
| 323 | return params |
| 324 | |
| 325 | def base_experiment(self): |
| 326 | """Get the base experiment config from --experiment field.""" |
| 327 | if self._flags_obj.experiment is None: |
| 328 | raise ValueError('The flag --experiment must be specified.') |
| 329 | return exp_factory.get_exp_config(self._flags_obj.experiment) |
| 330 | |
| 331 | def parse_config_file(self, params): |
| 332 | """Override the configs of params from the config_file.""" |
| 333 | for config_file in self._flags_obj.config_file or []: |
| 334 | params = hyperparams.override_params_dict( |
| 335 | params, config_file, is_strict=True) |
| 336 | return params |
| 337 | |
| 338 | def parse_runtime(self, params): |
| 339 | """Override the runtime configs of params from flags.""" |
| 340 | # Override the TPU address and tf.data service address. |
| 341 | params.override({ |
| 342 | 'runtime': { |
| 343 | 'tpu': self._flags_obj.tpu, |
| 344 | }, |
| 345 | }) |
| 346 | return params |
| 347 | |
| 348 | def parse_data_service(self, params): |
| 349 | """Override the data service configs of params from flags.""" |
| 350 | if ('tf_data_service' in self._flags_obj and |
| 351 | self._flags_obj.tf_data_service and |
| 352 | isinstance(params.task, config_definitions.TaskConfig)): |
| 353 | params.override({ |
| 354 | 'task': { |
| 355 | 'train_data': { |
| 356 | 'tf_data_service_address': self._flags_obj.tf_data_service, |
| 357 | }, |
no outgoing calls
no test coverage detected