How to set random seed in Keras?
In Keras, the randomness of a model can be controlled by setting the random seed of numpy and TensorFlow.
import numpy as np
import tensorflow as tf
# 设置 numpy 的随机种子
np.random.seed(0)
# 设置 tensorflow 的随机种子
tf.random.set_seed(0)
By setting a random seed using the code above before creating the model, it ensures that the random numbers generated during each run will be the same, thus ensuring the repeatability of the model.