Exemple #1
0
    def get_vars(self):
        """
        Provides access to the model's Variables.
        This may include Variables that are not parameters, such as batch
        norm running moments.
        :return: A list of all Variables defining the model.
        """

        # Catch eager execution and assert function overload.
        try:
            if tf.executing_eagerly():
                raise NotImplementedError(
                    "For Eager execution - get_vars " "must be overridden."
                )
        except AttributeError:
            pass

        done = False
        tried_to_make_params = False
        while not done:
            # Most models in cleverhans use only trainable variables and do not
            # make sure the other collections are updated correctly.
            trainable_vars = tf.get_collection(
                tf.GraphKeys.TRAINABLE_VARIABLES, self.scope + "/"
            )
            # When wrapping other code, such as the CIFAR 10 challenge models,
            # we need to make sure we get the batch norm running averages as well
            # as the trainable variables.
            model_vars = tf.get_collection(
                tf.GraphKeys.MODEL_VARIABLES, self.scope + "/"
            )
            scope_vars = ordered_union(trainable_vars, model_vars)

            if len(scope_vars) > 0:
                done = True
            else:
                assert not tried_to_make_params
                tried_to_make_params = True
                self.make_params()

        # Make sure no variables have been added or removed
        if hasattr(self, "num_vars"):
            assert self.num_vars == len(scope_vars)
        else:
            self.num_vars = len(scope_vars)

        return scope_vars
 def get_params(self):
   out = []
   for layer in self.layers:
     out = ordered_union(out, layer.get_params())
   return out