Use custom getter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# source: https://docs.nvidia.com/deeplearning/sdk/mixed-precision-training/index.html | |
def float32_variable_storage_getter(getter, name, shape=None, dtype=None, | |
initializer=None, regularizer=None, | |
trainable=True, | |
*args, **kwargs): | |
storage_dtype = tf.float32 if trainable else dtype | |
variable = getter(name, shape, dtype=storage_dtype, | |
initializer=initializer, regularizer=regularizer, | |
trainable=trainable, | |
*args, **kwargs) | |
if trainable and dtype != tf.float32: | |
variable = tf.cast(variable, dtype) | |
return variable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment