欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

成功解决python\ops\seq2seq.py TypeError: ms_error() got an unexpected keyword argument 'labels'

发布时间:2025/3/21 python 57 豆豆
生活随笔 收集整理的这篇文章主要介绍了 成功解决python\ops\seq2seq.py TypeError: ms_error() got an unexpected keyword argument 'labels' 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

成功解决python\ops\seq2seq.py TypeError: ms_error() got an unexpected keyword argument 'labels'

 

 

 

目录

解决问题

解决思路

解决方法


 

 

 

 

 

解决问题

错误地址:contrib\legacy_seq2seq\python\ops\seq2seq.py", line 1098, in sequence_loss_by_example
TypeError: ms_error() got an unexpected keyword argument 'labels'

 

 

 

 

解决思路

查看函数使用方法

def sequence_loss_by_example(logits,targets,weights,average_across_timesteps=True,softmax_loss_function=None,name=None):"""Weighted cross-entropy loss for a sequence of logits (per example).Args:logits: List of 2D Tensors of shape [batch_size x num_decoder_symbols].targets: List of 1D batch-sized int32 Tensors of the same length as logits.weights: List of 1D batch-sized float-Tensors of the same length as logits.average_across_timesteps: If set, divide the returned cost by the totallabel weight.softmax_loss_function: Function (labels, logits) -> loss-batchto be used instead of the standard softmax (the default if this is None).**Note that to avoid confusion, it is required for the function to acceptnamed arguments.**name: Optional name for this operation, default: "sequence_loss_by_example".Returns:1D batch-sized float Tensor: The log-perplexity for each sequence.Raises:ValueError: If len(logits) is different from len(targets) or len(weights)."""if len(targets) != len(logits) or len(weights) != len(logits):raise ValueError("Lengths of logits, weights, and targets must be the same ""%d, %d, %d." % (len(logits), len(weights), len(targets)))with ops.name_scope(name, "sequence_loss_by_example",logits + targets + weights):log_perp_list = []for logit, target, weight in zip(logits, targets, weights):if softmax_loss_function is None:# TODO(irving,ebrevdo): This reshape is needed because# sequence_loss_by_example is called with scalars sometimes, which# violates our general scalar strictness policy.target = array_ops.reshape(target, [-1])crossent = nn_ops.sparse_softmax_cross_entropy_with_logits(labels=target, logits=logit)else:crossent = softmax_loss_function(targets, logits=logit) #190318修改 targetslog_perp_list.append(crossent * weight)log_perps = math_ops.add_n(log_perp_list)if average_across_timesteps:total_size = math_ops.add_n(weights)total_size += 1e-12 # Just to avoid division by 0 for all-0 weights.log_perps /= total_sizereturn log_perps

 

 

 

解决方法

crossent = softmax_loss_function(labels=targets, logits=logit) 
修改为
crossent = softmax_loss_function(targets, logits) 

大功告成!哈哈!

 

 

《新程序员》:云原生和全面数字化实践50位技术专家共同创作,文字、视频、音频交互阅读

总结

以上是生活随笔为你收集整理的成功解决python\ops\seq2seq.py TypeError: ms_error() got an unexpected keyword argument 'labels'的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。