成功解决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 total

     label weight.

   softmax_loss_function: Function (labels, logits) -> loss-batch

     to 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 accept

     named 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修改 targets

     log_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_size

 return log_perps


解决方法

crossent = softmax_loss_function(labels=targets, logits=logit)

修改为

crossent = softmax_loss_function(targets, logits)


大功告成!哈哈!






相关文章
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
1883 102
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
614 104
|
人工智能 自然语言处理 算法框架/工具
Python:现代编程的首选语言
Python:现代编程的首选语言
494 103
|
机器学习/深度学习 人工智能 数据挖掘
Python:现代编程的首选语言
Python:现代编程的首选语言
539 82
|
Python
Python编程:运算符详解
本文全面详解Python各类运算符,涵盖算术、比较、逻辑、赋值、位、身份、成员运算符及优先级规则,结合实例代码与运行结果,助你深入掌握Python运算符的使用方法与应用场景。
655 3
|
数据处理 Python
Python编程:类型转换与输入输出
本教程介绍Python中输入输出与类型转换的基础知识,涵盖input()和print()的使用,int()、float()等类型转换方法,并通过综合示例演示数据处理、错误处理及格式化输出,助你掌握核心编程技能。
871 3
|
并行计算 安全 计算机视觉
Python多进程编程:用multiprocessing突破GIL限制
Python中GIL限制多线程性能,尤其在CPU密集型任务中。`multiprocessing`模块通过创建独立进程,绕过GIL,实现真正的并行计算。它支持进程池、队列、管道、共享内存和同步机制,适用于科学计算、图像处理等场景。相比多线程,多进程更适合利用多核优势,虽有较高内存开销,但能显著提升性能。合理使用进程池与通信机制,可最大化效率。
720 3
|
Java 调度 数据库
Python threading模块:多线程编程的实战指南
本文深入讲解Python多线程编程,涵盖threading模块的核心用法:线程创建、生命周期、同步机制(锁、信号量、条件变量)、线程通信(队列)、守护线程与线程池应用。结合实战案例,如多线程下载器,帮助开发者提升程序并发性能,适用于I/O密集型任务处理。
1019 0
|
Python
Python编程基石:整型、浮点、字符串与布尔值完全解读
本文介绍了Python中的四种基本数据类型:整型(int)、浮点型(float)、字符串(str)和布尔型(bool)。整型表示无大小限制的整数,支持各类运算;浮点型遵循IEEE 754标准,需注意精度问题;字符串是不可变序列,支持多种操作与方法;布尔型仅有True和False两个值,可与其他类型转换。掌握这些类型及其转换规则是Python编程的基础。
974 33
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的多面手
Python:现代编程的多面手
590 0

推荐镜像

更多