当前位置: 首页>>代码示例>>Python>>正文


Python function.no_backprop_mode方法代码示例

本文整理汇总了Python中chainer.function.no_backprop_mode方法的典型用法代码示例。如果您正苦于以下问题:Python function.no_backprop_mode方法的具体用法?Python function.no_backprop_mode怎么用?Python function.no_backprop_mode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在chainer.function的用法示例。


在下文中一共展示了function.no_backprop_mode方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _evaluate_local_single

# 需要导入模块: from chainer import function [as 别名]
# 或者: from chainer.function import no_backprop_mode [as 别名]
def _evaluate_local_single(self, iterator):
        for batch in iterator:
            in_arrays = convert._call_converter(
                self.converter, batch, self.device)

            with function.no_backprop_mode():
                if isinstance(in_arrays, tuple):
                    results = self.calc_local(*in_arrays)
                elif isinstance(in_arrays, dict):
                    results = self.calc_local(**in_arrays)
                else:
                    results = self.calc_local(in_arrays)

            if self._progress_hook:
                self._progress_hook(batch)
            yield results 
开发者ID:chainer,项目名称:chainer,代码行数:18,代码来源:multi_node_evaluator.py

示例2: predict

# 需要导入模块: from chainer import function [as 别名]
# 或者: from chainer.function import no_backprop_mode [as 别名]
def predict(self, images, oversample=True):
        """Computes all the probabilities of given images.

        Args:
            images (iterable of PIL.Image or numpy.ndarray): Input images.
            oversample (bool): If ``True``, it averages results across
                center, corners, and mirrors. Otherwise, it uses only the
                center.

        Returns:
            ~chainer.Variable: Output that contains the class probabilities
            of given images.

        """

        x = concat_examples([prepare(img, size=(256, 256)) for img in images])
        if oversample:
            x = imgproc.oversample(x, crop_dims=(224, 224))
        else:
            x = x[:, :, 16:240, 16:240]
        # Use no_backprop_mode to reduce memory consumption
        with function.no_backprop_mode():
            x = Variable(self.xp.asarray(x))
            y = self(x, layers=['prob'])['prob']
            if oversample:
                n = y.data.shape[0] // 10
                y_shape = y.data.shape[1:]
                y = reshape(y, (n, 10) + y_shape)
                y = sum(y, axis=1) / 10
        return y 
开发者ID:pfnet-research,项目名称:nips17-adversarial-attack,代码行数:32,代码来源:resnet_layer.py

示例3: evaluate

# 需要导入模块: from chainer import function [as 别名]
# 或者: from chainer.function import no_backprop_mode [as 别名]
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() 
开发者ID:pfnet-research,项目名称:contextual_augmentation,代码行数:42,代码来源:evaluator.py

示例4: __call__

# 需要导入模块: from chainer import function [as 别名]
# 或者: from chainer.function import no_backprop_mode [as 别名]
def __call__(self, imgs):
        imgs = self.model.xp.asarray([self.do_transform(img) for img in imgs])

        with using_config("train", False), no_backprop_mode():
            imgs = Variable(imgs)
            predictions = self.model(imgs)

        output = to_cpu(predictions.array if hasattr(predictions, "array") else cupy.asnumpy(predictions))
        return output 
开发者ID:osmr,项目名称:imgclsmob,代码行数:11,代码来源:utils.py

示例5: _evaluate_local

# 需要导入模块: from chainer import function [as 别名]
# 或者: from chainer.function import no_backprop_mode [as 别名]
def _evaluate_local(self, iterator):
        # Check whether local eval is all done every 8 rounds
        gather_interval = 8

        all_done = None
        while not all_done:
            all_done = None
            results = None
            for _ in range(gather_interval):
                try:
                    batch = iterator.next()
                    in_arrays = convert._call_converter(
                        self.converter, batch, self.device)

                    with function.no_backprop_mode():
                        if isinstance(in_arrays, tuple):
                            results = self.calc_local(*in_arrays)
                        elif isinstance(in_arrays, dict):
                            results = self.calc_local(**in_arrays)
                        else:
                            results = self.calc_local(in_arrays)

                    if self.comm.rank == self.root and self._progress_hook:
                        self._progress_hook(batch)

                except StopIteration:
                    batch = None
                    results = None

                results = self.comm.gather_obj(results, root=self.root)

                if self.comm.rank == self.root:
                    valid_results = [r for r in results if r is not None]
                    for result in valid_results:
                        yield result

                    all_done = len(valid_results) == 0

            all_done = self.comm.bcast_obj(all_done, root=self.root)
        return 
开发者ID:chainer,项目名称:chainer,代码行数:42,代码来源:multi_node_evaluator.py

示例6: predict

# 需要导入模块: from chainer import function [as 别名]
# 或者: from chainer.function import no_backprop_mode [as 别名]
def predict(self, images, oversample=True):
        """Computes all the probabilities of given images.

        Args:
            images (iterable of PIL.Image or numpy.ndarray): Input images.
                When you specify a color image as a :class:`numpy.ndarray`,
                make sure that color order is RGB.
            oversample (bool): If ``True``, it averages results across
                center, corners, and mirrors. Otherwise, it uses only the
                center.

        Returns:
            ~chainer.Variable: Output that contains the class probabilities
            of given images.

        """

        x = concat_examples([prepare(img, size=(256, 256)) for img in images])
        if oversample:
            x = imgproc.oversample(x, crop_dims=(224, 224))
        else:
            x = x[:, :, 16:240, 16:240]
        # Use no_backprop_mode to reduce memory consumption
        with function.no_backprop_mode(), chainer.using_config('train', False):
            x = Variable(self.xp.asarray(x))
            y = self(x, layers=['prob'])['prob']
            if oversample:
                n = len(y) // 10
                y_shape = y.shape[1:]
                y = reshape(y, (n, 10) + y_shape)
                y = sum(y, axis=1) / 10
        return y 
开发者ID:chainer,项目名称:chainer,代码行数:34,代码来源:vgg.py

示例7: predict

# 需要导入模块: from chainer import function [as 别名]
# 或者: from chainer.function import no_backprop_mode [as 别名]
def predict(self, images, oversample=True):
        """Computes all the probabilities of given images.

        Args:
            images (iterable of PIL.Image or numpy.ndarray): Input images.
                When you specify a color image as a :class:`numpy.ndarray`,
                make sure that color order is RGB.
            oversample (bool): If ``True``, it averages results across
                center, corners, and mirrors. Otherwise, it uses only the
                center.

        Returns:
            ~chainer.Variable: Output that contains the class probabilities
            of given images.

        """

        x = concat_examples([prepare(img, size=(256, 256)) for img in images])
        if oversample:
            x = imgproc.oversample(x, crop_dims=(224, 224))
        else:
            x = x[:, :, 16:240, 16:240]
        # Use no_backprop_mode to reduce memory consumption
        with function.no_backprop_mode(), chainer.using_config('train', False):
            x = Variable(self.xp.asarray(x))
            y = self(x, layers=['prob'])['prob']
            if oversample:
                n = len(y) // 10
                y_shape = y.shape[1:]
                y = reshape(y, (n, 10) + y_shape)
                y = average(y, axis=1)
        return y 
开发者ID:chainer,项目名称:chainer,代码行数:34,代码来源:googlenet.py

示例8: evaluate

# 需要导入模块: from chainer import function [as 别名]
# 或者: from chainer.function import no_backprop_mode [as 别名]
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() 
开发者ID:DwangoMediaVillage,项目名称:3dpose_gan,代码行数:41,代码来源:evaluator.py

示例9: evaluate

# 需要导入模块: from chainer import function [as 别名]
# 或者: from chainer.function import no_backprop_mode [as 别名]
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() 
开发者ID:ShimShim46,项目名称:HFT-CNN,代码行数:41,代码来源:MyEvaluator.py

示例10: evaluate

# 需要导入模块: from chainer import function [as 别名]
# 或者: from chainer.function import no_backprop_mode [as 别名]
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) 
开发者ID:Bartzi,项目名称:kiss,代码行数:61,代码来源:custom_mean_evaluator.py


注:本文中的chainer.function.no_backprop_mode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。