r'''Preprocess global step and evaluation's hooks.
(self)
| 851 | self._history = history |
| 852 | |
| 853 | def begin(self): |
| 854 | r'''Preprocess global step and evaluation's hooks. |
| 855 | ''' |
| 856 | self._evaluation_specs = ops.get_collection_ref( |
| 857 | EvaluationSpec.__name__) |
| 858 | if len(self._evaluation_specs) > 0: |
| 859 | raise ValueError('Only one evaluation spec allowed in a graph') |
| 860 | |
| 861 | self._timer = None |
| 862 | self._iter_count = 0 |
| 863 | self._hooks = [] |
| 864 | |
| 865 | self._timer = basic_session_run_hooks.SecondOrStepTimer( |
| 866 | every_steps=self._every_n_iter) |
| 867 | self._timer.reset() |
| 868 | |
| 869 | with reuse_variables(vs.AUTO_REUSE): |
| 870 | with scope(): |
| 871 | with ops.name_scope(ModeKeys.EVAL): |
| 872 | eval_spec = self._fn() |
| 873 | if isinstance(eval_spec, dict): |
| 874 | eval_dict = {} |
| 875 | update_ops = [] |
| 876 | for metric_name, metric_val_and_update in eval_spec.items(): |
| 877 | if not isinstance(metric_name, six.string_types): |
| 878 | raise ValueError( |
| 879 | f'Metric name {metric_name} should be a str') |
| 880 | if (not isinstance(metric_val_and_update, (tuple, list)) |
| 881 | or len(metric_val_and_update) != 2): |
| 882 | raise ValueError( |
| 883 | f'{metric_val_and_update} should be a tuple ' |
| 884 | 'of (metric, update_op)') |
| 885 | eval_dict[metric_name] = metric_val_and_update[0] |
| 886 | update_ops.append(metric_val_and_update[1]) |
| 887 | update_op = control_flow_ops.group(update_ops) |
| 888 | eval_spec = EvaluationSpec( |
| 889 | name=EvaluationSpec.__name__, |
| 890 | hooks=None, |
| 891 | update_op=update_op, |
| 892 | eval_dict=eval_dict) |
| 893 | if not isinstance(eval_spec, EvaluationSpec): |
| 894 | raise ValueError( |
| 895 | 'eval_fn should return a dict or a EvaluationSpec') |
| 896 | self._evaluation_specs.append(eval_spec) |
| 897 | if eval_spec.hooks: |
| 898 | self._hooks.extend(eval_spec.hooks) |
| 899 | eval_dict = dict(eval_spec.eval_dict) |
| 900 | if ops.GraphKeys.GLOBAL_STEP not in eval_dict: |
| 901 | global_step_tensor = training_util.get_global_step( |
| 902 | ops.get_default_graph()) |
| 903 | eval_dict[ops.GraphKeys.GLOBAL_STEP] = global_step_tensor |
| 904 | for h in self._hooks: |
| 905 | h.begin() |
| 906 | self._update_op = eval_spec.update_op |
| 907 | self._metrics = eval_dict |
| 908 | |
| 909 | def after_create_session(self, session, coord): # pylint: disable=unused-argument |
| 910 | r'''Call evaluation's hooks. |
nothing calls this directly
no test coverage detected