本文整理汇总了Python中chainer.reporter.DictSummary方法的典型用法代码示例。如果您正苦于以下问题:Python reporter.DictSummary方法的具体用法?Python reporter.DictSummary怎么用?Python reporter.DictSummary使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chainer.reporter
的用法示例。
在下文中一共展示了reporter.DictSummary方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: evaluate
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import DictSummary [as 别名]
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()
示例2: __init__
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import DictSummary [as 别名]
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
示例3: __init__
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import DictSummary [as 别名]
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)
示例4: _init_summary
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import DictSummary [as 别名]
def _init_summary(self):
self._summary = reporter.DictSummary()
示例5: evaluate
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import DictSummary [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()
示例6: evaluate
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import DictSummary [as 别名]
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()
示例7: evaluate
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import DictSummary [as 别名]
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()
示例8: evaluate
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import DictSummary [as 别名]
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()
示例9: __call__
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import DictSummary [as 别名]
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
示例10: evaluate
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import DictSummary [as 别名]
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()
示例11: evaluate
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import DictSummary [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()
示例12: evaluate
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import DictSummary [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()
示例13: __call__
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import DictSummary [as 别名]
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
示例14: evaluate
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import DictSummary [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)