TensorFlow报错:TypeError: msr_error() got an unexpected keyword argument ‘labels’
学习莫烦TsnsorFlow教程时,第 20.3 RNN lstm(regression 回归例子) 遇到的一个报错如下:
TypeError: msr_error() got an unexpected keyword argument ‘labels’
分析:
-
msr_error() 函数得到一个意外的关键参数 ‘lables’
-
通过 help() 查看 tf.contrib.legacy_seq2seq.sequence_loss_by_example 文档
help(tf.contrib.legacy_seq2seq.sequence_loss_by_example)
Help on function sequence_loss_by_example in module tensorflow.contrib.legacy_seq2seq.python.ops.seq2seq: 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).
-
softmax_loss_function 参数说明中 Note that to avoid confusion, it is required for the function to accept named arguments,即 注意,为了避免混淆,函数需要接受命名参数。
解决方式:定义 msr_error() 函数时,使用 labels,logits 指定,将
def msr_error(self, y_pre, y_target):
return tf.square(tf.subtract(y_pre, y_target))
改为
def msr_error(self, logits, labels):
return tf.square(tf.subtract(logits, labels))