Skip to content

Instantly share code, notes, and snippets.

@junjuew
Last active February 2, 2023 20:29
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save junjuew/19cd813bbccb7660f09b897fc260877e to your computer and use it in GitHub Desktop.
Set abseil / Tensorflow flags from python script
"""Example for passing Tensorflow configuration variables from python script.
Many tensorflow scripts variable are defined with absl, which parses inputs from
command line by default. Here is a way to set the flags in a python script.
"""
from absl import flags
FLAGS = flags.FLAGS
flags.DEFINE_integer('my_version', 0, 'Version number.')
flags.DEFINE_string('filename', None, 'Input file name.', short_name='f')
flags.register_validator('my_version',
lambda value: value % 2 == 0,
message='--my_version must be divisible by 2')
flags.mark_flag_as_required('filename')
# pass arguments into FLAGS parser
# https://github.com/abseil/abseil-py/blob/62b0407d5e6cd3912d2c7d130cffdf6613018260/absl/flags/_flagvalues.py#L595
FLAGS(['', '--filename=best'])
print(FLAGS.filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment