def define_train_higgs_flags(): """Add tree related flags as well as training/eval configuration.""" flags_core.define_base(stop_threshold=False, batch_size=False, num_gpu=False) flags.adopt_module_key_flags(flags_core) flags.DEFINE_integer( name='train_start', default=0, help=help_wrap('Start index of train examples within the data.')) flags.DEFINE_integer( name='train_count', default=1000000, help=help_wrap('Number of train examples within the data.')) flags.DEFINE_integer( name='eval_start', default=10000000, help=help_wrap('Start index of eval examples within the data.')) flags.DEFINE_integer( name='eval_count', default=1000000, help=help_wrap('Number of eval examples within the data.')) flags.DEFINE_integer( 'n_trees', default=100, help=help_wrap('Number of trees to build.')) flags.DEFINE_integer( 'max_depth', default=6, help=help_wrap('Maximum depths of each tree.')) flags.DEFINE_float( 'learning_rate', default=0.1, help=help_wrap('Maximum depths of each tree.')) flags_core.set_defaults(data_dir='/tmp/higgs_data', model_dir='/tmp/higgs_model')
def define_train_higgs_flags(): """Add tree related flags as well as training/eval configuration.""" flags_core.define_base(clean=False, stop_threshold=False, batch_size=False, num_gpu=False) flags_core.define_benchmark() flags.adopt_module_key_flags(flags_core) flags.DEFINE_integer( name="train_start", default=0, help=help_wrap("Start index of train examples within the data.")) flags.DEFINE_integer( name="train_count", default=1000000, help=help_wrap("Number of train examples within the data.")) flags.DEFINE_integer( name="eval_start", default=10000000, help=help_wrap("Start index of eval examples within the data.")) flags.DEFINE_integer( name="eval_count", default=1000000, help=help_wrap("Number of eval examples within the data.")) flags.DEFINE_integer( "n_trees", default=100, help=help_wrap("Number of trees to build.")) flags.DEFINE_integer( "max_depth", default=6, help=help_wrap("Maximum depths of each tree.")) flags.DEFINE_float( "learning_rate", default=0.1, help=help_wrap("The learning rate.")) flags_core.set_defaults(data_dir="/tmp/higgs_data", model_dir="/tmp/higgs_model")
def define_keras_benchmark_flags(): """Add flags for keras built-in application models.""" flags_core.define_base(hooks=False) flags_core.define_performance() flags_core.define_image() flags_core.define_benchmark() flags.adopt_module_key_flags(flags_core) flags_core.set_defaults( data_format="channels_last", use_synthetic_data=True, batch_size=32, train_epochs=2) flags.DEFINE_enum( name="model", default=None, enum_values=MODELS.keys(), case_sensitive=False, help=flags_core.help_wrap( "Model to be benchmarked.")) flags.DEFINE_list( name="callbacks", default=["ExamplesPerSecondCallback", "LoggingMetricCallback"], help=flags_core.help_wrap( "A list of (case insensitive) strings to specify the names of " "callbacks. For example: `--callbacks ExamplesPerSecondCallback," "LoggingMetricCallback`"))
def define_keras_benchmark_flags(): """Add flags for keras built-in application models.""" flags_core.define_base(hooks=False) flags_core.define_performance() flags_core.define_image() flags_core.define_benchmark() flags.adopt_module_key_flags(flags_core) flags_core.set_defaults( data_format="channels_last", use_synthetic_data=True, batch_size=32, train_epochs=2) flags.DEFINE_enum( name="model", default=None, enum_values=MODELS.keys(), case_sensitive=False, help=flags_core.help_wrap( "Model to be benchmarked.")) flags.DEFINE_integer( name="num_train_images", default=1000, help=flags_core.help_wrap( "The number of synthetic images for training. The default value is " "1000.")) flags.DEFINE_integer( name="num_eval_images", default=50, help=flags_core.help_wrap( "The number of synthetic images for evaluation. The default value is " "50.")) flags.DEFINE_boolean( name="eager", default=False, help=flags_core.help_wrap( "To enable eager execution. Note that if eager execution is enabled, " "only one GPU is utilized even if multiple GPUs are provided and " "multi_gpu_model is used.")) flags.DEFINE_boolean( name="dist_strat", default=False, help=flags_core.help_wrap( "To enable distribution strategy for model training and evaluation. " "Number of GPUs used for distribution strategy can be set by the " "argument --num_gpus.")) flags.DEFINE_list( name="callbacks", default=["ExamplesPerSecondCallback", "LoggingMetricCallback"], help=flags_core.help_wrap( "A list of (case insensitive) strings to specify the names of " "callbacks. For example: `--callbacks ExamplesPerSecondCallback," "LoggingMetricCallback`")) @flags.multi_flags_validator( ["eager", "dist_strat"], message="Both --eager and --dist_strat were set. Only one can be " "defined, as DistributionStrategy is not supported in Eager " "execution currently.") # pylint: disable=unused-variable def _check_eager_dist_strat(flag_dict): return not(flag_dict["eager"] and flag_dict["dist_strat"])
def define_mnist_eager_flags(): """Defined flags and defaults for MNIST in eager mode.""" flags_core.define_base_eager() flags_core.define_image() flags.adopt_module_key_flags(flags_core) flags.DEFINE_integer( name='log_interval', short_name='li', default=10, help=flags_core.help_wrap('batches between logging training status')) flags.DEFINE_string( name='output_dir', short_name='od', default=None, help=flags_core.help_wrap('Directory to write TensorBoard summaries')) flags.DEFINE_float(name='learning_rate', short_name='lr', default=0.01, help=flags_core.help_wrap('Learning rate.')) flags.DEFINE_float(name='momentum', short_name='m', default=0.5, help=flags_core.help_wrap('SGD momentum.')) flags.DEFINE_bool(name='no_gpu', short_name='nogpu', default=False, help=flags_core.help_wrap( 'disables GPU usage even if a GPU is available')) flags_core.set_defaults( data_dir='/tmp/tensorflow/mnist/input_data', model_dir='/tmp/tensorflow/mnist/checkpoints/', batch_size=100, train_epochs=10, )
def define_census_flags(): wide_deep_run_loop.define_wide_deep_flags() flags.adopt_module_key_flags(wide_deep_run_loop) flags_core.set_defaults(data_dir='/tmp/census_data', model_dir='/tmp/census_model', train_epochs=40, epochs_between_evals=2, batch_size=40)
def define_mnist_flags(): flags_core.define_base(multi_gpu=True, num_gpu=False) flags_core.define_image() flags.adopt_module_key_flags(flags_core) flags_core.set_defaults(data_dir='/tmp/mnist_data', model_dir='/tmp/mnist_model', batch_size=100, train_epochs=40)
def define_cifar_flags(): resnet_run_loop.define_resnet_flags() flags.adopt_module_key_flags(resnet_run_loop) flags_core.set_defaults(data_dir='/tmp/cifar10_data', model_dir='/tmp/cifar10_model', resnet_size='32', train_epochs=250, epochs_between_evals=10, batch_size=128)
def define_mnist_flags(): flags_core.define_base() flags_core.define_performance(num_parallel_calls=False) flags_core.define_image() flags.adopt_module_key_flags(flags_core) flags_core.set_defaults(data_dir='/tmp/mnist_data', model_dir='/tmp/mnist_model', batch_size=100, train_epochs=40)
def define_cifar_flags(): resnet_run_loop.define_resnet_flags() flags.adopt_module_key_flags(resnet_run_loop) flags_core.set_defaults(data_dir='/tmp/cifar10_data/cifar-10-batches-bin', model_dir='/tmp/cifar10_model', resnet_size='56', train_epochs=182, epochs_between_evals=10, batch_size=128, image_bytes_as_serving_input=False)
def _setup(self): """Sets up and resets flags before each test.""" tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.DEBUG) if KerasNCFBenchmarkBase.local_flags is None: # Loads flags to get defaults to then override. List cannot be empty. flags.FLAGS(['foo']) core.set_defaults(**self.default_flags) saved_flag_values = flagsaver.save_flag_values() KerasNCFBenchmarkBase.local_flags = saved_flag_values else: flagsaver.restore_flag_values(KerasNCFBenchmarkBase.local_flags)
def test_benchmark_setting(self): defaults = dict( hooks=["LoggingMetricHook"], benchmark_log_dir="/tmp/12345", gcp_project="project_abc", ) flags_core.set_defaults(**defaults) flags_core.parse_flags() for key, value in defaults.items(): assert flags.FLAGS.get_flag_value(name=key, default=None) == value
def define_wide_deep_flags(): """Add supervised learning flags, as well as wide-deep model type.""" flags_core.define_base() flags_core.define_benchmark() flags.adopt_module_key_flags(flags_core) flags.DEFINE_enum( name="model_type", short_name="mt", default="wide_deep", enum_values=['wide', 'deep', 'wide_deep'], help="Select model topology.") flags_core.set_defaults(data_dir='/tmp/census_data', model_dir='/tmp/census_model', train_epochs=40, epochs_between_evals=2, batch_size=40)
def define_movie_flags(): """Define flags for movie dataset training.""" wide_deep_run_loop.define_wide_deep_flags() flags.DEFINE_enum( name="dataset", default=movielens.ML_1M, enum_values=movielens.DATASETS, case_sensitive=False, help=flags_core.help_wrap("Dataset to be trained and evaluated.")) flags.adopt_module_key_flags(wide_deep_run_loop) flags_core.set_defaults(data_dir="/tmp/movielens-data/", model_dir='/tmp/movie_model', model_type="deep", train_epochs=50, epochs_between_evals=5, batch_size=256) @flags.validator("stop_threshold", message="stop_threshold not supported for movielens model") def _no_stop(stop_threshold): return stop_threshold is None
def test_default_setting(self): """Test to ensure fields exist and defaults can be set. """ defaults = dict( data_dir="dfgasf", model_dir="dfsdkjgbs", train_epochs=534, epochs_between_evals=15, batch_size=256, hooks=["LoggingTensorHook"], num_parallel_calls=18, inter_op_parallelism_threads=5, intra_op_parallelism_threads=10, data_format="channels_first" ) flags_core.set_defaults(**defaults) flags_core.parse_flags() for key, value in defaults.items(): assert flags.FLAGS.get_flag_value(name=key, default=None) == value
def define_keras_benchmark_flags(): """Add flags for keras built-in application models.""" flags_core.define_base(hooks=False) flags_core.define_performance() flags_core.define_image() flags_core.define_benchmark() flags.adopt_module_key_flags(flags_core) flags_core.set_defaults( data_format="channels_last", use_synthetic_data=True, batch_size=32, train_epochs=2) flags.DEFINE_enum( name="model", default=None, enum_values=MODELS.keys(), case_sensitive=False, help=flags_core.help_wrap( "Model to be benchmarked.")) flags.DEFINE_integer( name="num_images", default=1000, help=flags_core.help_wrap( "The number of synthetic images for training and evaluation. The " "default value is 1000.")) flags.DEFINE_boolean( name="eager", default=False, help=flags_core.help_wrap( "To enable eager execution. Note that if eager execution is enabled, " "only one GPU is utilized even if multiple GPUs are provided and " "multi_gpu_model is used.")) flags.DEFINE_list( name="callbacks", default=["ExamplesPerSecondCallback", "LoggingMetricCallback"], help=flags_core.help_wrap( "A list of (case insensitive) strings to specify the names of " "callbacks. For example: `--callbacks ExamplesPerSecondCallback," "LoggingMetricCallback`"))
def define_ncf_flags(): """Add flags for running ncf_main.""" # Add common flags flags_core.define_base(export_dir=False) flags_core.define_performance( num_parallel_calls=False, inter_op=False, intra_op=False, synthetic_data=True, max_train_steps=False, dtype=False, all_reduce_alg=False ) flags_core.define_device(tpu=True) flags_core.define_benchmark() flags.adopt_module_key_flags(flags_core) flags_core.set_defaults( model_dir="/tmp/ncf/", data_dir="/tmp/movielens-data/", train_epochs=2, batch_size=256, hooks="ProfilerHook", tpu=None ) # Add ncf-specific flags flags.DEFINE_enum( name="dataset", default="ml-1m", enum_values=["ml-1m", "ml-20m"], case_sensitive=False, help=flags_core.help_wrap( "Dataset to be trained and evaluated.")) flags.DEFINE_boolean( name="download_if_missing", default=True, help=flags_core.help_wrap( "Download data to data_dir if it is not already present.")) flags.DEFINE_integer( name="eval_batch_size", default=None, help=flags_core.help_wrap( "The batch size used for evaluation. This should generally be larger" "than the training batch size as the lack of back propagation during" "evaluation can allow for larger batch sizes to fit in memory. If not" "specified, the training batch size (--batch_size) will be used.")) flags.DEFINE_integer( name="num_factors", default=8, help=flags_core.help_wrap("The Embedding size of MF model.")) # Set the default as a list of strings to be consistent with input arguments flags.DEFINE_list( name="layers", default=["64", "32", "16", "8"], help=flags_core.help_wrap( "The sizes of hidden layers for MLP. Example " "to specify different sizes of MLP layers: --layers=32,16,8,4")) flags.DEFINE_float( name="mf_regularization", default=0., help=flags_core.help_wrap( "The regularization factor for MF embeddings. The factor is used by " "regularizer which allows to apply penalties on layer parameters or " "layer activity during optimization.")) flags.DEFINE_list( name="mlp_regularization", default=["0.", "0.", "0.", "0."], help=flags_core.help_wrap( "The regularization factor for each MLP layer. See mf_regularization " "help for more info about regularization factor.")) flags.DEFINE_integer( name="num_neg", default=4, help=flags_core.help_wrap( "The Number of negative instances to pair with a positive instance.")) flags.DEFINE_float( name="learning_rate", default=0.001, help=flags_core.help_wrap("The learning rate.")) flags.DEFINE_float( name="beta1", default=0.9, help=flags_core.help_wrap("beta1 hyperparameter for the Adam optimizer.")) flags.DEFINE_float( name="beta2", default=0.999, help=flags_core.help_wrap("beta2 hyperparameter for the Adam optimizer.")) flags.DEFINE_float( name="epsilon", default=1e-8, help=flags_core.help_wrap("epsilon hyperparameter for the Adam " "optimizer.")) flags.DEFINE_float( name="hr_threshold", default=None, help=flags_core.help_wrap( "If passed, training will stop when the evaluation metric HR is " "greater than or equal to hr_threshold. For dataset ml-1m, the " "desired hr_threshold is 0.68 which is the result from the paper; " "For dataset ml-20m, the threshold can be set as 0.95 which is " "achieved by MLPerf implementation.")) flags.DEFINE_enum( name="constructor_type", default="bisection", enum_values=["bisection", "materialized"], case_sensitive=False, help=flags_core.help_wrap( "Strategy to use for generating false negatives. materialized has a" "precompute that scales badly, but a faster per-epoch construction" "time and can be faster on very large systems.")) flags.DEFINE_bool( name="ml_perf", default=False, help=flags_core.help_wrap( "If set, changes the behavior of the model slightly to match the " "MLPerf reference implementations here: \n" "https://github.com/mlperf/reference/tree/master/recommendation/" "pytorch\n" "The two changes are:\n" "1. When computing the HR and NDCG during evaluation, remove " "duplicate user-item pairs before the computation. This results in " "better HRs and NDCGs.\n" "2. Use a different soring algorithm when sorting the input data, " "which performs better due to the fact the sorting algorithms are " "not stable.")) flags.DEFINE_bool( name="output_ml_perf_compliance_logging", default=False, help=flags_core.help_wrap( "If set, output the MLPerf compliance logging. This is only useful " "if one is running the model for MLPerf. See " "https://github.com/mlperf/policies/blob/master/training_rules.adoc" "#submission-compliance-logs for details. This uses sudo and so may " "ask for your password, as root access is needed to clear the system " "caches, which is required for MLPerf compliance." ) ) flags.DEFINE_integer( name="seed", default=None, help=flags_core.help_wrap( "This value will be used to seed both NumPy and TensorFlow.")) flags.DEFINE_boolean( name="turn_off_distribution_strategy", default=False, help=flags_core.help_wrap( "If set, do not use any distribution strategy.")) @flags.validator("eval_batch_size", "eval_batch_size must be at least {}" .format(rconst.NUM_EVAL_NEGATIVES + 1)) def eval_size_check(eval_batch_size): return (eval_batch_size is None or int(eval_batch_size) > rconst.NUM_EVAL_NEGATIVES) flags.DEFINE_bool( name="use_xla_for_gpu", default=False, help=flags_core.help_wrap( "If True, use XLA for the model function. Only works when using a " "GPU. On TPUs, XLA is always used")) xla_message = "--use_xla_for_gpu is incompatible with --tpu" @flags.multi_flags_validator(["use_xla_for_gpu", "tpu"], message=xla_message) def xla_validator(flag_dict): return not flag_dict["use_xla_for_gpu"] or not flag_dict["tpu"] flags.DEFINE_bool( name="clone_model_in_keras_dist_strat", default=True, help=flags_core.help_wrap( 'If False, then the experimental code path is used that doesn\'t ' "clone models for distribution."))
def define_imagenet_flags(dynamic_loss_scale=False): resnet_run_loop.define_resnet_flags( resnet_size_choices=['18', '34', '50', '101', '152', '200'], dynamic_loss_scale=dynamic_loss_scale) flags.adopt_module_key_flags(resnet_run_loop) flags_core.set_defaults(train_epochs=90)
def define_transformer_flags(): """Add flags and flag validators for running transformer_main.""" # Add common flags (data_dir, model_dir, train_epochs, etc.). flags_core.define_base() flags_core.define_performance( num_parallel_calls=True, inter_op=False, intra_op=False, synthetic_data=True, max_train_steps=False, dtype=False, all_reduce_alg=True ) flags_core.define_benchmark() flags_core.define_device(tpu=True) # Set flags from the flags_core module as "key flags" so they're listed when # the '-h' flag is used. Without this line, the flags defined above are # only shown in the full `--helpful` help text. flags.adopt_module_key_flags(flags_core) # Add transformer-specific flags flags.DEFINE_enum( name="param_set", short_name="mp", default="big", enum_values=PARAMS_MAP.keys(), help=flags_core.help_wrap( "Parameter set to use when creating and training the model. The " "parameters define the input shape (batch size and max length), " "model configuration (size of embedding, # of hidden layers, etc.), " "and various other settings. The big parameter set increases the " "default batch size, embedding/hidden size, and filter size. For a " "complete list of parameters, please see model/model_params.py.")) flags.DEFINE_bool( name="static_batch", default=False, help=flags_core.help_wrap( "Whether the batches in the dataset should have static shapes. In " "general, this setting should be False. Dynamic shapes allow the " "inputs to be grouped so that the number of padding tokens is " "minimized, and helps model training. In cases where the input shape " "must be static (e.g. running on TPU), this setting will be ignored " "and static batching will always be used.")) # Flags for training with steps (may be used for debugging) flags.DEFINE_integer( name="train_steps", short_name="ts", default=None, help=flags_core.help_wrap("The number of steps used to train.")) flags.DEFINE_integer( name="steps_between_evals", short_name="sbe", default=1000, help=flags_core.help_wrap( "The Number of training steps to run between evaluations. This is " "used if --train_steps is defined.")) # BLEU score computation flags.DEFINE_string( name="bleu_source", short_name="bls", default=None, help=flags_core.help_wrap( "Path to source file containing text translate when calculating the " "official BLEU score. Both --bleu_source and --bleu_ref must be set. " "Use the flag --stop_threshold to stop the script based on the " "uncased BLEU score.")) flags.DEFINE_string( name="bleu_ref", short_name="blr", default=None, help=flags_core.help_wrap( "Path to source file containing text translate when calculating the " "official BLEU score. Both --bleu_source and --bleu_ref must be set. " "Use the flag --stop_threshold to stop the script based on the " "uncased BLEU score.")) flags.DEFINE_string( name="vocab_file", short_name="vf", default=None, help=flags_core.help_wrap( "Path to subtoken vocabulary file. If data_download.py was used to " "download and encode the training data, look in the data_dir to find " "the vocab file.")) flags_core.set_defaults(data_dir="/tmp/translate_ende", model_dir="/tmp/transformer_model", batch_size=None, train_epochs=None) @flags.multi_flags_validator( ["train_epochs", "train_steps"], message="Both --train_steps and --train_epochs were set. Only one may be " "defined.") def _check_train_limits(flag_dict): return flag_dict["train_epochs"] is None or flag_dict["train_steps"] is None @flags.multi_flags_validator( ["bleu_source", "bleu_ref"], message="Both or neither --bleu_source and --bleu_ref must be defined.") def _check_bleu_files(flags_dict): return (flags_dict["bleu_source"] is None) == ( flags_dict["bleu_ref"] is None) @flags.multi_flags_validator( ["bleu_source", "bleu_ref", "vocab_file"], message="--vocab_file must be defined if --bleu_source and --bleu_ref " "are defined.") def _check_bleu_vocab_file(flags_dict): if flags_dict["bleu_source"] and flags_dict["bleu_ref"]: return flags_dict["vocab_file"] is not None return True @flags.multi_flags_validator( ["export_dir", "vocab_file"], message="--vocab_file must be defined if --export_dir is set.") def _check_export_vocab_file(flags_dict): if flags_dict["export_dir"]: return flags_dict["vocab_file"] is not None return True flags_core.require_cloud_storage(["data_dir", "model_dir", "export_dir"])
def define_ncf_flags(): """Add flags for running ncf_main.""" # Add common flags flags_core.define_base(export_dir=False) flags_core.define_performance( num_parallel_calls=False, inter_op=False, intra_op=False, synthetic_data=False, max_train_steps=False, dtype=False, all_reduce_alg=False ) flags_core.define_device(tpu=True) flags_core.define_benchmark() flags.adopt_module_key_flags(flags_core) flags_core.set_defaults( model_dir="/tmp/ncf/", data_dir="/tmp/movielens-data/", train_epochs=2, batch_size=256, hooks="ProfilerHook", tpu=None ) # Add ncf-specific flags flags.DEFINE_enum( name="dataset", default="ml-1m", enum_values=["ml-1m", "ml-20m"], case_sensitive=False, help=flags_core.help_wrap( "Dataset to be trained and evaluated.")) flags.DEFINE_boolean( name="download_if_missing", default=True, help=flags_core.help_wrap( "Download data to data_dir if it is not already present.")) flags.DEFINE_string( name="eval_batch_size", default=None, help=flags_core.help_wrap( "The batch size used for evaluation. This should generally be larger" "than the training batch size as the lack of back propagation during" "evaluation can allow for larger batch sizes to fit in memory. If not" "specified, the training batch size (--batch_size) will be used.")) flags.DEFINE_integer( name="num_factors", default=8, help=flags_core.help_wrap("The Embedding size of MF model.")) # Set the default as a list of strings to be consistent with input arguments flags.DEFINE_list( name="layers", default=["64", "32", "16", "8"], help=flags_core.help_wrap( "The sizes of hidden layers for MLP. Example " "to specify different sizes of MLP layers: --layers=32,16,8,4")) flags.DEFINE_float( name="mf_regularization", default=0., help=flags_core.help_wrap( "The regularization factor for MF embeddings. The factor is used by " "regularizer which allows to apply penalties on layer parameters or " "layer activity during optimization.")) flags.DEFINE_list( name="mlp_regularization", default=["0.", "0.", "0.", "0."], help=flags_core.help_wrap( "The regularization factor for each MLP layer. See mf_regularization " "help for more info about regularization factor.")) flags.DEFINE_integer( name="num_neg", default=4, help=flags_core.help_wrap( "The Number of negative instances to pair with a positive instance.")) flags.DEFINE_float( name="learning_rate", default=0.001, help=flags_core.help_wrap("The learning rate.")) flags.DEFINE_float( name="hr_threshold", default=None, help=flags_core.help_wrap( "If passed, training will stop when the evaluation metric HR is " "greater than or equal to hr_threshold. For dataset ml-1m, the " "desired hr_threshold is 0.68 which is the result from the paper; " "For dataset ml-20m, the threshold can be set as 0.95 which is " "achieved by MLPerf implementation.")) flags.DEFINE_bool( name="ml_perf", default=None, help=flags_core.help_wrap( "If set, changes the behavior of the model slightly to match the " "MLPerf reference implementations here: \n" "https://github.com/mlperf/reference/tree/master/recommendation/" "pytorch\n" "The two changes are:\n" "1. When computing the HR and NDCG during evaluation, remove " "duplicate user-item pairs before the computation. This results in " "better HRs and NDCGs.\n" "2. Use a different soring algorithm when sorting the input data, " "which performs better due to the fact the sorting algorithms are " "not stable."))
def define_transformer_flags(): """Add flags and flag validators for running transformer_main.""" # Add common flags (data_dir, model_dir, train_epochs, etc.). flags.DEFINE_integer(name="max_length", short_name="ml", default=None, help=flags_core.help_wrap("Max length.")) flags_core.define_base(clean=True, train_epochs=True, epochs_between_evals=True, stop_threshold=True, num_gpu=True, hooks=True, export_dir=True, distribution_strategy=True) flags_core.define_performance(num_parallel_calls=True, inter_op=False, intra_op=False, synthetic_data=True, max_train_steps=False, dtype=True, all_reduce_alg=True) flags_core.define_benchmark() flags_core.define_device(tpu=True) # Set flags from the flags_core module as "key flags" so they're listed when # the '-h' flag is used. Without this line, the flags defined above are # only shown in the full `--helpful` help text. flags.adopt_module_key_flags(flags_core) # Add transformer-specific flags flags.DEFINE_enum( name="param_set", short_name="mp", default="big", enum_values=PARAMS_MAP.keys(), help=flags_core.help_wrap( "Parameter set to use when creating and training the model. The " "parameters define the input shape (batch size and max length), " "model configuration (size of embedding, # of hidden layers, etc.), " "and various other settings. The big parameter set increases the " "default batch size, embedding/hidden size, and filter size. For a " "complete list of parameters, please see model/model_params.py.")) flags.DEFINE_bool( name="static_batch", default=False, help=flags_core.help_wrap( "Whether the batches in the dataset should have static shapes. In " "general, this setting should be False. Dynamic shapes allow the " "inputs to be grouped so that the number of padding tokens is " "minimized, and helps model training. In cases where the input shape " "must be static (e.g. running on TPU), this setting will be ignored " "and static batching will always be used.")) # Flags for training with steps (may be used for debugging) flags.DEFINE_integer( name="train_steps", short_name="ts", default=None, help=flags_core.help_wrap("The number of steps used to train.")) flags.DEFINE_integer( name="steps_between_evals", short_name="sbe", default=1000, help=flags_core.help_wrap( "The Number of training steps to run between evaluations. This is " "used if --train_steps is defined.")) # BLEU score computation flags.DEFINE_string( name="bleu_source", short_name="bls", default=None, help=flags_core.help_wrap( "Path to source file containing text translate when calculating the " "official BLEU score. Both --bleu_source and --bleu_ref must be set. " "Use the flag --stop_threshold to stop the script based on the " "uncased BLEU score.")) flags.DEFINE_string( name="bleu_ref", short_name="blr", default=None, help=flags_core.help_wrap( "Path to source file containing text translate when calculating the " "official BLEU score. Both --bleu_source and --bleu_ref must be set. " "Use the flag --stop_threshold to stop the script based on the " "uncased BLEU score.")) flags.DEFINE_string( name="vocab_file", short_name="vf", default=None, help=flags_core.help_wrap( "Path to subtoken vocabulary file. If data_download.py was used to " "download and encode the training data, look in the data_dir to find " "the vocab file.")) flags_core.set_defaults(data_dir="/tmp/translate_ende", model_dir="/tmp/transformer_model", batch_size=None, train_epochs=None) @flags.multi_flags_validator( ["train_epochs", "train_steps"], message= "Both --train_steps and --train_epochs were set. Only one may be " "defined.") def _check_train_limits(flag_dict): return flag_dict["train_epochs"] is None or flag_dict[ "train_steps"] is None @flags.multi_flags_validator( ["bleu_source", "bleu_ref"], message="Both or neither --bleu_source and --bleu_ref must be defined." ) def _check_bleu_files(flags_dict): return (flags_dict["bleu_source"] is None) == (flags_dict["bleu_ref"] is None) @flags.multi_flags_validator( ["bleu_source", "bleu_ref", "vocab_file"], message="--vocab_file must be defined if --bleu_source and --bleu_ref " "are defined.") def _check_bleu_vocab_file(flags_dict): if flags_dict["bleu_source"] and flags_dict["bleu_ref"]: return flags_dict["vocab_file"] is not None return True @flags.multi_flags_validator( ["export_dir", "vocab_file"], message="--vocab_file must be defined if --export_dir is set.") def _check_export_vocab_file(flags_dict): if flags_dict["export_dir"]: return flags_dict["vocab_file"] is not None return True flags_core.require_cloud_storage(["data_dir", "model_dir", "export_dir"])
def define_ncf_flags(): """Add flags for running ncf_main.""" # Add common flags flags_core.define_base(model_dir=True, clean=True, train_epochs=True, epochs_between_evals=True, export_dir=False, run_eagerly=True, stop_threshold=True, num_gpu=True, distribution_strategy=True) flags_core.define_performance( synthetic_data=True, dtype=True, fp16_implementation=True, loss_scale=True, dynamic_loss_scale=True, enable_xla=True, ) flags_core.define_device(tpu=True) flags_core.define_benchmark() flags.adopt_module_key_flags(flags_core) movielens.define_flags() flags_core.set_defaults(model_dir="/tmp/ncf/", data_dir="/tmp/movielens-data/", dataset=movielens.ML_1M, train_epochs=2, batch_size=99000, tpu=None) # Add ncf-specific flags flags.DEFINE_boolean( name="download_if_missing", default=True, help=flags_core.help_wrap( "Download data to data_dir if it is not already present.")) flags.DEFINE_integer( name="eval_batch_size", default=None, help=flags_core.help_wrap( "The batch size used for evaluation. This should generally be larger" "than the training batch size as the lack of back propagation during" "evaluation can allow for larger batch sizes to fit in memory. If not" "specified, the training batch size (--batch_size) will be used.")) flags.DEFINE_integer( name="num_factors", default=8, help=flags_core.help_wrap("The Embedding size of MF model.")) # Set the default as a list of strings to be consistent with input arguments flags.DEFINE_list( name="layers", default=["64", "32", "16", "8"], help=flags_core.help_wrap( "The sizes of hidden layers for MLP. Example " "to specify different sizes of MLP layers: --layers=32,16,8,4")) flags.DEFINE_float( name="mf_regularization", default=0., help=flags_core.help_wrap( "The regularization factor for MF embeddings. The factor is used by " "regularizer which allows to apply penalties on layer parameters or " "layer activity during optimization.")) flags.DEFINE_list( name="mlp_regularization", default=["0.", "0.", "0.", "0."], help=flags_core.help_wrap( "The regularization factor for each MLP layer. See mf_regularization " "help for more info about regularization factor.")) flags.DEFINE_integer( name="num_neg", default=4, help=flags_core.help_wrap( "The Number of negative instances to pair with a positive instance." )) flags.DEFINE_float(name="learning_rate", default=0.001, help=flags_core.help_wrap("The learning rate.")) flags.DEFINE_float(name="beta1", default=0.9, help=flags_core.help_wrap( "beta1 hyperparameter for the Adam optimizer.")) flags.DEFINE_float(name="beta2", default=0.999, help=flags_core.help_wrap( "beta2 hyperparameter for the Adam optimizer.")) flags.DEFINE_float(name="epsilon", default=1e-8, help=flags_core.help_wrap( "epsilon hyperparameter for the Adam " "optimizer.")) flags.DEFINE_float( name="hr_threshold", default=1.0, help=flags_core.help_wrap( "If passed, training will stop when the evaluation metric HR is " "greater than or equal to hr_threshold. For dataset ml-1m, the " "desired hr_threshold is 0.68 which is the result from the paper; " "For dataset ml-20m, the threshold can be set as 0.95 which is " "achieved by MLPerf implementation.")) flags.DEFINE_enum( name="constructor_type", default="bisection", enum_values=["bisection", "materialized"], case_sensitive=False, help=flags_core.help_wrap( "Strategy to use for generating false negatives. materialized has a" "precompute that scales badly, but a faster per-epoch construction" "time and can be faster on very large systems.")) flags.DEFINE_string(name="train_dataset_path", default=None, help=flags_core.help_wrap("Path to training data.")) flags.DEFINE_string(name="eval_dataset_path", default=None, help=flags_core.help_wrap("Path to evaluation data.")) flags.DEFINE_string( name="input_meta_data_path", default=None, help=flags_core.help_wrap("Path to input meta data file.")) flags.DEFINE_bool( name="ml_perf", default=False, help=flags_core.help_wrap( "If set, changes the behavior of the model slightly to match the " "MLPerf reference implementations here: \n" "https://github.com/mlperf/reference/tree/master/recommendation/" "pytorch\n" "The two changes are:\n" "1. When computing the HR and NDCG during evaluation, remove " "duplicate user-item pairs before the computation. This results in " "better HRs and NDCGs.\n" "2. Use a different soring algorithm when sorting the input data, " "which performs better due to the fact the sorting algorithms are " "not stable.")) flags.DEFINE_bool( name="output_ml_perf_compliance_logging", default=False, help=flags_core.help_wrap( "If set, output the MLPerf compliance logging. This is only useful " "if one is running the model for MLPerf. See " "https://github.com/mlperf/policies/blob/master/training_rules.adoc" "#submission-compliance-logs for details. This uses sudo and so may " "ask for your password, as root access is needed to clear the system " "caches, which is required for MLPerf compliance.")) flags.DEFINE_integer( name="seed", default=None, help=flags_core.help_wrap( "This value will be used to seed both NumPy and TensorFlow.")) @flags.validator("eval_batch_size", "eval_batch_size must be at least {}".format( rconst.NUM_EVAL_NEGATIVES + 1)) def eval_size_check(eval_batch_size): return (eval_batch_size is None or int(eval_batch_size) > rconst.NUM_EVAL_NEGATIVES) flags.DEFINE_bool( name="early_stopping", default=False, help=flags_core.help_wrap( "If True, we stop the training when it reaches hr_threshold")) flags.DEFINE_bool(name="keras_use_ctl", default=False, help=flags_core.help_wrap( "If True, we use a custom training loop for keras."))
def define_deep_speech_flags(): """Add flags for run_deep_speech.""" # Add common flags flags_core.define_base( data_dir=False, # we use train_data_dir and eval_data_dir instead export_dir=True, train_epochs=True, hooks=True, epochs_between_evals=True, ) flags_core.define_performance(num_parallel_calls=False, inter_op=False, intra_op=False, synthetic_data=False, max_train_steps=False, dtype=False) flags_core.define_benchmark() flags.adopt_module_key_flags(flags_core) flags_core.set_defaults(model_dir=_DEFAULT_MODEL_DIR, export_dir=_DEFAULT_SAVE_MODEL_DIR, train_epochs=10, batch_size=128, hooks=[], epochs_between_evals=4) # Deep speech flags flags.DEFINE_integer(name="seed", default=1, help=flags_core.help_wrap("The random seed.")) flags.DEFINE_string( name="train_data_dir", default=_DEFAULT_TRAIN_DIR, help=flags_core.help_wrap("The csv file path of train dataset.")) flags.DEFINE_string( name="eval_data_dir", default=_DEFAULT_EVAL_DIR, help=flags_core.help_wrap("The csv file path of evaluation dataset.")) flags.DEFINE_bool( name="sortagrad", default=True, help=flags_core.help_wrap( "If true, sort examples by audio length and perform no " "batch_wise shuffling for the first epoch.")) flags.DEFINE_integer( name="sample_rate", default=16000, help=flags_core.help_wrap("The sample rate for audio.")) flags.DEFINE_integer( name="window_ms", default=20, help=flags_core.help_wrap("The frame length for spectrogram.")) flags.DEFINE_integer(name="stride_ms", default=10, help=flags_core.help_wrap("The frame step.")) flags.DEFINE_string( name="vocabulary_file", default=_DEFAULT_CHARACTERS_FILE, help=flags_core.help_wrap("The file path of vocabulary file.")) # RNN related flags flags.DEFINE_integer(name="rnn_hidden_size", default=800, help=flags_core.help_wrap("The hidden size of RNNs.")) flags.DEFINE_integer( name="rnn_hidden_layers", default=5, help=flags_core.help_wrap("The number of RNN layers.")) flags.DEFINE_bool(name="use_bias", default=True, help=flags_core.help_wrap( "Use bias in the last fully-connected layer")) flags.DEFINE_bool( name="is_bidirectional", default=True, help=flags_core.help_wrap("If rnn unit is bidirectional")) flags.DEFINE_enum(name="rnn_type", default="gru", enum_values=deep_speech.SUPPORTED_RNNS.keys(), case_sensitive=False, help=flags_core.help_wrap("Type of RNN cell.")) # Training related flags flags.DEFINE_float(name="learning_rate", default=5e-4, help=flags_core.help_wrap("The initial learning rate.")) # Evaluation metrics threshold flags.DEFINE_float( name="wer_threshold", default=None, help=flags_core.help_wrap( "If passed, training will stop when the evaluation metric WER is " "greater than or equal to wer_threshold. For libri speech dataset " "the desired wer_threshold is 0.23 which is the result achieved by " "MLPerf implementation.")) flags.DEFINE_integer(name='num_gpus', default=-1, help='num_gpus')
train_df = df.sample(frac=0.8, random_state=0) eval_df = df.drop(train_df.index) train_df = train_df.reset_index(drop=True) eval_df = eval_df.reset_index(drop=True) train_input_fn = _df_to_input_fn( df=train_df, name="train", dataset=dataset, data_dir=data_dir, batch_size=batch_size, repeat=repeat, shuffle=movielens.NUM_RATINGS[dataset]) eval_input_fn = _df_to_input_fn( df=eval_df, name="eval", dataset=dataset, data_dir=data_dir, batch_size=batch_size, repeat=repeat, shuffle=None) model_column_fn = functools.partial(build_model_columns, dataset=dataset) train_input_fn() return train_input_fn, eval_input_fn, model_column_fn def main(_): movielens.download(dataset=flags.FLAGS.dataset, data_dir=flags.FLAGS.data_dir) construct_input_fns(flags.FLAGS.dataset, flags.FLAGS.data_dir) if __name__ == "__main__": tf.logging.set_verbosity(tf.logging.INFO) movielens.define_data_download_flags() flags.adopt_module_key_flags(movielens) flags_core.set_defaults(dataset="ml-1m") absl_app.run(main)
def define_deep_speech_flags(): """Add flags for run_deep_speech.""" # Add common flags flags_core.define_base( data_dir=False # we use train_data_dir and eval_data_dir instead ) flags_core.define_performance( num_parallel_calls=False, inter_op=False, intra_op=False, synthetic_data=False, max_train_steps=False, dtype=False ) flags_core.define_benchmark() flags.adopt_module_key_flags(flags_core) flags_core.set_defaults( model_dir="/tmp/deep_speech_model/", export_dir="/tmp/deep_speech_saved_model/", train_epochs=10, batch_size=128, hooks="") # Deep speech flags flags.DEFINE_integer( name="seed", default=1, help=flags_core.help_wrap("The random seed.")) flags.DEFINE_string( name="train_data_dir", default="/tmp/librispeech_data/test-clean/LibriSpeech/test-clean.csv", help=flags_core.help_wrap("The csv file path of train dataset.")) flags.DEFINE_string( name="eval_data_dir", default="/tmp/librispeech_data/test-clean/LibriSpeech/test-clean.csv", help=flags_core.help_wrap("The csv file path of evaluation dataset.")) flags.DEFINE_bool( name="sortagrad", default=True, help=flags_core.help_wrap( "If true, sort examples by audio length and perform no " "batch_wise shuffling for the first epoch.")) flags.DEFINE_integer( name="sample_rate", default=16000, help=flags_core.help_wrap("The sample rate for audio.")) flags.DEFINE_integer( name="window_ms", default=20, help=flags_core.help_wrap("The frame length for spectrogram.")) flags.DEFINE_integer( name="stride_ms", default=10, help=flags_core.help_wrap("The frame step.")) flags.DEFINE_string( name="vocabulary_file", default=_VOCABULARY_FILE, help=flags_core.help_wrap("The file path of vocabulary file.")) # RNN related flags flags.DEFINE_integer( name="rnn_hidden_size", default=800, help=flags_core.help_wrap("The hidden size of RNNs.")) flags.DEFINE_integer( name="rnn_hidden_layers", default=5, help=flags_core.help_wrap("The number of RNN layers.")) flags.DEFINE_bool( name="use_bias", default=True, help=flags_core.help_wrap("Use bias in the last fully-connected layer")) flags.DEFINE_bool( name="is_bidirectional", default=True, help=flags_core.help_wrap("If rnn unit is bidirectional")) flags.DEFINE_enum( name="rnn_type", default="gru", enum_values=deep_speech_model.SUPPORTED_RNNS.keys(), case_sensitive=False, help=flags_core.help_wrap("Type of RNN cell.")) # Training related flags flags.DEFINE_float( name="learning_rate", default=5e-4, help=flags_core.help_wrap("The initial learning rate.")) # Evaluation metrics threshold flags.DEFINE_float( name="wer_threshold", default=None, help=flags_core.help_wrap( "If passed, training will stop when the evaluation metric WER is " "greater than or equal to wer_threshold. For libri speech dataset " "the desired wer_threshold is 0.23 which is the result achieved by " "MLPerf implementation."))
def define_imagenet_flags(): resnet_run_loop.define_resnet_flags( resnet_size_choices=['18', '34', '50', '101', '152', '200']) flags.adopt_module_key_flags(resnet_run_loop) flags_core.set_defaults(train_epochs=100)
def define_ncf_flags(): """Add flags for running ncf_main.""" # Add common flags flags_core.define_base(export_dir=False) flags_core.define_performance(num_parallel_calls=False, inter_op=False, intra_op=False, synthetic_data=True, max_train_steps=False, dtype=False, all_reduce_alg=False) flags_core.define_device(tpu=True) flags_core.define_benchmark() flags.adopt_module_key_flags(flags_core) flags_core.set_defaults(model_dir="/tmp/ncf/", data_dir="/tmp/movielens-data/", train_epochs=2, batch_size=256, hooks="ProfilerHook", tpu=None) # Add ncf-specific flags flags.DEFINE_enum( name="dataset", default="ml-1m", enum_values=["ml-1m", "ml-20m"], case_sensitive=False, help=flags_core.help_wrap("Dataset to be trained and evaluated.")) flags.DEFINE_boolean( name="download_if_missing", default=True, help=flags_core.help_wrap( "Download data to data_dir if it is not already present.")) flags.DEFINE_string( name="eval_batch_size", default=None, help=flags_core.help_wrap( "The batch size used for evaluation. This should generally be larger" "than the training batch size as the lack of back propagation during" "evaluation can allow for larger batch sizes to fit in memory. If not" "specified, the training batch size (--batch_size) will be used.")) flags.DEFINE_integer( name="num_factors", default=8, help=flags_core.help_wrap("The Embedding size of MF model.")) # Set the default as a list of strings to be consistent with input arguments flags.DEFINE_list( name="layers", default=["64", "32", "16", "8"], help=flags_core.help_wrap( "The sizes of hidden layers for MLP. Example " "to specify different sizes of MLP layers: --layers=32,16,8,4")) flags.DEFINE_float( name="mf_regularization", default=0., help=flags_core.help_wrap( "The regularization factor for MF embeddings. The factor is used by " "regularizer which allows to apply penalties on layer parameters or " "layer activity during optimization.")) flags.DEFINE_list( name="mlp_regularization", default=["0.", "0.", "0.", "0."], help=flags_core.help_wrap( "The regularization factor for each MLP layer. See mf_regularization " "help for more info about regularization factor.")) flags.DEFINE_integer( name="num_neg", default=4, help=flags_core.help_wrap( "The Number of negative instances to pair with a positive instance." )) flags.DEFINE_float(name="learning_rate", default=0.001, help=flags_core.help_wrap("The learning rate.")) flags.DEFINE_float(name="beta1", default=0.9, help=flags_core.help_wrap( "beta1 hyperparameter for the Adam optimizer.")) flags.DEFINE_float(name="beta2", default=0.999, help=flags_core.help_wrap( "beta2 hyperparameter for the Adam optimizer.")) flags.DEFINE_float(name="epsilon", default=1e-8, help=flags_core.help_wrap( "epsilon hyperparameter for the Adam " "optimizer.")) flags.DEFINE_float( name="hr_threshold", default=None, help=flags_core.help_wrap( "If passed, training will stop when the evaluation metric HR is " "greater than or equal to hr_threshold. For dataset ml-1m, the " "desired hr_threshold is 0.68 which is the result from the paper; " "For dataset ml-20m, the threshold can be set as 0.95 which is " "achieved by MLPerf implementation.")) flags.DEFINE_bool( name="ml_perf", default=False, help=flags_core.help_wrap( "If set, changes the behavior of the model slightly to match the " "MLPerf reference implementations here: \n" "https://github.com/mlperf/reference/tree/master/recommendation/" "pytorch\n" "The two changes are:\n" "1. When computing the HR and NDCG during evaluation, remove " "duplicate user-item pairs before the computation. This results in " "better HRs and NDCGs.\n" "2. Use a different soring algorithm when sorting the input data, " "which performs better due to the fact the sorting algorithms are " "not stable.")) flags.DEFINE_bool( name="output_ml_perf_compliance_logging", default=False, help=flags_core.help_wrap( "If set, output the MLPerf compliance logging. This is only useful " "if one is running the model for MLPerf. See " "https://github.com/mlperf/policies/blob/master/training_rules.adoc" "#submission-compliance-logs for details. This uses sudo and so may " "ask for your password, as root access is needed to clear the system " "caches, which is required for MLPerf compliance.")) flags.DEFINE_integer( name="seed", default=None, help=flags_core.help_wrap( "This value will be used to seed both NumPy and TensorFlow.")) flags.DEFINE_bool( name="hash_pipeline", default=False, help=flags_core.help_wrap( "This flag will perform a separate run of the pipeline and hash " "batches as they are produced. \nNOTE: this will significantly slow " "training. However it is useful to confirm that a random seed is " "does indeed make the data pipeline deterministic.")) @flags.validator("eval_batch_size", "eval_batch_size must be at least {}".format( rconst.NUM_EVAL_NEGATIVES + 1)) def eval_size_check(eval_batch_size): return (eval_batch_size is None or int(eval_batch_size) > rconst.NUM_EVAL_NEGATIVES) flags.DEFINE_bool( name="use_subprocess", default=True, help=flags_core.help_wrap( "By default, ncf_main.py starts async data generation process as a " "subprocess. If set to False, ncf_main.py will assume the async data " "generation process has already been started by the user.")) flags.DEFINE_integer( name="cache_id", default=None, help=flags_core.help_wrap( "Use a specified cache_id rather than using a timestamp. This is only " "needed to synchronize across multiple workers. Generally this flag will " "not need to be set.")) flags.DEFINE_bool( name="use_xla_for_gpu", default=False, help=flags_core.help_wrap( "If True, use XLA for the model function. Only works when using a " "GPU. On TPUs, XLA is always used")) xla_message = "--use_xla_for_gpu is incompatible with --tpu" @flags.multi_flags_validator(["use_xla_for_gpu", "tpu"], message=xla_message) def xla_validator(flag_dict): return not flag_dict["use_xla_for_gpu"] or not flag_dict["tpu"] flags.DEFINE_bool( name="use_estimator", default=True, help=flags_core.help_wrap( "If True, use Estimator to train. Setting to False is slightly " "faster, but when False, the following are currently unsupported:\n" " * Using TPUs\n" " * Using more than 1 GPU\n" " * Reloading from checkpoints\n" " * Any hooks specified with --hooks\n")) flags.DEFINE_bool( name="use_while_loop", default=None, help=flags_core.help_wrap( "If set, run an entire epoch in a session.run() call using a " "TensorFlow while loop. This can improve performance, but will not " "print out losses throughout the epoch. Requires " "--use_estimator=false")) xla_message = "--use_while_loop requires --use_estimator=false" @flags.multi_flags_validator(["use_while_loop", "use_estimator"], message=xla_message) def while_loop_validator(flag_dict): return (not flag_dict["use_while_loop"] or not flag_dict["use_estimator"])
def define_ncf_flags(): """Add flags for running ncf_main.""" # Add common flags flags_core.define_base(export_dir=False) flags_core.define_performance( num_parallel_calls=False, inter_op=False, intra_op=False, synthetic_data=True, max_train_steps=False, dtype=False, all_reduce_alg=False ) flags_core.define_device(tpu=True) flags_core.define_benchmark() flags.adopt_module_key_flags(flags_core) flags_core.set_defaults( model_dir="/tmp/ncf/", data_dir="/tmp/movielens-data/", train_epochs=2, batch_size=256, hooks="ProfilerHook", tpu=None ) # Add ncf-specific flags flags.DEFINE_enum( name="dataset", default="ml-1m", enum_values=["ml-1m", "ml-20m"], case_sensitive=False, help=flags_core.help_wrap( "Dataset to be trained and evaluated.")) flags.DEFINE_boolean( name="download_if_missing", default=True, help=flags_core.help_wrap( "Download data to data_dir if it is not already present.")) flags.DEFINE_string( name="eval_batch_size", default=None, help=flags_core.help_wrap( "The batch size used for evaluation. This should generally be larger" "than the training batch size as the lack of back propagation during" "evaluation can allow for larger batch sizes to fit in memory. If not" "specified, the training batch size (--batch_size) will be used.")) flags.DEFINE_integer( name="num_factors", default=8, help=flags_core.help_wrap("The Embedding size of MF model.")) # Set the default as a list of strings to be consistent with input arguments flags.DEFINE_list( name="layers", default=["64", "32", "16", "8"], help=flags_core.help_wrap( "The sizes of hidden layers for MLP. Example " "to specify different sizes of MLP layers: --layers=32,16,8,4")) flags.DEFINE_float( name="mf_regularization", default=0., help=flags_core.help_wrap( "The regularization factor for MF embeddings. The factor is used by " "regularizer which allows to apply penalties on layer parameters or " "layer activity during optimization.")) flags.DEFINE_list( name="mlp_regularization", default=["0.", "0.", "0.", "0."], help=flags_core.help_wrap( "The regularization factor for each MLP layer. See mf_regularization " "help for more info about regularization factor.")) flags.DEFINE_integer( name="num_neg", default=4, help=flags_core.help_wrap( "The Number of negative instances to pair with a positive instance.")) flags.DEFINE_float( name="learning_rate", default=0.001, help=flags_core.help_wrap("The learning rate.")) flags.DEFINE_float( name="beta1", default=0.9, help=flags_core.help_wrap("beta1 hyperparameter for the Adam optimizer.")) flags.DEFINE_float( name="beta2", default=0.999, help=flags_core.help_wrap("beta2 hyperparameter for the Adam optimizer.")) flags.DEFINE_float( name="epsilon", default=1e-8, help=flags_core.help_wrap("epsilon hyperparameter for the Adam " "optimizer.")) flags.DEFINE_float( name="hr_threshold", default=None, help=flags_core.help_wrap( "If passed, training will stop when the evaluation metric HR is " "greater than or equal to hr_threshold. For dataset ml-1m, the " "desired hr_threshold is 0.68 which is the result from the paper; " "For dataset ml-20m, the threshold can be set as 0.95 which is " "achieved by MLPerf implementation.")) flags.DEFINE_bool( name="ml_perf", default=False, help=flags_core.help_wrap( "If set, changes the behavior of the model slightly to match the " "MLPerf reference implementations here: \n" "https://github.com/mlperf/reference/tree/master/recommendation/" "pytorch\n" "The two changes are:\n" "1. When computing the HR and NDCG during evaluation, remove " "duplicate user-item pairs before the computation. This results in " "better HRs and NDCGs.\n" "2. Use a different soring algorithm when sorting the input data, " "which performs better due to the fact the sorting algorithms are " "not stable.")) flags.DEFINE_bool( name="output_ml_perf_compliance_logging", default=False, help=flags_core.help_wrap( "If set, output the MLPerf compliance logging. This is only useful " "if one is running the model for MLPerf. See " "https://github.com/mlperf/policies/blob/master/training_rules.adoc" "#submission-compliance-logs for details. This uses sudo and so may " "ask for your password, as root access is needed to clear the system " "caches, which is required for MLPerf compliance." ) ) flags.DEFINE_integer( name="seed", default=None, help=flags_core.help_wrap( "This value will be used to seed both NumPy and TensorFlow.")) flags.DEFINE_bool( name="hash_pipeline", default=False, help=flags_core.help_wrap( "This flag will perform a separate run of the pipeline and hash " "batches as they are produced. \nNOTE: this will significantly slow " "training. However it is useful to confirm that a random seed is " "does indeed make the data pipeline deterministic.")) @flags.validator("eval_batch_size", "eval_batch_size must be at least {}" .format(rconst.NUM_EVAL_NEGATIVES + 1)) def eval_size_check(eval_batch_size): return (eval_batch_size is None or int(eval_batch_size) > rconst.NUM_EVAL_NEGATIVES) flags.DEFINE_bool( name="use_subprocess", default=True, help=flags_core.help_wrap( "By default, ncf_main.py starts async data generation process as a " "subprocess. If set to False, ncf_main.py will assume the async data " "generation process has already been started by the user.")) flags.DEFINE_integer(name="cache_id", default=None, help=flags_core.help_wrap( "Use a specified cache_id rather than using a timestamp. This is only " "needed to synchronize across multiple workers. Generally this flag will " "not need to be set." )) flags.DEFINE_bool( name="use_xla_for_gpu", default=False, help=flags_core.help_wrap( "If True, use XLA for the model function. Only works when using a " "GPU. On TPUs, XLA is always used")) xla_message = "--use_xla_for_gpu is incompatible with --tpu" @flags.multi_flags_validator(["use_xla_for_gpu", "tpu"], message=xla_message) def xla_validator(flag_dict): return not flag_dict["use_xla_for_gpu"] or not flag_dict["tpu"] flags.DEFINE_bool( name="use_estimator", default=True, help=flags_core.help_wrap( "If True, use Estimator to train. Setting to False is slightly " "faster, but when False, the following are currently unsupported:\n" " * Using TPUs\n" " * Using more than 1 GPU\n" " * Reloading from checkpoints\n" " * Any hooks specified with --hooks\n")) flags.DEFINE_bool( name="use_while_loop", default=None, help=flags_core.help_wrap( "If set, run an entire epoch in a session.run() call using a " "TensorFlow while loop. This can improve performance, but will not " "print out losses throughout the epoch. Requires " "--use_estimator=false" )) xla_message = "--use_while_loop requires --use_estimator=false" @flags.multi_flags_validator(["use_while_loop", "use_estimator"], message=xla_message) def while_loop_validator(flag_dict): return (not flag_dict["use_while_loop"] or not flag_dict["use_estimator"])
def define_imagenet_keras_flags(): keras_common.define_keras_flags() flags_core.set_defaults(train_epochs=90)
def define_ncf_flags(): """Add flags for running ncf_main.""" # Add common flags flags_core.define_base(export_dir=False) flags_core.define_performance( num_parallel_calls=False, inter_op=False, intra_op=False, synthetic_data=False, max_train_steps=False, dtype=False, all_reduce_alg=False ) flags_core.define_benchmark() flags.adopt_module_key_flags(flags_core) flags_core.set_defaults( model_dir="/tmp/ncf/", data_dir="/tmp/movielens-data/", train_epochs=2, batch_size=256, hooks="ProfilerHook") # Add ncf-specific flags flags.DEFINE_enum( name="dataset", default="ml-1m", enum_values=["ml-1m", "ml-20m"], case_sensitive=False, help=flags_core.help_wrap( "Dataset to be trained and evaluated.")) flags.DEFINE_integer( name="num_factors", default=8, help=flags_core.help_wrap("The Embedding size of MF model.")) # Set the default as a list of strings to be consistent with input arguments flags.DEFINE_list( name="layers", default=["64", "32", "16", "8"], help=flags_core.help_wrap( "The sizes of hidden layers for MLP. Example " "to specify different sizes of MLP layers: --layers=32,16,8,4")) flags.DEFINE_float( name="mf_regularization", default=0., help=flags_core.help_wrap( "The regularization factor for MF embeddings. The factor is used by " "regularizer which allows to apply penalties on layer parameters or " "layer activity during optimization.")) flags.DEFINE_list( name="mlp_regularization", default=["0.", "0.", "0.", "0."], help=flags_core.help_wrap( "The regularization factor for each MLP layer. See mf_regularization " "help for more info about regularization factor.")) flags.DEFINE_integer( name="num_neg", default=4, help=flags_core.help_wrap( "The Number of negative instances to pair with a positive instance.")) flags.DEFINE_float( name="learning_rate", default=0.001, help=flags_core.help_wrap("The learning rate.")) flags.DEFINE_float( name="hr_threshold", default=None, help=flags_core.help_wrap( "If passed, training will stop when the evaluation metric HR is " "greater than or equal to hr_threshold. For dataset ml-1m, the " "desired hr_threshold is 0.68 which is the result from the paper; " "For dataset ml-20m, the threshold can be set as 0.95 which is " "achieved by MLPerf implementation."))
def define_ncf_flags(): """Add flags for running ncf_main.""" # Add common flags flags_core.define_base(export_dir=False) flags_core.define_performance(num_parallel_calls=False, inter_op=False, intra_op=False, synthetic_data=False, max_train_steps=False, dtype=False) flags_core.define_benchmark() flags.adopt_module_key_flags(flags_core) flags_core.set_defaults(model_dir="/tmp/ncf/", data_dir="/tmp/movielens-data/", train_epochs=2, batch_size=256, hooks="ProfilerHook") # Add ncf-specific flags flags.DEFINE_enum( name="dataset", default="ml-1m", enum_values=["ml-1m", "ml-20m"], case_sensitive=False, help=flags_core.help_wrap("Dataset to be trained and evaluated.")) flags.DEFINE_integer( name="num_factors", default=8, help=flags_core.help_wrap("The Embedding size of MF model.")) # Set the default as a list of strings to be consistent with input arguments flags.DEFINE_list( name="layers", default=["64", "32", "16", "8"], help=flags_core.help_wrap( "The sizes of hidden layers for MLP. Example " "to specify different sizes of MLP layers: --layers=32,16,8,4")) flags.DEFINE_float( name="mf_regularization", default=0., help=flags_core.help_wrap( "The regularization factor for MF embeddings. The factor is used by " "regularizer which allows to apply penalties on layer parameters or " "layer activity during optimization.")) flags.DEFINE_list( name="mlp_regularization", default=["0.", "0.", "0.", "0."], help=flags_core.help_wrap( "The regularization factor for each MLP layer. See mf_regularization " "help for more info about regularization factor.")) flags.DEFINE_integer( name="num_neg", default=4, help=flags_core.help_wrap( "The Number of negative instances to pair with a positive instance." )) flags.DEFINE_float(name="learning_rate", default=0.001, help=flags_core.help_wrap("The learning rate.")) flags.DEFINE_float( name="hr_threshold", default=None, help=flags_core.help_wrap( "If passed, training will stop when the evaluation metric HR is " "greater than or equal to hr_threshold. For dataset ml-1m, the " "desired hr_threshold is 0.68 which is the result from the paper; " "For dataset ml-20m, the threshold can be set as 0.95 which is " "achieved by MLPerf implementation."))