defdropout_layer(one_layer: tf.tensor, dropout_probability: float): assert0 <= dropout_probability <= 1 if dropout_probability == 1: # in this case, all elements are dropped out return tf.zeros_like(one_layer) # tf.zeros_like: create a tensor with all elements set to zero
if dropout_probability == 0: # in this case, all elements are kept return one_layer
# tf.random.uniform: outputs random values from a uniform distribution. # with (1-p)'s probability that value will become x / (1-p) mask = tf.random.uniform(shape=tf.shape(one_layer), minval=0, maxval=1) < (1 - dropout_probability)