# coding=utf-8
# Copyright 2022 The Google Research Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""CIFAR10 ResNet architecture, with customized layers for first PC regularization

Adapted from
https://github.com/keras-team/keras/blob/master/examples/cifar10_resnet.py
"""
import numpy as np
import tensorflow.compat.v2 as tf
from tensorflow.keras import layers
from absl import logging


def power_iterate(x, u_var, num_iter=1):
  """Perform power iteration.

  Args:
    x: A matrix.
    u_var: Stored eigenvector.
    num_iter: The number of iterations to perform power iteration for.

  Returns:
    Amount of variance explained by the first principal component.
  """
  u = u_var
  for _ in range(num_iter):
    u = tf.stop_gradient(u)