示例#1
0
    def _cosine_similarity(self, x, y, eps, view_func):
        x = view_func(x)
        y = view_func(y)

        x_norm = math_ops.euclidean_norm(x, axis=-1) + eps
        y_norm = math_ops.euclidean_norm(y, axis=-1) + eps
        dot = math_ops.reduce_sum(x * y, axis=-1)

        return math_ops.abs(dot) / x_norm / y_norm
示例#2
0
    def layer_true_fn(self, var, perturb, wd_ratio, eps):
        expand_size = [-1] + [1] * (len(var.shape) - 1)
        var_n = var / (array_ops.reshape(math_ops.euclidean_norm(
            self._layer_view(var), axis=-1),
                                         shape=expand_size) + eps)
        perturb -= var_n * array_ops.reshape(math_ops.reduce_sum(
            self._layer_view(var_n * perturb), axis=-1),
                                             shape=expand_size)
        wd = wd_ratio

        return perturb, wd
示例#3
0
    def channel_true_fn(self, var, perturb, wd_ratio, eps):
        expand_size = [-1] + [1] * (len(var.shape) - 1)
        var_n = var / (array_ops.reshape(math_ops.euclidean_norm(
            self._channel_view(var), axis=-1),
                                         shape=expand_size) + eps)
        perturb = state_ops.assign_sub(
            perturb,
            var_n * array_ops.reshape(math_ops.reduce_sum(
                self._channel_view(var_n * perturb), axis=-1),
                                      shape=expand_size))
        wd = wd_ratio

        return perturb, wd