Exemplo n.º 1
0
    def initialize(self, no_scratch=False):
        """Fetch record then uses tf's saver.restore."""
        if self.do_restore:

            # First, determine which checkpoint to use.
            if self.from_ckpt is not None:
                # Use a cached checkpoint file.
                ckpt_filename = self.from_ckpt
                log.info('Restoring variables from checkpoint %s ...' % ckpt_filename)
            else:
                # Otherwise, use a database checkpoint.
                self.load_rec() if self.load_data is None else None
                if self.load_data is not None:
                    rec, ckpt_filename = self.load_data
                    log.info('Restoring variables from record %s (step %d)...' %
                             (str(rec['_id']), rec['step']))
                else:
                    # No db checkpoint to load.
                    ckpt_filename = None

            if ckpt_filename is not None:

                all_vars = tf.global_variables() + tf.local_variables()  # get list of all variables
                self.all_vars = strip_prefix(self.params['model_params']['prefix'], all_vars)

                # Next, determine which vars should be restored from the specified checkpoint.
                restore_vars = self.get_restore_vars(ckpt_filename, self.all_vars)
                restore_stripped = strip_prefix(self.params['model_params']['prefix'], list(restore_vars.values()))
                restore_names =  [name for name, var in restore_stripped.items()]
                # Actually load the vars.
                log.info('Restored Vars:\n' + str(restore_names))
                tf_saver_restore = tf.train.Saver(restore_vars)
                tf_saver_restore.restore(self.sess, ckpt_filename)
                log.info('... done restoring.')

                # Reinitialize all other, unrestored vars.
                unrestored_vars = [var for name, var in self.all_vars.items() if name not in restore_names]
                unrestored_var_names = [name for name, var in self.all_vars.items() if name not in restore_names]
                log.info('Unrestored Vars:\n' + str(unrestored_var_names))
                self.sess.run(tf.variables_initializer(unrestored_vars))  # initialize variables not restored
                assert len(self.sess.run(tf.report_uninitialized_variables())) == 0, (
                    self.sess.run(tf.report_uninitialized_variables()))

        if not self.do_restore or (self.load_data is None and self.from_ckpt is None):
            init_op_global = tf.global_variables_initializer()
            self.sess.run(init_op_global)
            init_op_local = tf.local_variables_initializer()
            self.sess.run(init_op_local)
Exemplo n.º 2
0
    def get_restore_vars(self, save_file, all_vars=None):
        """Create the `var_list` init argument to tf.Saver from save_file.

        Extracts the subset of variables from tf.global_variables that match the
        name and shape of variables saved in the checkpoint file, and returns these
        as a list of variables to restore.

        To support multi-model training, a model prefix is prepended to all
        tf global_variable names, although this prefix is stripped from
        all variables before they are saved to a checkpoint. Thus,


        Args:
            save_file: path of tf.train.Saver checkpoint.

        Returns:
            dict: checkpoint variables.

        """
        reader = tf.train.NewCheckpointReader(save_file)
        var_shapes = reader.get_variable_to_shape_map()
        log.info('Saved Vars:\n' + str(var_shapes.keys()))

        var_shapes = {  # Strip the prefix off saved var names.
            strip_prefix_from_name(self.params['model_params']['prefix'], name): shape
            for name, shape in var_shapes.items()}

        # Map old vars from checkpoint to new vars via load_param_dict.
        mapped_var_shapes = self.remap_var_list(var_shapes)
        log.info('Saved shapes:\n' + str(mapped_var_shapes))

        if all_vars is None:
            all_vars = tf.global_variables() + tf.local_variables()  # get list of all variables
            all_vars = strip_prefix(self.params['model_params']['prefix'], all_vars)

        # Specify which vars are to be restored vs. reinitialized.
        if self.load_param_dict is None:
            restore_vars = {name: var for name, var in all_vars.items() if name in mapped_var_shapes}
        else:
            # associate checkpoint names with actual variables
            load_var_dict = {}
            for ckpt_var_name, curr_var_name in self.load_param_dict.items():
                for curr_name, curr_var in all_vars.items():
                    if curr_name == curr_var_name:
                        load_var_dict[ckpt_var_name] = curr_var
                        break

            restore_vars = load_var_dict

        restore_vars = self.filter_var_list(restore_vars)

        # Ensure the vars to restored have the correct shape.
        var_list = {}
        for name, var in restore_vars.items():
            var_shape = var.get_shape().as_list()
            if var_shape == mapped_var_shapes[name]:
                var_list[name] = var
        return var_list
Exemplo n.º 3
0
def test_from_params(load_params,
                     model_params,
                     validation_params,
                     log_device_placement=False,
                     save_params=None,
                     dont_run=False,
                     skip_check=False,
                     ):
    """
    Main testing interface function.

    Same as train_from_parameters; but just performs testing without training.

    For documentation, see argument descriptions in train_from_params.

    """
    params, test_args = parse_params(
            'test',
            model_params,
            dont_run=dont_run,
            skip_check=skip_check,
            save_params=save_params,
            load_params=load_params,
            validation_params=validation_params,
            log_device_placement=log_device_placement,
            )

    with tf.Graph().as_default(), tf.device(DEFAULT_HOST):

        # create session
        sess = tf.Session(
                config=tf.ConfigProto(
                    allow_soft_placement=True,
                    log_device_placement=log_device_placement,
                    ))

        init_op_global = tf.global_variables_initializer()
        sess.run(init_op_global)
        init_op_local = tf.local_variables_initializer()
        sess.run(init_op_local)
        log.info('Initialized from scratch first')

        # For convenience, use list of dicts instead of dict of lists
        _params = [{key: value[i] for (key, value) in params.items()}
                   for i in range(len(params['model_params']))]
        _ttargs = [{key: value[i] for (key, value) in test_args.items()}
                   for i in range(len(params['model_params']))]

        # Build a graph for each distinct model.
        for param, ttarg in zip(_params, _ttargs):

            if not 'cache_dir' in load_params:
                temp_cache_dir = save_params.get('cache_dir', None)
                load_params['cache_dir'] = temp_cache_dir
                log.info('cache_dir not found in load_params, using cache_dir ({}) from save_params'.format(temp_cache_dir))

            ttarg['dbinterface'] = DBInterface(params=param, load_params=param['load_params'])
            ttarg['dbinterface'].load_rec()
            ld = ttarg['dbinterface'].load_data
            assert ld is not None, "No load data found for query, aborting"
            ld = ld[0]
            # TODO: have option to reconstitute model_params entirely from
            # saved object ("revivification")
            param['model_params']['seed'] = ld['params']['model_params']['seed']
            cfg_final = ld['params']['model_params']['cfg_final']

            ttarg['validation_targets'] = \
                    get_valid_targets_dict(
                        loss_params=None,
                        cfg_final=cfg_final,
                        **param)

            # tf.get_variable_scope().reuse_variables()

            param['load_params']['do_restore'] = True
            param['model_params']['cfg_final'] = cfg_final

            prefix = param['model_params']['prefix'] + '/'
            all_vars = variables._all_saveable_objects()
            var_list = strip_prefix(prefix, all_vars)

            ttarg['dbinterface'] = DBInterface(sess=sess,
                                               params=param,
                                               var_list=var_list,
                                               load_params=param['load_params'],
                                               save_params=param['save_params'])
            ttarg['dbinterface'].initialize(no_scratch=True)
            ttarg['save_intermediate_freq'] = param['save_params'].get('save_intermediate_freq')

        # Convert back to a dictionary of lists
        params = {key: [param[key] for param in _params]
                  for key in _params[0].keys()}
        test_args = {key: [ttarg[key] for ttarg in _ttargs]
                     for key in _ttargs[0].keys()}

        if dont_run:
            return test_args

        res = test(sess, **test_args)
        sess.close()
        return res
Exemplo n.º 4
0
def train_from_params(
        save_params,
        model_params,
        train_params,
        loss_params=None,
        learning_rate_params=None,
        optimizer_params=None,
        validation_params=None,
        load_params=None,
        log_device_placement=DEFAULT_PARAMS[
            'log_device_placement'],  # advanced
        dont_run=DEFAULT_PARAMS['dont_run'],  # advanced
        skip_check=DEFAULT_PARAMS['skip_check'],  # advanced
):
    """
    Main training interface function.

    Args:
        save_params (dict): 
            Describing the parameters used to construct the save database, and
            control saving. These include:

            - host (str)
                Hostname where database connection lives
            - port (int)
                Port where database connection lives
            - dbname (str)
                Name of database for storage
            - collname (str)
                Name of collection for storage
            - exp_id (str)
                Experiment id descriptor
                NOTE: the variables host/port/dbname/coll/exp_id control
                the location of the saved data for the run, in order of
                increasing specificity.  When choosing these, note that:

                - If a given host/port/dbname/coll/exp_id already has saved checkpoints,\
                then any new call to start training with these same location variables\
                will start to train from the most recent saved checkpoint.  If you mistakenly\
                try to start training a new model with different variable names, or structure,\
                from that existing checkpoint, an error will be raised, as the model will be\
                incompatiable with the saved variables.

                - When choosing what dbname, coll, and exp_id, to use, keep in mind that mongodb\
                queries only operate over a single collection.  So if you want to analyze\
                results from a bunch of experiments together using mongod queries, you should\
                put them all in the same collection, but with different exp_ids. If, on the\
                other hand, you never expect to analyze data from two experiments together,\
                you can put them in different collections or different databases. Choosing\
                between putting two experiments in two collections in the same database\
                or in two totally different databases will depend on how you want to organize\
                your results and is really a matter of preference.

            - do_save (bool, default: True)
                Whether to save to database
            - save_initial_filters (bool, default: True)
                Whether to save initial model filters at step = 0,
            - save_metrics_freq (int, default: 5)
                How often to store train results to database
            - save_valid_freq (int, default: 3000)
                How often to calculate and store validation results
                to database
            - save_filters_freq (int, default: 30000)
                How often to save filter values to database
            - cache_filters_freq (int, default: 3000)
                How often to cache filter values locally and save
                to ___RECENT database
            - cache_max_num (int, default: 6)
                Maximal number of cached filters to keep in __RECENT database
            - cache_dir (str, default: None)
                Path where caches will be saved locally. If None, will default to
                ~/.tfutils/<host:post>/<dbname>/<collname>/<exp_id>.

        model_params (dict): Containing function that produces model and arguments to that function.

            - model_params['func'] 
                The function producing the model.

                The function's signature is:

                Args:

                - ``inputs``: data object
                - ``train`` (boolean): if in training or testing 
                - ``seed`` (int): seed for use in random generation

                Returns:

                - ``outputs`` (tf.Operations): train output tensorflow nodes
                - Additional configurations you want to store in database

            - Remaining items in model_params are dictionary of arguments passed to func.

        train_params (dict): Containing params for data sources and targets in training.

            - train_params['data_params'] 
                This contains params for the data

                - ``train_params['data_params']['func']`` is the function that constructs the data:

                    The function's signature is:

                    Args:

                    - ``batch_size``: Batch size for input data

                    Returns:

                    - ``inputs``: A dictionary of tensors that will be sent to model function

                - ``train_params['data_params']['batch_size']`` batch size of the data, will be sent to func

                - Remainder of ``train_params['data_params']`` are kwargs passed to func

            - train_params['targets'] (optional) 
                contains params for additional train targets

                - ``train_params['targets']['func']`` is a function that produces tensorflow nodes as training targets:

                    The function's signature is:

                    Args:

                    - ``inputs``: returned values of ``train_params['data_params']['func']``
                    - ``output``: first returned value of ``train_params['model_params']['func']``

                    Returns:

                    A dictionary of tensors that will be computed and stored in the database

                - Remainder of ``train_parms['targets']`` are arguments to func.

            - train_params['validate_first'] (optional, bool, default is True):
                controls whether validating before training

            - train_params['thres_loss'] (optional, float, default: 100): 
                If loss exceeds this during training, HiLossError is thrown

            - train_params['num_steps'] (int or None, default: None): 
                How many total steps of the optimization are run.
                If None, train is run until process is cancelled.

        loss_params (dict): Parameters for helper.get_loss_base function to build loss.

            - loss_params['pred_targets'] (a string or a list of strings):
                contain the names of inputs nodes that will be sent into the loss function

            - loss_params['loss_func']:
                the function used to calculate the loss. Must be provided.

            - loss_params['loss_func_kwargs'] (dict):
                Keyword parameters sent to ``loss_params['loss_func']``. Default is {}.

            - loss_params['agg_func']:
                The aggregate function, default is None.

            - loss_params['agg_func_kwargs']: 
                Keyword parameters sent to ``loss_params['agg_func']``. Default is {}.

            - loss_params['loss_per_case_func'] (Deprecated):
                Deprecated parameter, the same as ``loss_params['loss_func']``.

            - loss_params['targets'] (Deprecated):
                Deprecated parameter, the same as ``loss_params['targets']``.

        learning_rate_params (dict): Parameters for specifying learning_rate.

            - learning_rate_params['func']:
                The function producing tensorflow node acting as learning rate. 
                This function must accept argument ``global_step``.

            - remainder of learning_rate_params are arguments to func.

        optimizer_params (dict): Parameters for creating optimizer.

            - optimizer_params['optimizer']:
                A class producing an optimizer object, 
                which should have function ``compute_gradients`` and ``apply_gradients``. 
                The signatures of these two functions are similar as tensorflow basic optimizer classes.

                Must accept:

                - "learning_rate" -- the result of the learning_rate_func call

                - Remainder of optimizer_params (aside form "optimizer") are arguments
                  to the optimizer func

            - optimizer_params['func'] (Deprecated):
                Deprecated parameter, the same as ``optimizer_params['optimizer']``.

        validation_params (dict): Dictionary of validation sources. The structure if this dictionary is:

            {
                <validation_target_name_1>: {
                    data: {
                        'func': (callable) data source function for this validation,

                        <kwarg1>: <value1> for 'func',

                        ...
                        },
                    targets: {
                        'func': (callable) returning targets,

                        <kwarg1>: <value1> for 'func',

                        ...
                        },
                    num_steps (int): 
                        number of batches of validation source to compute,
                    agg_func (optional, callable):  
                        how to aggregate validation results
                        across batches after computation. Signature is:

                            - one input argument: the list of validation batch results
                            - one output: aggregated version
                        Default is ``utils.identity_func``
                    online_agg_func (optional, callable):  
                        how to aggregate validation results
                        on a per-batch basis. Siganture is:

                            - three input arguments: (current aggregate, new result, step)
                            - one output: new aggregated result
                        On first step, current aggregate passed in is None.
                        The final result is passed to the "agg_func".
                        Default is ``utils.append_and_return``
                },

                <validation_target_name_2>: ...
            }

            For each validation_target_name key, the targets are computed and then added to
            the output dictionary to be computed every so often -- unlike train_targets which
            are computed on each time step, these are computed on a basic controlled by the
            valid_save_freq specific in the save_params.

        load_params (dict):
            Similar to save_params, if you want loading to happen from a different
            location than where saving occurs. Parameters include:

            - host (str)
                Hostname where database connection lives
            - port (int)
                Port where database connection lives
            - dbname (str)
                Name of database for storage
            - collname (str)
                Name of collection for storage
            - exp_id (str)
                Experiment id descriptor
            - do_restore (bool, default: True)
                Whether to restore from saved model
            - query (dict)
                mongodb query describing how to load from loading database
            - from_ckpt (string)
                Path to load from a TensorFlow checkpoint (instead of from the db)
            - to_restore (list of strings or a regex/callable which returns strings)
                Specifies which variables should be loaded from the checkpoint.
                Any variables not specified here will be reinitialized.
            - load_param_dict (dict)
                A dictionary whose keys are the names of the variables that are to be loaded
                from the checkpoint, and the values are the names of the variables of the model
                that you want to restore with the value of the corresponding checkpoint variable.

        log_device_placement (bool, default is False): 
            Advanced parameter. Whether to log device placement in tensorflow session

        dont_run (bool, default is False): 
            Advanced parameter. Whether returning everything, not actually training 

        skip_check (bool, default is False): 
            Advanced parameter. Whether skipping github check, could be useful when working in detached head

    """
    params, train_args = parse_params(
        'train',
        model_params,
        dont_run=dont_run,
        skip_check=skip_check,
        load_params=load_params,
        loss_params=loss_params,
        save_params=save_params,
        train_params=train_params,
        optimizer_params=optimizer_params,
        validation_params=validation_params,
        learning_rate_params=learning_rate_params,
        log_device_placement=log_device_placement,
    )

    with tf.Graph().as_default(), tf.device(DEFAULT_HOST):
        # For convenience, use list of dicts instead of dict of lists
        _params = [{key: value[i]
                    for (key, value) in params.items()}
                   for i in range(len(params['model_params']))]
        _trargs = [{key: value[i]
                    for (key, value) in train_args.items()}
                   for i in range(len(params['model_params']))]

        # Use a single dataprovider for all models.
        data_params = _params[0]['train_params']['data_params']

        _params[0]['train_params']['data_params'], inputs = get_data(
            **data_params)

        # Build a graph for each distinct model.
        for param, trarg in zip(_params, _trargs):
            with tf.variable_scope(param['model_params']['prefix']):
                # Build global step
                trarg['global_step'] = tf.get_variable(
                    'global_step', [],
                    dtype=tf.int64,
                    trainable=False,
                    initializer=tf.constant_initializer(0))

                _, _, param, trarg = get_model(inputs,
                                               param['model_params'],
                                               param=param,
                                               trarg=trarg)

                tf.get_variable_scope().reuse_variables()

                trarg['validation_targets'] = \
                        get_valid_targets_dict(
                                **param)

        # Create session.
        gpu_options = tf.GPUOptions(allow_growth=True)
        sess = tf.Session(config=tf.ConfigProto(
            allow_soft_placement=True,
            gpu_options=gpu_options,
            log_device_placement=log_device_placement,
        ))

        init_op_global = tf.global_variables_initializer()
        sess.run(init_op_global)
        init_op_local = tf.local_variables_initializer()
        sess.run(init_op_local)
        log.info('Initialized from scratch first')

        for param, trarg in zip(_params, _trargs):

            prefix = param['model_params']['prefix'] + '/'
            all_vars = variables._all_saveable_objects()
            var_list = strip_prefix(prefix, all_vars)
            for var in var_list:
                print(var)

            trarg['dbinterface'] = DBInterface(
                sess=sess,
                params=param,
                var_list=var_list,
                global_step=trarg['global_step'],
                save_params=param['save_params'],
                load_params=param['load_params'])
            trarg['dbinterface'].initialize()

        # Convert back to a dictionary of lists
        params = {
            key: [param[key] for param in _params]
            for key in _params[0].keys()
        }
        train_args = {
            key: [trarg[key] for trarg in _trargs]
            for key in _trargs[0].keys()
        }

        if dont_run:
            return train_args

        return train(sess, **train_args)