| 169 | return dense_layer |
| 170 | |
| 171 | def _l2_regularizer(self, scale, scope=None): |
| 172 | if isinstance(scale, numbers.Integral): |
| 173 | raise ValueError(f'Scale cannot be an integer: {scale}') |
| 174 | if isinstance(scale, numbers.Real): |
| 175 | if scale < 0.: |
| 176 | raise ValueError(f'Setting a scale less than 0 on a regularizer: {scale}.') |
| 177 | if scale == 0.: |
| 178 | return lambda _: None |
| 179 | |
| 180 | def l2(weights): |
| 181 | with tf.name_scope(scope, 'l2_regularizer', [weights]) as name: |
| 182 | my_scale = tf.convert_to_tensor(scale, dtype=weights.dtype.base_dtype, name='scale') |
| 183 | return tf.math.multiply(my_scale, tf.nn.l2_loss(weights), name=name) |
| 184 | |
| 185 | return l2 |
| 186 | |
| 187 | def _make_scope(self, name, bf16): |
| 188 | if(bf16): |