rfho/hyper_gradients.py
def backward(self, T, val_feed_dict_suppliers=None, train_feed_dict_supplier=None, hyper_batch_step=None,
                 summary_utils=None, check_if_zero=False):
        """
        Performs backward computation of hyper-gradients

        :param hyper_batch_step: supports for stochastic sampling of validation set
        :param T: Total number of iterations
        :param val_feed_dict_suppliers: either a callable that returns a feed_dict
                                        or a dictionary {validation_error tensor: callable
                                            (signature `t -> feed_dict` or `() -> feed_dict`) that
                                        is used to initialize the dual variables `p` (generally supplier of
                                        validation example set).

        :param train_feed_dict_supplier: (optional) A callable with signature `t -> feed_dict` or `() -> feed_dict` to
                                            pass to `tf.Session.run`
                                    feed_dict argument
        :param summary_utils: (optional) object that implements a method method `run(tf.Session, step)`
                                that is executed at every iteration (see for instance utils.PrintUtils
        :param check_if_zero: (optional) debug flag
        :return: A dictionary of lists of step-wise hyper-gradients. In usual application the "true" hyper-gradients
                 can be obtained with method std_collect_hyper_gradients
        """
        if not train_feed_dict_supplier:
            # noinspection PyUnusedLocal
            train_feed_dict_supplier = lambda: None
        if not val_feed_dict_suppliers:  # FIXME probably won't work with the current settings.
            val_feed_dict_suppliers = lambda: None
        else:
            if not isinstance(val_feed_dict_suppliers, dict) and len(self.val_error_dict.keys()) == 1:
                # cast validation supplier into a dict
                val_feed_dict_suppliers = {list(self.val_error_dict.keys())[0]: val_feed_dict_suppliers}

        # compute alpha_T using the validation set
        [tf.variables_initializer([self.p_dict[ve]]).run(feed_dict=cmo(data_supplier, hyper_batch_step))
         for ve, data_supplier in val_feed_dict_suppliers.items()]

        # set hyper-derivatives to 0
        hyper_derivatives = self._initialize_hyper_derivatives_res()  # TODO deal better with the hyper-derivatives
        ss = tf.get_default_session()

        if summary_utils: summary_utils.run(ss, T)

        for t in range(T - 1, -1, -1):
            self.global_step.decrease.eval()

            # revert w_t to w_(t-1)
            ss.run(self._back_hist_op, feed_dict={self._w_placeholder: self.w_hist[t]})

            # noinspection PyNoneFunctionAssignment
            fds = cmo(train_feed_dict_supplier, self.global_step.eval())
            # TODO read below  (maybe use tf.control_dependencies ... would it speed this up?)
            """ Unfortunately it looks like that the following two lines cannot be run together (will this
            degrade the performances???"""

            if check_if_zero:  # debug
                if self._abs_sum_p.eval() < 1.e-20:
                    # ss.run([self.bk_ops, self.global_step.decrease], feed_dict=fds)
                    print('exiting backward pass at iteration %d.' % t)
                    return {k: list(reversed(v)) for k, v in hyper_derivatives.items()}

            # compute partial results for hyper_derivatives: alpha_t*B_t and concatenates them
            mid_res = ss.run([e[1] for e in self.hyper_derivatives], feed_dict=fds)
            for k in range(len(mid_res)):
                hyper_list = self.hyper_derivatives[k][0]
                mr = mid_res[k]
                for j in range(len(hyper_list)):
                    hyper_derivatives[hyper_list[j]].append(mr[j])

            # computes alpha_t = alpha_(t+1)*A_(t+1)

            ss.run([self._bk_ops], feed_dict=fds)  # check this global_step here.. (for Adam)

            if summary_utils: summary_utils.run(ss, t)

        hyper_derivatives = {k: list(reversed(v)) for k, v in hyper_derivatives.items()}

        # updates also variables that keep track of hyper-gradients
        [self._hyper_assign_ops[h].eval(feed_dict={self._grad_wrt_hypers_placeholder: ghv})
         for h, ghv in ReverseHG.std_collect_hyper_gradients(hyper_derivatives).items()]

        return hyper_derivatives

        Performs backward computation of hyper-gradients

        :param hyper_batch_step: supports for stochastic sampling of validation set
        :param T: Total number of iterations
        :param val_feed_dict_suppliers: either a callable that returns a feed_dict
                                        or a dictionary {validation_error tensor: callable
                                            (signature `t -> feed_dict` or `() -> feed_dict`) that
                                        is used to initialize the dual variables `p` (generally supplier of
                                        validation example set).

        :param train_feed_dict_supplier: (optional) A callable with signature `t -> feed_dict` or `() -> feed_dict` to
                                            pass to `tf.Session.run`
                                    feed_dict argument
        :param summary_utils: (optional) object that implements a method method `run(tf.Session, step)`
                                that is executed at every iteration (see for instance utils.PrintUtils
        :param check_if_zero: (optional) debug flag
        :return: A dictionary of lists of step-wise hyper-gradients. In usual application the "true" hyper-gradients
                 can be obtained with method std_collect_hyper_gradients
        
Performs backward computation of hyper-gradients
Performs backward computation of hyper - gradients
def backward(self, T, val_feed_dict_suppliers=None, train_feed_dict_supplier=None, hyper_batch_step=None,
                 summary_utils=None, check_if_zero=False):
        if not train_feed_dict_supplier:
            train_feed_dict_supplier = lambda: None
        if not val_feed_dict_suppliers:  
            val_feed_dict_suppliers = lambda: None
        else:
            if not isinstance(val_feed_dict_suppliers, dict) and len(self.val_error_dict.keys()) == 1:
                val_feed_dict_suppliers = {list(self.val_error_dict.keys())[0]: val_feed_dict_suppliers}
        [tf.variables_initializer([self.p_dict[ve]]).run(feed_dict=cmo(data_supplier, hyper_batch_step))
         for ve, data_supplier in val_feed_dict_suppliers.items()]
        hyper_derivatives = self._initialize_hyper_derivatives_res()  
        ss = tf.get_default_session()
        if summary_utils: summary_utils.run(ss, T)
        for t in range(T - 1, -1, -1):
            self.global_step.decrease.eval()
            ss.run(self._back_hist_op, feed_dict={self._w_placeholder: self.w_hist[t]})
            fds = cmo(train_feed_dict_supplier, self.global_step.eval())
            if check_if_zero:  
                if self._abs_sum_p.eval() < 1.e-20:
                    print('exiting backward pass at iteration %d.' % t)
                    return {k: list(reversed(v)) for k, v in hyper_derivatives.items()}
            mid_res = ss.run([e[1] for e in self.hyper_derivatives], feed_dict=fds)
            for k in range(len(mid_res)):
                hyper_list = self.hyper_derivatives[k][0]
                mr = mid_res[k]
                for j in range(len(hyper_list)):
                    hyper_derivatives[hyper_list[j]].append(mr[j])
            ss.run([self._bk_ops], feed_dict=fds)  
            if summary_utils: summary_utils.run(ss, t)
        hyper_derivatives = {k: list(reversed(v)) for k, v in hyper_derivatives.items()}
        [self._hyper_assign_ops[h].eval(feed_dict={self._grad_wrt_hypers_placeholder: ghv})
         for h, ghv in ReverseHG.std_collect_hyper_gradients(hyper_derivatives).items()]
        return hyper_derivatives
def backward ( self , T , val_feed_dict_suppliers = None , train_feed_dict_supplier = None , hyper_batch_step = None , summary_utils = None , check_if_zero = False ) : if not train_feed_dict_supplier : train_feed_dict_supplier = lambda : None if not val_feed_dict_suppliers : val_feed_dict_suppliers = lambda : None else : if not isinstance ( val_feed_dict_suppliers , dict ) and len ( self . val_error_dict . keys ( ) ) == 1 : val_feed_dict_suppliers = { list ( self . val_error_dict . keys ( ) ) [ 0 ] : val_feed_dict_suppliers } [ tf . variables_initializer ( [ self . p_dict [ ve ] ] ) . run ( feed_dict = cmo ( data_supplier , hyper_batch_step ) ) for ve , data_supplier in val_feed_dict_suppliers . items ( ) ] hyper_derivatives = self . _initialize_hyper_derivatives_res ( ) ss = tf . get_default_session ( ) if summary_utils : summary_utils . run ( ss , T ) for t in range ( T - 1 , - 1 , - 1 ) : self . global_step . decrease . eval ( ) ss . run ( self . _back_hist_op , feed_dict = { self . _w_placeholder : self . w_hist [ t ] } ) fds = cmo ( train_feed_dict_supplier , self . global_step . eval ( ) ) """ Unfortunately it looks like that the following two lines cannot be run together (will this
            degrade the performances???""" if check_if_zero : if self . _abs_sum_p . eval ( ) < 1.e-20 : print ( 'exiting backward pass at iteration %d.' % t ) return { k : list ( reversed ( v ) ) for k , v in hyper_derivatives . items ( ) } mid_res = ss . run ( [ e [ 1 ] for e in self . hyper_derivatives ] , feed_dict = fds ) for k in range ( len ( mid_res ) ) : hyper_list = self . hyper_derivatives [ k ] [ 0 ] mr = mid_res [ k ] for j in range ( len ( hyper_list ) ) : hyper_derivatives [ hyper_list [ j ] ] . append ( mr [ j ] ) ss . run ( [ self . _bk_ops ] , feed_dict = fds ) if summary_utils : summary_utils . run ( ss , t ) hyper_derivatives = { k : list ( reversed ( v ) ) for k , v in hyper_derivatives . items ( ) } [ self . _hyper_assign_ops [ h ] . eval ( feed_dict = { self . _grad_wrt_hypers_placeholder : ghv } ) for h , ghv in ReverseHG . std_collect_hyper_gradients ( hyper_derivatives ) . items ( ) ] return hyper_derivatives
Performs backward computation of hyper-gradients
Performs backward computation of hyper - gradients
"""
        Performs backward computation of hyper-gradients

        :param hyper_batch_step: supports for stochastic sampling of validation set
        :param T: Total number of iterations
        :param val_feed_dict_suppliers: either a callable that returns a feed_dict
                                        or a dictionary {validation_error tensor: callable
                                            (signature `t -> feed_dict` or `() -> feed_dict`) that
                                        is used to initialize the dual variables `p` (generally supplier of
                                        validation example set).

        :param train_feed_dict_supplier: (optional) A callable with signature `t -> feed_dict` or `() -> feed_dict` to
                                            pass to `tf.Session.run`
                                    feed_dict argument
        :param summary_utils: (optional) object that implements a method method `run(tf.Session, step)`
                                that is executed at every iteration (see for instance utils.PrintUtils
        :param check_if_zero: (optional) debug flag
        :return: A dictionary of lists of step-wise hyper-gradients. In usual application the "true" hyper-gradients
                 can be obtained with method std_collect_hyper_gradients
        """ # noinspection PyUnusedLocal # FIXME probably won't work with the current settings. # cast validation supplier into a dict # compute alpha_T using the validation set # set hyper-derivatives to 0 # TODO deal better with the hyper-derivatives # revert w_t to w_(t-1) # noinspection PyNoneFunctionAssignment # TODO read below  (maybe use tf.control_dependencies ... would it speed this up?) """ Unfortunately it looks like that the following two lines cannot be run together (will this
            degrade the performances???""" # debug # ss.run([self.bk_ops, self.global_step.decrease], feed_dict=fds) # compute partial results for hyper_derivatives: alpha_t*B_t and concatenates them # computes alpha_t = alpha_(t+1)*A_(t+1) # check this global_step here.. (for Adam) # updates also variables that keep track of hyper-gradients
A dictionary of lists of step-wise hyper-gradients. In usual application the "true" hyper-gradients
can be obtained with method std_collect_hyper_gradients
A dictionary of lists of step - wise hyper - gradients . In usual application the " true " hyper - gradients can be obtained with method std_collect_hyper_gradients
self
T
Total number of iterations
Total number of iterations
val_feed_dict_suppliers
either a callable that returns a feed_dict
or a dictionary {validation_error tensor: callable
(signature `t -> feed_dict` or `() -> feed_dict`) that
is used to initialize the dual variables `p` (generally supplier of
validation example set).
either a callable that returns a feed_dict or a dictionary { validation_error tensor : callable ( signature ` t - > feed_dict ` or ` () - > feed_dict ` ) that is used to initialize the dual variables ` p ` ( generally supplier of validation example set ) .
train_feed_dict_supplier
(optional) A callable with signature `t -> feed_dict` or `() -> feed_dict` to
pass to `tf.Session.run`
feed_dict argument
( optional ) A callable with signature ` t - > feed_dict ` or ` () - > feed_dict ` to pass to ` tf . Session . run ` feed_dict argument
hyper_batch_step
supports for stochastic sampling of validation set
supports for stochastic sampling of validation set
summary_utils
(optional) object that implements a method method `run(tf.Session, step)`
that is executed at every iteration (see for instance utils.PrintUtils
( optional ) object that implements a method method ` run ( tf . Session step ) ` that is executed at every iteration ( see for instance utils . PrintUtils
check_if_zero
(optional) debug flag
( optional ) debug flag