in tensorflow_addons/layers/noisy_dense.py [0:0]
def build(self, input_shape):
# Make sure dtype is correct
dtype = tf.dtypes.as_dtype(self.dtype or K.floatx())
if not (dtype.is_floating or dtype.is_complex):
raise TypeError(
"Unable to build `Dense` layer with non-floating point "
"dtype %s" % (dtype,)
)
input_shape = tf.TensorShape(input_shape)
self.last_dim = tf.compat.dimension_value(input_shape[-1])
sqrt_dim = self.last_dim ** (1 / 2)
if self.last_dim is None:
raise ValueError(
"The last dimension of the inputs to `Dense` "
"should be defined. Found `None`."
)
self.input_spec = InputSpec(min_ndim=2, axes={-1: self.last_dim})
# use factorising Gaussian variables
if self.use_factorised:
mu_init = 1.0 / sqrt_dim
sigma_init = self.sigma / sqrt_dim
# use independent Gaussian variables
else:
mu_init = (3.0 / self.last_dim) ** (1 / 2)
sigma_init = 0.017
sigma_init = initializers.Constant(value=sigma_init)
mu_init = initializers.RandomUniform(minval=-mu_init, maxval=mu_init)
# Learnable parameters
self.sigma_kernel = self.add_weight(
"sigma_kernel",
shape=[self.last_dim, self.units],
initializer=sigma_init,
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint,
dtype=self.dtype,
trainable=True,
)
self.mu_kernel = self.add_weight(
"mu_kernel",
shape=[self.last_dim, self.units],
initializer=mu_init,
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint,
dtype=self.dtype,
trainable=True,
)
self.eps_kernel = self.add_weight(
"eps_kernel",
shape=[self.last_dim, self.units],
initializer=initializers.Zeros(),
regularizer=None,
constraint=None,
dtype=self.dtype,
trainable=False,
)
if self.use_bias:
self.sigma_bias = self.add_weight(
"sigma_bias",
shape=[
self.units,
],
initializer=sigma_init,
regularizer=self.bias_regularizer,
constraint=self.bias_constraint,
dtype=self.dtype,
trainable=True,
)
self.mu_bias = self.add_weight(
"mu_bias",
shape=[
self.units,
],
initializer=mu_init,
regularizer=self.bias_regularizer,
constraint=self.bias_constraint,
dtype=self.dtype,
trainable=True,
)
self.eps_bias = self.add_weight(
"eps_bias",
shape=[
self.units,
],
initializer=initializers.Zeros(),
regularizer=None,
constraint=None,
dtype=self.dtype,
trainable=False,
)
else:
self.sigma_bias = None
self.mu_bias = None
self.eps_bias = None
self.reset_noise()
self.built = True