为什么 TensorFlow tf.data.Dataset.shuffle 函数的 reshuffle_each_iteration 布尔参数默认为 None,而不是 True?

Why does the TensorFlow tf.data.Dataset.shuffle function's reshuffle_each_iteration boolean argument default to None, as opposed to True?

documentation for the tf.Dataset.data.shuffle function 声明如下:

  • reshuffle_each_iteration: (Optional.) A boolean, which if true indicates that the dataset should be pseudorandomly reshuffled each time it is iterated over. (Defaults to True.)

但是,函数中的默认值为 None,如同一页和 the actual code 中所述:

def shuffle(self, buffer_size, seed=None, reshuffle_each_iteration=None):

函数调用ShuffleDatasetclass,其__init__函数也sets the same argument to None by default, and uses the following logic将参数的默认值设置为True:

if reshuffle_each_iteration is None:
  self._reshuffle_each_iteration = True
else:
  self._reshuffle_each_iteration = reshuffle_each_iteration

为什么函数和 class 中的参数不默认设置为 True?这将使上面的代码块变得多余,并且只允许用 self._reshuffle_each_iteration = reshuffle_each_iteration.

替换它

@mrry 回答here:

  • In general I prefer None for default arguments, because it makes it easier to write wrappers around an API. If the default argument is set to True, it's difficult for a wrapper to use the default of the wrapped function, without making a copy of the default at each layer of wrapping. Using None and implementing the default at the innermost level is simpler to maintain.
  • Using None is also consistent with the necessary style when the default argument can be a mutable type (like a list) and it is unsafe to put the default value in the argument list.