本文整理汇总了Python中chainer.report方法的典型用法代码示例。如果您正苦于以下问题:Python chainer.report方法的具体用法?Python chainer.report怎么用?Python chainer.report使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chainer
的用法示例。
在下文中一共展示了chainer.report方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_loss_gen
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import report [as 别名]
def _get_loss_gen(self):
batchsize = self.y_fake.data.shape[0]
L_mce = F.softmax_cross_entropy(self.pred_label_map, self.ground_truth, normalize=False)
L_bce = F.softmax_cross_entropy(self.y_fake, Variable(self.xp.ones(batchsize, dtype=self.xp.int32), volatile=not self.gen.train))
loss = L_mce + self.L_bce_weight * L_bce
# log report
label_true = chainer.cuda.to_cpu(self.ground_truth.data)
label_pred = chainer.cuda.to_cpu(self.pred_label_map.data).argmax(axis=1)
logs = []
for i in six.moves.range(batchsize):
acc, acc_cls, iu, fwavacc = utils.label_accuracy_score(
label_true[i], label_pred[i], self.n_class)
logs.append((acc, acc_cls, iu, fwavacc))
log = np.array(logs).mean(axis=0)
values = {
'loss': loss,
'accuracy': log[0],
'accuracy_cls': log[1],
'iu': log[2],
'fwavacc': log[3],
}
chainer.report(values, self.gen)
return loss
示例2: calc_loss
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import report [as 别名]
def calc_loss(self):
batchsize = self.ground_truth.shape[0]
self.loss = F.softmax_cross_entropy(self.pred_label_map, self.ground_truth, normalize=False)
# log report
label_true = chainer.cuda.to_cpu(self.ground_truth.data)
label_pred = chainer.cuda.to_cpu(self.pred_label_map.data).argmax(axis=1)
logs = []
for i in six.moves.range(batchsize):
acc, acc_cls, iu, fwavacc = utils.label_accuracy_score(
label_true[i], label_pred[i], self.n_class)
logs.append((acc, acc_cls, iu, fwavacc))
log = np.array(logs).mean(axis=0)
values = {
'loss': self.loss,
'accuracy': log[0],
'accuracy_cls': log[1],
'iu': log[2],
'fwavacc': log[3],
}
chainer.report(values, self.model)
示例3: forward
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import report [as 别名]
def forward(self, x, t):
# def forward(self, x):
h = F.max_pooling_2d(F.local_response_normalization(
F.relu(self.conv1(x))), 3, stride=2)
h = F.max_pooling_2d(F.local_response_normalization(
F.relu(self.conv2(h))), 3, stride=2)
h = F.relu(self.conv3(h))
h = F.relu(self.conv4(h))
h = F.max_pooling_2d(F.relu(self.conv5(h)), 3, stride=2)
h = F.dropout(F.relu(self.fc6(h)))
h = F.dropout(F.relu(self.fc7(h)))
h = self.fc8(h)
loss = F.softmax_cross_entropy(h, t)
#loss = h
# chainer.report({'loss': loss, 'accuracy': F.accuracy(h, t)}, self)
return loss
# from https://github.com/chainer/chainer/blob/master/examples/imagenet/alex.py
示例4: forward
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import report [as 别名]
def forward(self, x, t):
h = self.bn1(self.conv1(x))
h = F.max_pooling_2d(F.relu(h), 3, stride=2)
h = self.res2(h)
h = self.res3(h)
h = self.res4(h)
h = self.res5(h)
h = F.average_pooling_2d(h, 7, stride=1)
h = self.fc(h)
#loss = F.softmax_cross_entropy(h, t)
loss = self.softmax_cross_entropy(h, t)
if self.compute_accuracy:
chainer.report({'loss': loss, 'accuracy': F.accuracy(h, np.argmax(t, axis=1))}, self)
else:
chainer.report({'loss': loss}, self)
return loss
示例5: forward
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import report [as 别名]
def forward(self, x, t):
h = F.max_pooling_2d(F.local_response_normalization(
F.relu(self.conv1(x))), 3, stride=2)
h = F.max_pooling_2d(F.local_response_normalization(
F.relu(self.conv2(h))), 3, stride=2)
h = F.relu(self.conv3(h))
h = F.relu(self.conv4(h))
h = F.max_pooling_2d(F.relu(self.conv5(h)), 3, stride=2)
h = F.dropout(F.relu(self.fc6(h)))
h = F.dropout(F.relu(self.fc7(h)))
h = self.fc8(h)
# EDIT(hamaji): ONNX-chainer cannot output SoftmaxCrossEntropy.
# loss = F.softmax_cross_entropy(h, t)
loss = self.softmax_cross_entropy(h, t)
if self.compute_accuracy:
chainer.report({'loss': loss, 'accuracy': F.accuracy(h, t)}, self)
else:
chainer.report({'loss': loss}, self)
return loss
示例6: __call__
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import report [as 别名]
def __call__(self, trainer):
device = self.device
with chainer.no_backprop_mode():
references = []
hypotheses = []
for i in range(0, len(self.test_data), self.batch):
sources, targets = zip(*self.test_data[i:i + self.batch])
references.extend([[t.tolist()] for t in targets])
sources = [device.send(x) for x in sources]
ys = [y.tolist()
for y in self.model.translate(sources, self.max_length)]
hypotheses.extend(ys)
bleu = bleu_score.corpus_bleu(
references, hypotheses,
smoothing_function=bleu_score.SmoothingFunction().method1)
chainer.report({self.key: bleu})
示例7: test_add_observers
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import report [as 别名]
def test_add_observers(self):
reporter = chainer.Reporter()
observer1 = object()
reporter.add_observer('o1', observer1)
observer2 = object()
reporter.add_observer('o2', observer2)
reporter.report({'x': 1}, observer1)
reporter.report({'y': 2}, observer2)
observation = reporter.observation
self.assertIn('o1/x', observation)
self.assertEqual(observation['o1/x'], 1)
self.assertIn('o2/y', observation)
self.assertEqual(observation['o2/y'], 2)
self.assertNotIn('x', observation)
self.assertNotIn('y', observation)
self.assertNotIn('o1/y', observation)
self.assertNotIn('o2/x', observation)
示例8: scope
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import report [as 别名]
def scope(self, observation):
"""Creates a scope to report observed values to ``observation``.
This is a context manager to be passed to ``with`` statements. In this
scope, the observation dictionary is changed to the given one.
It also makes this reporter object current.
Args:
observation (dict): Observation dictionary. All observations
reported inside of the ``with`` statement are written to this
dictionary.
"""
old = self.observation
self.observation = observation
self.__enter__()
try:
yield
finally:
self.__exit__(None, None, None)
self.observation = old
示例9: forward
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import report [as 别名]
def forward(self, trainer):
with chainer.no_backprop_mode():
references = []
hypotheses = []
for i in range(0, len(self.test_data), self.batch):
sources, targets = zip(*self.test_data[i:i + self.batch])
references.extend([[t.tolist()] for t in targets])
sources = [
chainer.dataset.to_device(self.device, x) for x in sources]
ys = [y.tolist()
for y in self.model.translate(sources, self.max_length)]
hypotheses.extend(ys)
bleu = bleu_score.corpus_bleu(
references, hypotheses,
smoothing_function=bleu_score.SmoothingFunction().method1)
chainer.report({self.key: bleu})
示例10: __call__
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import report [as 别名]
def __call__(self, x, t=None, w=None):
# t, w is on host.
# Forward network
alpha = self.forward(x)
if t is None:
assert not chainer.config.train
return
# Weighted mean squared error
# TODO: Do more tests
# loss = F.mean(F.squared_error(alpha, t) * w)
loss = F.mean_squared_error(alpha, t)
if np.isnan(float(loss.data)):
raise ValueError('Loss is nan.')
chainer.report({'loss': loss}, self)
return loss
示例11: __call__
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import report [as 别名]
def __call__(self, x, t=None):
score = self.forward(x)
if t is None:
assert not chainer.config.train
return
loss = F.softmax_cross_entropy(score, t, normalize=True)
if np.isnan(float(loss.data)):
raise ValueError('Loss is nan.')
chainer.report({'loss': loss}, self)
accuracy = F.accuracy(score, t)
chainer.report({'accuracy': accuracy}, self)
return loss
示例12: _loss_discriminator
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import report [as 别名]
def _loss_discriminator(self, discriminator, d_real, d_fake):
b, _, t = d_real.data.shape
loss_real = F.sum(F.softplus(-d_real)) / (b * t)
chainer.report({'real': loss_real}, discriminator)
loss_fake = F.sum(F.softplus(d_fake)) / (b * t)
chainer.report({'fake': loss_fake}, discriminator)
loss = loss_real + loss_fake
chainer.report({'loss': loss}, discriminator)
tp = (d_real.data > 0.5).sum()
fp = (d_fake.data > 0.5).sum()
fn = (d_real.data <= 0.5).sum()
tn = (d_fake.data <= 0.5).sum()
accuracy = (tp + tn) / (tp + fp + fn + tn)
precision = tp / (tp + fp)
recall = tp / (tp + fn)
chainer.report({'accuracy': accuracy}, self.discriminator)
chainer.report({'precision': precision}, self.discriminator)
chainer.report({'recall': recall}, self.discriminator)
return loss
示例13: _loss_discriminator
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import report [as 别名]
def _loss_discriminator(self, discriminator, d_real, d_fake):
b, _, w, h = d_real.data.shape
loss_real = F.sum(F.softplus(-d_real)) / (b * w * h)
chainer.report({'real': loss_real}, discriminator)
loss_fake = F.sum(F.softplus(d_fake)) / (b * w * h)
chainer.report({'fake': loss_fake}, discriminator)
loss = loss_real + loss_fake
chainer.report({'loss': loss}, discriminator)
tp = (d_real.data > 0.5).sum()
fp = (d_fake.data > 0.5).sum()
fn = (d_real.data <= 0.5).sum()
tn = (d_fake.data <= 0.5).sum()
accuracy = (tp + tn) / (tp + fp + fn + tn)
precision = tp / (tp + fp)
recall = tp / (tp + fn)
chainer.report({'accuracy': accuracy}, self.discriminator)
chainer.report({'precision': precision}, self.discriminator)
chainer.report({'recall': recall}, self.discriminator)
return loss
示例14: fid_extension
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import report [as 别名]
def fid_extension(fidapi,
generate_func,
seed=None,
report_key='FID',
verbose=True):
@chainer.training.make_extension()
def calc(trainer):
if verbose:
print('Running FID...')
fidapi.calc_fake(generate_func, seed)
fid = fidapi.calc_FID()
chainer.report({report_key: fid})
if verbose:
print(report_key + ' Value: ', fid)
return calc
示例15: _get_loss_dis
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import report [as 别名]
def _get_loss_dis(self):
batchsize = self.y_fake.data.shape[0]
loss = F.softmax_cross_entropy(self.y_real, Variable(self.xp.ones(batchsize, dtype=self.xp.int32), volatile=not self.gen.train))
loss += F.softmax_cross_entropy(self.y_fake, Variable(self.xp.zeros(batchsize, dtype=self.xp.int32), volatile=not self.gen.train))
chainer.report({'loss': loss}, self.dis)
return loss