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
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
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