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

问题描述 投票:1回答:1

documentation for the tf.Dataset.data.shuffle function声明以下内容:

  • reshuffle_each_iteration :(可选。)布尔值,如果为true,则表示每次迭代数据集时都应进行伪随机重排。 (默认为True。)

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

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

该函数调用ShuffleDataset类,该类的__init__函数也为sets the same argument to None by default,并且uses the following logic将参数的默认值设置为True:

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

为什么在函数和类中默认不将参数默认设置为True?这将使上面的代码块变得多余,并允许仅用self._reshuffle_each_iteration = reshuffle_each_iteration替换它。

python tensorflow tensorflow-datasets
1个回答
0
投票

[@ mrry here回答:

  • 通常,我更喜欢使用None作为默认参数,因为这样可以更轻松地围绕API编写包装器。如果将默认参数设置为True,则包装程序很难使用被包装函数的默认值,而不在包装的每一层都复制默认值。使用None并在最内层实现默认值更易于维护。
  • 当默认参数可以是可变类型(如列表)并且将默认值放在参数列表中是不安全的时,使用None也与必要的样式一致。
© www.soinside.com 2019 - 2024. All rights reserved.