google-research
74 строки · 2.5 Кб
1# coding=utf-8
2# Copyright 2024 The Google Research Authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16"""Defines common utilties for variational dropout layers."""
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import print_function
20
21import tensorflow.compat.v1 as tf
22
23
24EPSILON = 1e-8
25
26
27def compute_log_alpha(log_sigma2, theta, eps=EPSILON, value_limit=8.):
28R"""Compute the log \alpha values from \theta and log \sigma^2.
29
30The relationship between \sigma^2, \theta, and \alpha as defined in the
31paper https://arxiv.org/abs/1701.05369 is
32
33\sigma^2 = \alpha * \theta^2
34
35This method calculates the log \alpha values based on this relation.
36
37Args:
38log_sigma2: tf.Variable. The log variance for each weight.
39theta: tf.Variable. The mean for each weight.
40eps: Small constant value to use in log and sqrt operations to avoid NaNs.
41value_limit: If not None, the log_alpha values will be clipped to the
42range [-value_limit, value_limit]. This is consistent with the
43implementation provided with the publication.
44
45Returns:
46A tf.Tensor representing the calculated log \alpha values.
47"""
48log_alpha = log_sigma2 - tf.log(tf.square(theta) + eps)
49
50if value_limit is not None:
51# If a limit is specified, clip the alpha values
52return tf.clip_by_value(log_alpha, -value_limit, value_limit)
53return log_alpha
54
55
56def compute_log_sigma2(log_alpha, theta, eps=EPSILON):
57R"""Compute the log \sigma^2 values from log \alpha and \theta.
58
59The relationship between \sigma^2, \theta, and \alpha as defined in the
60paper https://arxiv.org/abs/1701.05369 is
61
62\sigma^2 = \alpha * \theta^2
63
64This method calculates the log \sigma^2 values based on this relation.
65
66Args:
67log_alpha: tf.Tensor. The log alpha values for each weight.
68theta: tf.Variable. The mean for each weight.
69eps: Small constant value to use in log and sqrt operations to avoid NaNs.
70
71Returns:
72A tf.Tensor representing the calculated log \sigma^2 values.
73"""
74return log_alpha + tf.log(tf.square(theta) + eps)
75