本文整理匯總了Python中tensorflow.keras.initializers方法的典型用法代碼示例。如果您正苦於以下問題:Python keras.initializers方法的具體用法?Python keras.initializers怎麽用?Python keras.initializers使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tensorflow.keras
的用法示例。
在下文中一共展示了keras.initializers方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: build
# 需要導入模塊: from tensorflow import keras [as 別名]
# 或者: from tensorflow.keras import initializers [as 別名]
def build(self, input_shape=None):
self.layer.input_spec = InputSpec(shape=input_shape)
if hasattr(self.layer, 'built') and not self.layer.built:
self.layer.build(input_shape)
self.layer.built = True
# initialise p
self.p_logit = self.add_weight(name='p_logit', shape=(1,),
initializer=initializers.RandomUniform(self.init_min, self.init_max),
dtype=tf.float32, trainable=True)
self.p = tf.nn.sigmoid(self.p_logit)
tf.compat.v1.add_to_collection("LAYER_P", self.p)
# initialise regularizer / prior KL term
input_dim = tf.reduce_prod(input_shape[1:]) # we drop only last dim
weight = self.layer.kernel
kernel_regularizer = self.weight_regularizer * tf.reduce_sum(tf.square(weight)) / (1. - self.p)
dropout_regularizer = self.p * tf.math.log(self.p)
dropout_regularizer += (1. - self.p) * tf.math.log(1. - self.p)
dropout_regularizer *= self.dropout_regularizer * tf.cast(input_dim, tf.float32)
regularizer = tf.reduce_sum(kernel_regularizer + dropout_regularizer)
self.layer.add_loss(regularizer)
# Add the regularisation loss to collection.
tf.compat.v1.add_to_collection(tf.compat.v1.GraphKeys.REGULARIZATION_LOSSES, regularizer)
示例2: initializer_factory
# 需要導入模塊: from tensorflow import keras [as 別名]
# 或者: from tensorflow.keras import initializers [as 別名]
def initializer_factory(initializer_or_name_with_params
) -> Union[keras.initializers.Initializer, None]:
"""
Factory to construct keras initializer from its name or config
Parameters
----------
initializer_or_name_with_params
initilaizer itself, then returned as is, or the name of initializer
from keras.initializers namespace or config with name and params
to pass to constructor
Returns
-------
initializer
initializer
"""
if isinstance(initializer_or_name_with_params,
keras.initializers.Initializer):
return initializer_or_name_with_params
if initializer_or_name_with_params is None:
return None
name_with_params = initializer_or_name_with_params
name, params = _get_name_and_params(name_with_params, 'RandomNormal')
if not hasattr(keras.initializers, name):
assert ("Use names from keras.initializers class names as initializer "
"name (got {})".format(name))
initializer = getattr(keras.initializers, name)(**params)
return initializer