本文整理汇总了Python中fairseq.utils.item方法的典型用法代码示例。如果您正苦于以下问题:Python utils.item方法的具体用法?Python utils.item怎么用?Python utils.item使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类fairseq.utils
的用法示例。
在下文中一共展示了utils.item方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: forward
# 需要导入模块: from fairseq import utils [as 别名]
# 或者: from fairseq.utils import item [as 别名]
def forward(self, model, sample, reduce=True):
"""Compute the loss for the given sample.
Returns a tuple with three elements:
1) the loss
2) the sample size, which is used as the denominator for the gradient
3) logging outputs to display while training
"""
net_output = model(**sample['net_input'])
lprobs = model.get_normalized_probs(net_output, log_probs=True)
lprobs = lprobs.view(-1, lprobs.size(-1))
target = model.get_targets(sample, net_output).view(-1)
loss = F.nll_loss(lprobs, target, size_average=False, ignore_index=self.padding_idx,
reduce=reduce)
sample_size = sample['target'].size(0) if self.args.sentence_avg else sample['ntokens']
logging_output = {
'loss': utils.item(loss.data) if reduce else loss.data,
'ntokens': sample['ntokens'],
'sample_size': sample_size,
}
return loss, sample_size, logging_output
示例2: get_logging_output
# 需要导入模块: from fairseq import utils [as 别名]
# 或者: from fairseq.utils import item [as 别名]
def get_logging_output(self, sample, target, lprobs, loss):
target = target.view(-1)
mask = target != self.padding_idx
correct = torch.sum(
lprobs.argmax(1).masked_select(mask) == target.masked_select(mask)
)
total = torch.sum(mask)
sample_size = (
sample["target"].size(0) if self.sentence_avg else sample["ntokens"]
)
logging_output = {
"loss": utils.item(loss.data), # * sample['ntokens'],
"ntokens": sample["ntokens"],
"nsentences": sample["target"].size(0),
"sample_size": sample_size,
"correct": utils.item(correct.data),
"total": utils.item(total.data),
"nframes": torch.sum(sample["net_input"]["src_lengths"]).item(),
}
return sample_size, logging_output
示例3: reduce_metrics
# 需要导入模块: from fairseq import utils [as 别名]
# 或者: from fairseq.utils import item [as 别名]
def reduce_metrics(logging_outputs) -> None:
"""Aggregate logging outputs from data parallel training."""
sample_size = utils.item(sum(log.get("sample_size", 0) for log in logging_outputs))
loss = utils.item(sum(log.get("loss", 0) for log in logging_outputs))
nll_loss = utils.item(sum(log.get("nll_loss", 0) for log in logging_outputs))
metrics.log_scalar('loss', loss / sample_size / math.log(2), sample_size, round=3)
metrics.log_scalar('nll_loss', nll_loss / sample_size / math.log(2), sample_size, round=3)
metrics.log_derived('ppl', lambda meters: utils.get_perplexity(meters['loss'].avg))
for key in logging_outputs[0]:
if key[-5:] == "-loss":
val = sum(log.get(key, 0) for log in logging_outputs)
metrics.log_scalar(
key[:-5],
val / sample_size / math.log(2) if sample_size > 0 else 0.0,
sample_size,
round=3,
)
示例4: upgrade_state_dict_named
# 需要导入模块: from fairseq import utils [as 别名]
# 或者: from fairseq.utils import item [as 别名]
def upgrade_state_dict_named(self, state_dict, name):
"""Upgrade a (possibly old) state dict for new versions of fairseq."""
if isinstance(self.embed_positions, SinusoidalPositionalEmbedding):
weights_key = "{}.embed_positions.weights".format(name)
if weights_key in state_dict:
print("deleting {0}".format(weights_key))
del state_dict[weights_key]
state_dict[
"{}.embed_positions._float_tensor".format(name)
] = torch.FloatTensor(1)
for i in range(self.num_layers):
# update layer norms
self.layers[i].upgrade_state_dict_named(
state_dict, "{}.layers.{}".format(name, i)
)
version_key = "{}.version".format(name)
if utils.item(state_dict.get(version_key, torch.Tensor([1]))[0]) < 2:
# earlier checkpoints did not normalize after the stack of layers
self.layer_norm = None
self.normalize = False
state_dict[version_key] = torch.Tensor([1])
return state_dict
示例5: forward
# 需要导入模块: from fairseq import utils [as 别名]
# 或者: from fairseq.utils import item [as 别名]
def forward(self, model, sample, reduce=True):
"""Compute the loss for the given sample.
Returns a tuple with three elements:
1) the loss
2) the sample size, which is used as the denominator for the gradient
3) logging outputs to display while training
"""
net_output = model(**sample['net_input'])
target = sample['target']
loss = vocab_parallel_cross_entropy(net_output[0].float(), target)
loss = (loss * (target != self.padding_idx)).sum()
sample_size = sample['target'].size(0) if self.sentence_avg else sample['ntokens']
logging_output = {
'loss': utils.item(loss.data) if reduce else loss.data,
'ntokens': sample['ntokens'],
'nsentences': sample['target'].size(0),
'sample_size': sample_size,
}
return loss, sample_size, logging_output
示例6: forward
# 需要导入模块: from fairseq import utils [as 别名]
# 或者: from fairseq.utils import item [as 别名]
def forward(self, model, sample, reduce=True):
"""Compute the loss for the given sample.
Returns a tuple with three elements:
1) the loss, as a Variable
2) the sample size, which is used as the denominator for the gradient
3) logging outputs to display while training
"""
translations, bleu_scores = self.generate_translations(model, sample)
nll_loss = self.compute_nll(model, sample, translations)
loss = nll_loss[:, 0] + torch.logsumexp(-nll_loss, 1)
if reduce:
loss = loss.sum()
sample_size = (
sample["target"].size(0) if self.args.sentence_avg else sample["ntokens"]
)
logging_output = {
"loss": utils.item(loss.data) if reduce else loss.data,
"ntokens": sample["ntokens"],
"nsentences": sample["target"].size(0),
"sample_size": sample_size,
}
return loss, sample_size, logging_output
示例7: update_output
# 需要导入模块: from fairseq import utils [as 别名]
# 或者: from fairseq.utils import item [as 别名]
def update_output(
args,
extra_state: Dict[str, Any],
output_queue: Optional[mp_queues.Queue],
num_updates: int,
train_ppl: float,
wps: Optional[float],
):
if distributed_utils.is_master(args) and output_queue is not None:
progress_output: Tuple[int, Dict] = (
num_updates,
{
"train_ppl": train_ppl,
"tune_loss": utils.item(extra_state["tune_eval"]["loss"]),
"tune_ppl": extra_state["tune_eval"]["perplexity"],
"wps": utils.item(wps),
# translation_samples isn't currently used by the queue reader,
# so just pass None for now until we start needing it.
"translation_samples": None,
},
)
output_queue.put_nowait(progress_output)
extra_state["training_progress"].append(progress_output)
return extra_state
示例8: forward
# 需要导入模块: from fairseq import utils [as 别名]
# 或者: from fairseq.utils import item [as 别名]
def forward(self, model, sample, reduce=True):
"""Compute the loss for the given sample.
Returns a tuple with three elements:
1) the loss, as a Variable
2) the sample size, which is used as the denominator for the gradient
3) logging outputs to display while training
"""
net_output = model(**sample['net_input'])
lprobs = model.get_normalized_probs(net_output, log_probs=True)
lprobs = lprobs.view(-1, lprobs.size(-1))
target = sample['target'].view(-1)
loss = F.nll_loss(lprobs, target, size_average=False, ignore_index=self.padding_idx,
reduce=reduce)
sample_size = sample['target'].size(0) if self.args.sentence_avg else sample['ntokens']
logging_output = {
'loss': utils.item(loss.data) if reduce else loss.data,
'ntokens': sample['ntokens'],
'sample_size': sample_size,
}
return loss, sample_size, logging_output
示例9: forward
# 需要导入模块: from fairseq import utils [as 别名]
# 或者: from fairseq.utils import item [as 别名]
def forward(self, model, sample, reduce=True):
"""Compute the loss for the given sample.
Returns a tuple with three elements:
1) the loss
2) the sample size, which is used as the denominator for the gradient
3) logging outputs to display while training
"""
net_output = model(**sample['net_input'])
loss, nll_loss, extra_loss = self.compute_loss(model, net_output, sample, reduce=reduce)
sample_size = sample['target'].size(0) if self.args.sentence_avg else sample['ntokens']
logging_output = {
'loss': utils.item(loss.data) if reduce else loss.data,
'nll_loss': utils.item(nll_loss.data) if reduce else nll_loss.data,
'qv_loss': utils.item(extra_loss.data),
'ntokens': sample['ntokens'],
'nsentences': sample['target'].size(0),
'sample_size': sample_size,
}
return loss, sample_size, logging_output
示例10: forward
# 需要导入模块: from fairseq import utils [as 别名]
# 或者: from fairseq.utils import item [as 别名]
def forward(self, model, sample, step=-1, epoche=-1, reduce=True):
"""Compute the loss for the given sample.
Returns a tuple with three elements:
1) the loss
2) the sample size, which is used as the denominator for the gradient
3) logging outputs to display while training
"""
net_output = model(**sample['net_input'])
loss, nll_loss = self.compute_loss_multi(model, net_output, sample, reduce=reduce)
sample_size = sample['target'].size(0) if self.args.sentence_avg else sample['ntokens']
logging_output = {
'loss': utils.item(loss.data) if reduce else loss.data,
'nll_loss': utils.item(nll_loss.data) if reduce else nll_loss.data,
'ntokens': sample['ntokens'],
'nsentences': sample['target'].size(0),
'sample_size': sample_size,
}
return loss, sample_size, logging_output
示例11: forward
# 需要导入模块: from fairseq import utils [as 别名]
# 或者: from fairseq.utils import item [as 别名]
def forward(self, model, sample, step=-1, epoche=-1, reduce=True):
"""Compute the loss for the given sample.
Returns a tuple with three elements:
1) the loss
2) the sample size, which is used as the denominator for the gradient
3) logging outputs to display while training
"""
net_output = model(**sample['net_input'])
loss, nll_loss = self.compute_loss(model, net_output, sample, reduce=reduce)
sample_size = sample['target'].size(0) if self.args.sentence_avg else sample['ntokens']
logging_output = {
'loss': utils.item(loss.data) if reduce else loss.data,
'nll_loss': utils.item(nll_loss.data) if reduce else nll_loss.data,
'ntokens': sample['ntokens'],
'nsentences': sample['target'].size(0),
'sample_size': sample_size,
}
return loss, sample_size, logging_output
示例12: forward
# 需要导入模块: from fairseq import utils [as 别名]
# 或者: from fairseq.utils import item [as 别名]
def forward(self, model, sample, step=-1, epoche=-1, reduce=True):
"""Compute the loss for the given sample.
Returns a tuple with three elements:
1) the loss
2) the sample size, which is used as the denominator for the gradient
3) logging outputs to display while training
"""
net_output = model(**sample['net_input'])
writing_loss, nll_loss = self.compute_loss(model, net_output, sample, reduce=reduce)
align_loss = self.compute_alignment_loss(net_output, sample)
loss = writing_loss + self.alpha * align_loss
sample_size = sample['target'].size(0) if self.args.sentence_avg else sample['ntokens']
logging_output = {
'loss': utils.item(loss.data) if reduce else loss.data,
'writing_loss': utils.item(writing_loss.data) if reduce else writing_loss.data,
'regul_loss': utils.item(align_loss.data) if reduce else align_loss.data,
'nll_loss': utils.item(nll_loss.data) if reduce else nll_loss.data,
'ntokens': sample['ntokens'],
'nsentences': sample['target'].size(0),
'sample_size': sample_size,
}
return loss, sample_size, logging_output
示例13: forward
# 需要导入模块: from fairseq import utils [as 别名]
# 或者: from fairseq.utils import item [as 别名]
def forward(self, model, sample, step=-1, epoche=-1, reduce=True):
"""Compute the loss for the given sample.
Returns a tuple with three elements:
1) the loss
2) the sample size, which is used as the denominator for the gradient
3) logging outputs to display while training
"""
net_output = model(**sample['net_input'], tgt_lengths=sample['tgt_lengths'])
loss, nll_loss = self.compute_loss(model, net_output, sample, reduce=reduce)
sample_size = sample['target'].size(0) if self.args.sentence_avg else sample['ntokens']
logging_output = {
'loss': utils.item(loss.data) if reduce else loss.data,
'nll_loss': utils.item(nll_loss.data) if reduce else nll_loss.data,
'ntokens': sample['ntokens'],
'nsentences': sample['target'].size(0),
'sample_size': sample_size,
}
return loss, sample_size, logging_output
示例14: forward
# 需要导入模块: from fairseq import utils [as 别名]
# 或者: from fairseq.utils import item [as 别名]
def forward(self, model, sample, step=-1, epoche=-1, reduce=True):
"""Compute the loss for the given sample.
Returns a tuple with three elements:
1) the loss
2) the sample size, which is used as the denominator for the gradient
3) logging outputs to display while training
"""
net_output = model(**sample['net_input'])
if self.weight_scale in ['inverse', 'sqrt', 'cubic-root']:
loss, nll_loss = self.compute_loss(model, net_output, sample, reduce=reduce)
elif self.weight_scale == 'propto':
loss, nll_loss = self.compute_propto_loss(model, net_output, sample, reduce=reduce)
sample_size = sample['target'].size(0) if self.args.sentence_avg else sample['ntokens']
logging_output = {
'loss': utils.item(loss.data) if reduce else loss.data,
'nll_loss': utils.item(nll_loss.data) if reduce else nll_loss.data,
'ntokens': sample['ntokens'],
'nsentences': sample['target'].size(0),
'sample_size': sample_size,
}
return loss, sample_size, logging_output
示例15: forward
# 需要导入模块: from fairseq import utils [as 别名]
# 或者: from fairseq.utils import item [as 别名]
def forward(self, model, sample, step=-1, epoche=-1, reduce=True):
"""Compute the loss for the given sample.
Returns a tuple with three elements:
1) the loss
2) the sample size, which is used as the denominator for the gradient
3) logging outputs to display while training
"""
net_output = model(**sample['net_input'], tgt_lengths=sample['tgt_lengths'])
writing_loss, nll_loss = self.compute_loss(model, net_output, sample, reduce=reduce)
regul_loss = net_output[1].float()
loss = writing_loss + self.alpha * regul_loss
sample_size = sample['target'].size(0) if self.args.sentence_avg else sample['ntokens']
logging_output = {
'loss': utils.item(loss.data),
'writing_loss': utils.item(writing_loss.data),
'nll_loss': utils.item(nll_loss.data) if reduce else nll_loss.data,
'regul_loss': utils.item(regul_loss.data),
'ntokens': sample['ntokens'],
'nsentences': sample['target'].size(0),
'sample_size': sample_size,
}
return loss, sample_size, logging_output