我没有'我不理解为什么在使用torch和tensorflow导入时需要set_sed


def set_seed(seed: int):
"""
Helper function for reproducible behavior to set the seed in ``random``, ``numpy``, ``torch`` and/or ``tf`` (if
installed).
Args:
seed (:obj:`int`): The seed to set.
"""
random.seed(seed)
np.random.seed(seed)
if is_torch_available():
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
# ^^ safe to call this function even if cuda is not available
if is_tf_available():
import tensorflow as tf
tf.random.set_seed(seed)
set_seed(1)

该代码在伯特微调代码的开头执行。你能帮我理解一下这个代码的用途吗?

这样做的目的是确保结果是可重复的,即每次都会得到相同的结果。

有关更多详细信息,请参阅对有关Rset.seed()函数的同一问题的回答。

最新更新