<filename>tensorflow_federated/python/learning/optimizers/optimizer_test_utils.py<gh_stars>1-10
# Copyright 2021, The TensorFlow Federated 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.
"""Utilities for implementation and testing of optimizers."""

import tensorflow as tf

# Positive-definite matrix with maximal eigenvalue approximately 13.3 and
# condition number approximately 260, generated by:
# np.random.seed(42)
# X = np.random.randn(5, 5)
# A = np.matmul(X, np.transpose(X))
_TEST_MATRIX = [
    [3.05979016, -0.67965499, -2.51916374, -0.98797549, -1.23950244],
    [-0.67965499, 3.65246689, -0.47892836, -1.56662479, -0.27436542],
    [-2.51916374, -0.47892836, 7.12618873, 4.98172794, 3.10724048],
    [-0.98797549, -1.56662479, 4.98172794, 4.259855, 1.48831719],
    [-1.23950244, -0.27436542, 3.10724048, 1.48831719, 4.52992126],
]

_ITITIAL_W = [
    [-0.60170661],
    [1.85227818],
    [-0.01349722],
    [-1.05771093],
    [0.82254491],
]


def test_quadratic_problem():
  """Returns a test problem.

  The test problem is a 5-dimensional quadratic problem of form
  `(1/2) * w^t * A * w` with the optimum in w* = (0.0, 0.0, 0.0, 0.0, 0.0). The
  matrix A is positive-definite with maximal eigenvalue approximately 13.3 and
  condition number approximately 260.

  It is meant to be used as a simple deterministic problem to be used when unit
  testing optimizers.

  Returns:
    A tuple (initial_w, f, grad_w), where these are initial point for
    optimization and one-arg callables returning the function value and gradient
    given a point w, respectively.
  """

  @tf.function
  def fn(w):
    a = tf.constant(_TEST_MATRIX, tf.float32)
    return tf.matmul(tf.matmul(w, a, transpose_a=True), w) / 2

  @tf.function
  def grad_fn(w):
    a = tf.constant(_TEST_MATRIX, tf.float32)
    return tf.matmul(a, w)

  initial_w = tf.constant(_ITITIAL_W, tf.float32)

  return initial_w, fn, grad_fn

<filename>tensorflow_federated/python/learning/optimizers/optimizer_test_utils.py<gh_stars>1-10
# Copyright 2021, The TensorFlow Federated 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.
"""Utilities for implementation and testing of optimizers."""

import tensorflow as tf

# Positive-definite matrix with maximal eigenvalue approximately 13.3 and
# condition number approximately 260, generated by:
# np.random.seed(42)
# X = np.random.randn(5, 5)
# A = np.matmul(X, np.transpose(X))
_TEST_MATRIX = [
    [3.05979016, -0.67965499, -2.51916374, -0.98797549, -1.23950244],
    [-0.67965499, 3.65246689, -0.47892836, -1.56662479, -0.27436542],
    [-2.51916374, -0.47892836, 7.12618873, 4.98172794, 3.10724048],
    [-0.98797549, -1.56662479, 4.98172794, 4.259855, 1.48831719],
    [-1.23950244, -0.27436542, 3.10724048, 1.48831719, 4.52992126],
]

_ITITIAL_W = [
    [-0.60170661],
    [1.85227818],
    [-0.01349722],
    [-1.05771093],
    [0.82254491],
]


def test_quadratic_problem():
  """Returns a test problem.

  The test problem is a 5-dimensional quadratic problem of form
  `(1/2) * w^t * A * w` with the optimum in w* = (0.0, 0.0, 0.0, 0.0, 0.0). The
  matrix A is positive-definite with maximal eigenvalue approximately 13.3 and
  condition number approximately 260.

  It is meant to be used as a simple deterministic problem to be used when unit
  testing optimizers.

  Returns:
    A tuple (initial_w, f, grad_w), where these are initial point for
    optimization and one-arg callables returning the function value and gradient
    given a point w, respectively.
  """

  @tf.function
  def fn(w):
    a = tf.constant(_TEST_MATRIX, tf.float32)
    return tf.matmul(tf.matmul(w, a, transpose_a=True), w) / 2

  @tf.function
  def grad_fn(w):
    a = tf.constant(_TEST_MATRIX, tf.float32)
    return tf.matmul(a, w)

  initial_w = tf.constant(_ITITIAL_W, tf.float32)

  return initial_w, fn, grad_fn

# Copyright 2021, The TensorFlow Federated 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. Utilities for implementation and testing of optimizers. # Positive-definite matrix with maximal eigenvalue approximately 13.3 and # condition number approximately 260, generated by: # np.random.seed(42) # X = np.random.randn(5, 5) # A = np.matmul(X, np.transpose(X)) Returns a test problem.    The test problem is a 5-dimensional quadratic problem of form   `(1/2) * w^t * A * w` with the optimum in w* = (0.0, 0.0, 0.0, 0.0, 0.0). The   matrix A is positive-definite with maximal eigenvalue approximately 13.3 and   condition number approximately 260.    It is meant to be used as a simple deterministic problem to be used when unit   testing optimizers.    Returns:     A tuple (initial_w, f, grad_w), where these are initial point for     optimization and one-arg callables returning the function value and gradient     given a point w, respectively.