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?

点点圈 提交于 2020-05-15 02:58:45

问题


I'm currently training a convolutional neural network using a conv2D layer defined like this:

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

My understanding is that the default kernel_initializer is glorot_uniform which has a default seed of '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)

I'm trying to produce reproducible code and have already set random seeds as per this StackOverflow post:

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)

Is the tf.random.set_seed seed number used by glorot_uniform within a conv2D layer? If not, how would that seed be defined when defining the conv2D layer?


回答1:


For each layer, you can use the seed for kernel and bias initializers.

You can seed your initializer separately,

kernel_initializer=initializers.glorot_uniform(seed=0))

From documentation:

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.




回答2:


Thanks to Zabir Al Nazi, the answer is "yes". Setting tf.random.set_seed() also sets the Conv2D layer's glorot_uniform seed.



来源:https://stackoverflow.com/questions/61364793/does-setting-the-seed-in-tf-random-set-seed-also-set-the-seed-used-by-the-glorot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!