0
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)

This code is executed at the beginning of the bert fine-tuning code. Can you please help me to understand the purpose of this code?

AloneTogether
  • 25,814
  • 5
  • 20
  • 39
Shanuka
  • 9
  • 1

1 Answers1

2

The purpose of this is to ensure that results are reproducible, i.e. that you will get the same outcome every time.

For more detail see this answer to the same question asked about the R set.seed() function.

SamR
  • 8,826
  • 3
  • 11
  • 33