def set_seed(seed): tf.random.set_seed(seed) os.environ['PYTHONHASHSEED'] = str(seed) np.random.seed(seed) random.seed(seed)
时间: 2023-12-06 21:04:03 浏览: 171
This function sets the random seed for TensorFlow, NumPy, and Python's built-in random library. This ensures that the same sequence of random numbers will be generated every time the code is run with the same seed value. This is useful for reproducibility and debugging purposes.
The function takes a single argument, `seed`, which is the seed value to use. The `tf.random.set_seed()` function sets the random seed for TensorFlow, while `np.random.seed()` sets the seed for NumPy, and `random.seed()` sets the seed for Python's built-in random library. Finally, the `os.environ['PYTHONHASHSEED'] = str(seed)` line sets the seed for Python's hash function, which is used for dict and set objects.
Overall, this function is a best practice for ensuring reproducibility in machine learning experiments.
阅读全文