composite_optimizer.py 2.56 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Copyright 2017 Google Inc. All Rights Reserved.
#
# 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.
# ==============================================================================
Ivan Bogatyy's avatar
Ivan Bogatyy committed
15
"""An optimizer that switches between several methods."""
16
17
import functools

Ivan Bogatyy's avatar
Ivan Bogatyy committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31

import tensorflow as tf
from tensorflow.python.training import optimizer


class CompositeOptimizer(optimizer.Optimizer):
  """Optimizer that switches between several methods.
  """

  def __init__(self,
               optimizer1,
               optimizer2,
               switch,
               use_locking=False,
32
               name="Composite"):
Ivan Bogatyy's avatar
Ivan Bogatyy committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    """Construct a new Composite optimizer.

    Args:
      optimizer1: A tf.python.training.optimizer.Optimizer object.
      optimizer2: A tf.python.training.optimizer.Optimizer object.
      switch: A tf.bool Tensor, selecting whether to use the first or the second
        optimizer.
      use_locking: Bool. If True apply use locks to prevent concurrent updates
        to variables.
      name: Optional name prefix for the operations created when applying
        gradients.  Defaults to "Composite".
    """
    super(CompositeOptimizer, self).__init__(use_locking, name)
    self._optimizer1 = optimizer1
    self._optimizer2 = optimizer2
    self._switch = switch

  def apply_gradients(self, grads_and_vars, global_step=None, name=None):
51
52
53
54
55
    return tf.cond(self._switch,
                   functools.partial(self._optimizer1.apply_gradients,
                                     grads_and_vars, global_step, name),
                   functools.partial(self._optimizer2.apply_gradients,
                                     grads_and_vars, global_step, name))
Ivan Bogatyy's avatar
Ivan Bogatyy committed
56
57

  def get_slot(self, var, name):
58
59
60
61
    if name.startswith("c1-"):
      return self._optimizer1.get_slot(var, name[3:])
    else:
      return self._optimizer2.get_slot(var, name[3:])
Ivan Bogatyy's avatar
Ivan Bogatyy committed
62
63

  def get_slot_names(self):
64
65
66
67
    opt1_names = self._optimizer1.get_slot_names()
    opt2_names = self._optimizer2.get_slot_names()
    return sorted(["c1-{}".format(name) for name in opt1_names] +
                  ["c2-{}".format(name) for name in opt2_names])