Different behavior for training and test

6 views
Skip to first unread message

Isaac Gerg

unread,
Mar 3, 2021, 4:01:19 PM3/3/21
to TensorFlow End Users - GETTING STARTED, TUTORIALS & HOW-TO'S
Hi,

I have a layer which I want to behave differently at test time than at training time.  How do I do this with tf.keras?

Upon reading the documentation, I suspected the code below would work, but it doesn't. Please help.

class UniformNoiseLayer(tf.keras.layers.Layer):
    def __init__(self, **kwargs):
        super(UniformNoiseLayer, self).__init__(**kwargs)
    def call(self, input):
        if tf.keras.backend.learning_phase():              
            return input + tf.random.uniform(tf.shape(input), minval= -0.5, maxval = 0.5, dtype=tf.float32, name='uniform_distribution')
        else:
            return tf.round(input)

Isaac Gerg

unread,
Mar 3, 2021, 4:29:09 PM3/3/21
to TensorFlow End Users - GETTING STARTED, TUTORIALS & HOW-TO'S
I believe I was able to fix my issue by reading through the source code.

class UniformNoiseLayer(tf.keras.layers.Layer):
    def __init__(self, **kwargs):
        super(UniformNoiseLayer, self).__init__(**kwargs)
    def call(self, input):
        result = tf.keras.backend.in_train_phase(tf.add(input, tf.random.uniform(tf.shape(input), minval= -0.5, maxval = 0.5, dtype=tf.float32, name='uniform_distribution')),
                                tf.round(input))
        return result

tf.keras.backend.in_train_phase is not documented so I filed a github issue.

Thanks,
Isaac

--
You received this message because you are subscribed to the Google Groups "TensorFlow End Users - GETTING STARTED, TUTORIALS & HOW-TO'S" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tensorflow+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tensorflow/488cbc58-1e31-4a20-a0ce-55e2f93ca751n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages