Python chainer.reporter.DictSummary() Examples

The following are 19 code examples of chainer.reporter.DictSummary(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module chainer.reporter , or try the search function .
Example #1
Source File: parameter_statistics.py    From wavenet with Apache License 2.0 6 votes vote down vote up
def __init__(self, links, trigger=(1, 'epoch'), sparsity=True,
                 sparsity_include_bias=True, prefix=None):

        if not isinstance(links, (tuple, list)):
            links = links,

        self._links = links
        self._trigger = training.trigger.get_trigger(trigger)
        self._prefix = prefix
        self._summary = reporter.DictSummary()
        self._targets = [('W', 'data'), ('b', 'data'),
                         ('W', 'grad'), ('b', 'grad')]
        self._ratio_targets = [(('W'), ('data', 'grad')),
                               (('b'), ('data', 'grad'))]
        self._sparsity_targets = []

        if sparsity:
            if sparsity_include_bias:
                self._sparsity_targets.append((('W', 'b'), 'data'))
            else:
                self._sparsity_targets.append((('W'), 'data'))

        self._statistic_functions = ('min', 'max', 'mean', 'std')
        self._percentile_sigmas = (0.13, 2.28, 15.87, 50, 84.13, 97.72, 99.87) 
Example #2
Source File: parameter_statistics.py    From chainer with MIT License 6 votes vote down vote up
def __init__(self, links, statistics='default',
                 report_params=True, report_grads=True, prefix=None,
                 trigger=(1, 'epoch'), skip_nan_params=False):

        if not isinstance(links, (list, tuple)):
            links = links,
        self._links = links

        if statistics is None:
            statistics = {}
        elif statistics == 'default':
            statistics = self.default_statistics
        self._statistics = dict(statistics)

        attrs = []
        if report_params:
            attrs.append('data')
        if report_grads:
            attrs.append('grad')
        self._attrs = attrs

        self._prefix = prefix
        self._trigger = trigger_module.get_trigger(trigger)
        self._summary = reporter.DictSummary()
        self._skip_nan_params = skip_nan_params 
Example #3
Source File: extensions.py    From Semantic-Segmentation-using-Adversarial-Networks with MIT License 6 votes vote down vote up
def evaluate(self):
        iterator = self.get_iterator('main')
        all_targets = self.get_all_targets()
        for model in all_targets.values():
            if hasattr(model, 'train'):
                model.train = False

        if self.eval_hook:
            self.eval_hook(self)
        it = copy.copy(iterator)
        summary = reporter_module.DictSummary()

        for batch in it:
            observation = {}
            with reporter_module.report_scope(observation):
                self.updater.forward(batch)
                self.updater.calc_loss()
            summary.add(observation)

        for model in all_targets.values():
            if hasattr(model, 'train'):
                model.train = True
        return summary.compute_mean() 
Example #4
Source File: subfuncs.py    From convolutional_seq2seq with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _init_summary(self):
        self._summary = reporter.DictSummary() 
Example #5
Source File: MyEvaluator.py    From HFT-CNN with MIT License 5 votes vote down vote up
def evaluate(self):

        iterator = self._iterators['main']
        eval_func = self.eval_func or self._targets['main']

        if self.eval_hook:
            self.eval_hook(self)

        if hasattr(iterator, 'reset'):
            iterator.reset()
            it = iterator
        else:
            it = copy.copy(iterator)

        summary = reporter_module.DictSummary()

        for batch in it:
            observation = {}
            with reporter_module.report_scope(observation):
                row_idx, col_idx, val_idx = [], [], []
                x = cuda.to_gpu(np.array([i[0] for i in batch]))
                labels = [l[1] for l in batch]
                for i in range(len(labels)):
                    l_list = list(set(labels[i]))
                    for y in l_list:
                        row_idx.append(i)
                        col_idx.append(y)
                        val_idx.append(1)
                m = len(labels)
                n = self.class_dim
                t = sp.csr_matrix((val_idx, (row_idx, col_idx)), shape=(m, n), dtype=np.int8).todense()
                t = cuda.to_gpu(t)

                with function.no_backprop_mode():
                    loss = F.sigmoid_cross_entropy(eval_func(x), t)
                    summary.add({MyEvaluator.default_name + '/main/loss':loss})
            summary.add(observation)

        return summary.compute_mean() 
Example #6
Source File: evaluator.py    From 3dpose_gan with MIT License 5 votes vote down vote up
def evaluate(self):
        iterator = self._iterators['main']
        gen = self._targets['gen']

        if self.eval_hook:
            self.eval_hook(self)

        if hasattr(iterator, 'reset'):
            iterator.reset()
            it = iterator
        else:
            it = copy.copy(iterator)

        summary = reporter_module.DictSummary()

        for batch in it:
            observation = {}
            with reporter_module.report_scope(observation):
                xy_proj, xyz, scale = self.converter(batch, self.device)
                xy_proj, xyz = xy_proj[:, 0], xyz[:, 0]
                with function.no_backprop_mode(), \
                        chainer.using_config('train', False):
                    xy_real = chainer.Variable(xy_proj)
                    z_pred = gen(xy_real)
                    z_mse = F.mean_squared_error(z_pred, xyz[:, 2::3])
                    chainer.report({'z_mse': z_mse}, gen)

                    lx = gen.xp.power(xyz[:, 0::3] - xy_proj[:, 0::2], 2)
                    ly = gen.xp.power(xyz[:, 1::3] - xy_proj[:, 1::2], 2)
                    lz = gen.xp.power(xyz[:, 2::3] - z_pred.data, 2)

                    euclidean_distance = gen.xp.sqrt(lx + ly + lz).mean(axis=1)
                    euclidean_distance *= scale[:, 0]
                    euclidean_distance = gen.xp.mean(euclidean_distance)
                    chainer.report(
                        {'euclidean_distance': euclidean_distance}, gen)
            summary.add(observation)

        return summary.compute_mean() 
Example #7
Source File: chainer_utility.py    From Comicolorization with MIT License 5 votes vote down vote up
def evaluate(self):
        from chainer import reporter
        import copy

        iterator = self._iterators['main']
        target = self._targets['main']
        eval_func = self.eval_func or target

        if self.eval_hook:
            self.eval_hook(self)
        it = copy.copy(iterator)
        summary = reporter.DictSummary()

        for batch in it:
            observation = {}
            with reporter.report_scope(observation):
                in_arrays = self.converter(batch, self.device)
                if isinstance(in_arrays, tuple):
                    eval_func(*in_arrays)
                elif isinstance(in_arrays, dict):
                    eval_func(**in_arrays)
                else:
                    eval_func(in_arrays)

            summary.add(observation)

        return summary.compute_mean() 
Example #8
Source File: parameter_statistics.py    From wavenet with Apache License 2.0 5 votes vote down vote up
def __call__(self, trainer):

        """Execute the extension and collect statistics for the current state
        of parameters.

        Note that this method will merely update its statistic summary, unless
        the internal trigger is fired. If the trigger is fired, the summary
        will also be reported and then reset for the next accumulation.

        Args:
            trainer (~chainer.training.Trainer): Associated trainer that
                invoked this extension.
        """
        for link in self._links:
            for target in self._targets:
                stats = self.get_statistics(link, *target)
                stats = self.post_process(stats)
                self._summary.add(stats)

            for target in self._sparsity_targets:
                stats = self.get_sparsity(link, *target)
                stats = self.post_process(stats)
                self._summary.add(stats)

            for target in self._ratio_targets:
                stats = self.get_ratio(link, *target)
                stats = self.post_process(stats)
                self._summary.add(stats)

        if self._trigger(trainer):
            reporter.report(self._summary.compute_mean())
            self._summary = reporter.DictSummary()  # Clear summary 
Example #9
Source File: asr.py    From espnet with Apache License 2.0 5 votes vote down vote up
def evaluate(self):
        """Main evaluate routine for CustomEvaluator."""
        iterator = self._iterators["main"]

        if self.eval_hook:
            self.eval_hook(self)

        if hasattr(iterator, "reset"):
            iterator.reset()
            it = iterator
        else:
            it = copy.copy(iterator)

        summary = reporter_module.DictSummary()

        self.model.eval()
        with torch.no_grad():
            for batch in it:
                x = _recursive_to(batch, self.device)
                observation = {}
                with reporter_module.report_scope(observation):
                    # read scp files
                    # x: original json with loaded features
                    #    will be converted to chainer variable later
                    if self.ngpu == 0:
                        self.model(*x)
                    else:
                        # apex does not support torch.nn.DataParallel
                        data_parallel(self.model, x, range(self.ngpu))

                summary.add(observation)
        self.model.train()

        return summary.compute_mean() 
Example #10
Source File: copy_transformer_eval_function.py    From models with MIT License 5 votes vote down vote up
def evaluate(self):
        summary = reporter.DictSummary()
        eval_func = self.eval_func or self._targets['main']

        observation = {}
        with reporter.report_scope(observation):
            # we always use the same array for testing, since this is only an example ;)
            data = eval_func.net.xp.array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]], dtype='int32')
            eval_func(data=data, label=data)

        summary.add(observation)
        return summary.compute_mean() 
Example #11
Source File: train_utils.py    From see with GNU General Public License v3.0 5 votes vote down vote up
def evaluate(self):
        iterator = self._iterators['main']
        target = self._targets['main']
        eval_func = self.eval_func or target

        if self.eval_hook:
            self.eval_hook(self)
        it = copy.copy(iterator)
        summary = reporter_module.DictSummary()

        for _ in range(min(len(iterator.dataset) // iterator.batch_size, self.num_iterations)):
            batch = next(it, None)
            if batch is None:
                break

            observation = {}
            with reporter_module.report_scope(observation), chainer.using_config('train', False), chainer.using_config('enable_backprop', False):
                in_arrays = self.converter(batch, self.device)
                if isinstance(in_arrays, tuple):
                    eval_func(*in_arrays)
                elif isinstance(in_arrays, dict):
                    eval_func(**in_arrays)
                else:
                    eval_func(in_arrays)

            summary.add(observation)

        return summary.compute_mean() 
Example #12
Source File: early_stopping_trigger.py    From chainer with MIT License 5 votes vote down vote up
def _init_summary(self):
        self._summary = reporter.DictSummary() 
Example #13
Source File: plot_report.py    From chainer with MIT License 5 votes vote down vote up
def _init_summary(self):
        self._summary = reporter.DictSummary() 
Example #14
Source File: log_report.py    From chainer with MIT License 5 votes vote down vote up
def _init_summary(self):
        self._summary = reporter.DictSummary() 
Example #15
Source File: writetensorboard.py    From tensorboardX with MIT License 5 votes vote down vote up
def _init_summary(self):
        self._summary = reporter.DictSummary() 
Example #16
Source File: evaluator.py    From contextual_augmentation with MIT License 5 votes vote down vote up
def evaluate(self):
        iterator = self._iterators['main']
        eval_func = self.eval_func or self._targets['main']

        if self.eval_hook:
            self.eval_hook(self)

        if hasattr(iterator, 'reset'):
            iterator.reset()
            it = iterator
        else:
            it = copy.copy(iterator)

        # summary = reporter_module.DictSummary()
        summary = collections.defaultdict(list)

        for batch in it:
            observation = {}
            with reporter_module.report_scope(observation):
                in_arrays = self.converter(batch, self.device)
                with function.no_backprop_mode():
                    if isinstance(in_arrays, tuple):
                        eval_func(*in_arrays)
                    elif isinstance(in_arrays, dict):
                        eval_func(**in_arrays)
                    else:
                        eval_func(in_arrays)
            n_data = len(batch)
            summary['n'].append(n_data)
            # summary.add(observation)
            for k, v in observation.items():
                summary[k].append(v)

        mean = dict()
        ns = summary['n']
        del summary['n']
        for k, vs in summary.items():
            mean[k] = sum(v * n for v, n in zip(vs, ns)) / sum(ns)
        return mean
        # return summary.compute_mean() 
Example #17
Source File: triggers.py    From contextual_augmentation with MIT License 5 votes vote down vote up
def _init_summary(self):
        self._summary = reporter.DictSummary() 
Example #18
Source File: parameter_statistics.py    From chainer with MIT License 4 votes vote down vote up
def __call__(self, trainer):
        """Execute the statistics extension.

        Collect statistics for the current state of parameters.

        Note that this method will merely update its statistic summary, unless
        the internal trigger is fired. If the trigger is fired, the summary
        will also be reported and then reset for the next accumulation.

        Args:
            trainer (~chainer.training.Trainer): Associated trainer that
                invoked this extension.
        """
        statistics = {}

        for link in self._links:
            link_name = getattr(link, 'name', 'None')
            for param_name, param in link.namedparams():
                for attr_name in self._attrs:
                    for function_name, function in \
                            six.iteritems(self._statistics):
                        # Get parameters as a flattened one-dimensional array
                        # since the statistics function should make no
                        # assumption about the axes
                        params = getattr(param, attr_name).ravel()
                        if (self._skip_nan_params
                            and (
                                backend.get_array_module(params).isnan(params)
                                .any())):
                            value = numpy.nan
                        else:
                            value = function(params)
                        key = self.report_key_template.format(
                            prefix=self._prefix + '/' if self._prefix else '',
                            link_name=link_name,
                            param_name=param_name,
                            attr_name=attr_name,
                            function_name=function_name
                        )
                        if (isinstance(value, chainer.get_array_types())
                                and value.size > 1):
                            # Append integer indices to the keys if the
                            # statistic function return multiple values
                            statistics.update({'{}/{}'.format(key, i): v for
                                               i, v in enumerate(value)})
                        else:
                            statistics[key] = value

        self._summary.add(statistics)

        if self._trigger(trainer):
            reporter.report(self._summary.compute_mean())
            self._summary = reporter.DictSummary()  # Clear summary 
Example #19
Source File: custom_mean_evaluator.py    From kiss with GNU General Public License v3.0 4 votes vote down vote up
def evaluate(self):
        """Evaluates the model and returns a result dictionary.

        This method runs the evaluation loop over the validation dataset. It
        accumulates the reported values to :class:`~chainer.DictSummary` and
        returns a dictionary whose values are means computed by the summary.

        Note that this function assumes that the main iterator raises
        ``StopIteration`` or code in the evaluation loop raises an exception.
        So, if this assumption is not held, the function could be caught in
        an infinite loop.

        Users can override this method to customize the evaluation routine.

        .. note::

            This method encloses :attr:`eval_func` calls with
            :func:`function.no_backprop_mode` context, so all calculations
            using :class:`~chainer.FunctionNode`\\s inside
            :attr:`eval_func` do not make computational graphs. It is for
            reducing the memory consumption.

        Returns:
            dict: Result dictionary. This dictionary is further reported via
            :func:`~chainer.report` without specifying any observer.

        """
        iterator = self._iterators['main']
        eval_func = self.eval_func or self._targets['main']

        if self.eval_hook:
            self.eval_hook(self)

        if hasattr(iterator, 'reset'):
            iterator.reset()
            it = iterator
        else:
            it = copy.copy(iterator)

        if self.max_num_iterations is not None:
            it = self.fixed_num_iterations_iterator(it)

        summary = reporter_module.DictSummary()

        for batch in it:
            observation = {}
            with reporter_module.report_scope(observation):
                in_arrays = self.converter(batch, self.device)
                with function.no_backprop_mode():
                    if isinstance(in_arrays, tuple):
                        eval_func(*in_arrays)
                    elif isinstance(in_arrays, dict):
                        eval_func(**in_arrays)
                    else:
                        eval_func(in_arrays)

            summary.add(observation)

        return self.calculate_mean_of_summary(summary)