在 keras 中使用 conv2D 层时,在 tf.random.set_seed 中设置种子是否也会设置 glorot_uniform kernel_initializer 使用的种子?

Does setting the seed in tf.random.set_seed also set the seed used by the glorot_uniform kernel_initializer when using a conv2D layer in keras?

我目前正在使用如下定义的 conv2D layer 训练 convolutional neural network

conv1 = tf.keras.layers.Conv2D(filters=64, kernel_size=(3,3), padding='SAME', activation='relu')(inputs)

我的理解是默认的 kernel_initializer 是 glorot_uniform,它的默认种子是 'none':

tf.keras.layers.Conv2D(
        filters, kernel_size, strides=(1, 1), padding='valid', data_format=None,
        dilation_rate=(1, 1), activation=None, use_bias=True,
        kernel_initializer='glorot_uniform', bias_initializer='zeros',
        kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None,
        kernel_constraint=None, bias_constraint=None, **kwargs
    )



tf.compat.v1.keras.initializers.glorot_uniform(seed=None, dtype=tf.dtypes.float32)

我正在尝试生成可重现的代码,并且已经按照 设置了随机种子:

seed_num = 1

os.environ['PYTHONHASHSEED'] = '0'
np.random.seed(seed_num)
rn.seed(seed_num)

session_conf = tf.compat.v1.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1)

tf.random.set_seed(seed_num)

sess = tf.compat.v1.Session(graph=tf.compat.v1.get_default_graph(), config=session_conf)
K.set_session(sess)

glorot_uniform使用的tf.random.set_seed种子号是否在conv2D layer内?如果不是,那么在定义 conv2D layer?

时如何定义该种子

对于每一层,您都可以将种子用于内核和偏置初始化器。

您可以单独为初始化程序设置种子,

kernel_initializer=initializers.glorot_uniform(seed=0))

来自文档:

glorot_normal

keras.initializers.glorot_normal(seed=None)

Glorot normal initializer, also called Xavier normal initializer.

It draws samples from a truncated normal distribution centered on 0 with stddev = sqrt(2 / (fan_in + fan_out)) where fan_in is the number of input units in the weight tensor and fan_out is the number of output units in the weight tensor.

Arguments

    seed: A Python integer. Used to seed the random generator.

感谢Zabir Al Nazi,答案是"yes"。设置 tf.random.set_seed() 还会设置 Conv2D 图层的 glorot_uniform 种子。