ValueError:没有足够的值来解包(预期3,得到2) - python / tensorflow

时间:2017-08-12 06:11:23

标签: python tensorflow jupyter-notebook

我正在尝试理解在jupyter笔记本上使用tensorflow框架实现的一些代码,当我在运行此单元格时收到以下错误。我不明白这里的问题,一些澄清会很有帮助。

作为参考,这里在张量流中创建训练解码层。

代码:

   def decoding_layer_train(encoder_state, dec_cell, dec_embed_input, 
                         target_sequence_length, max_summary_length, 
                         output_layer, keep_prob):
    """
    Create a decoding layer for training
    :param encoder_state: Encoder State
    :param dec_cell: Decoder RNN Cell
    :param dec_embed_input: Decoder embedded input
    :param target_sequence_length: The lengths of each sequence in the target batch
    :param max_summary_length: The length of the longest sequence in the batch
    :param output_layer: Function to apply the output layer
    :param keep_prob: Dropout keep probability
    :return: BasicDecoderOutput containing training logits and sample_id
    """
    # TODO: Implement Function

    # Helper for the training process; used by Basic Decoder to read inputs
    training_helper = tf.contrib.seq2seq.TrainingHelper(inputs=dec_embed_input,
                                                        sequence_length=target_sequence_length,
                                                        time_major=False)


    # Basic decoder
    training_decoder = tf.contrib.seq2seq.BasicDecoder(dec_cell,
                                                       training_helper,
                                                       encoder_state,
                                                        output_layer)


    # Performs dynamic decoding using the decoder
    training_decoder_output, final_state, final_sequence_lengths = tf.contrib.seq2seq.dynamic_decode(training_decoder,
                                                                                                     impute_finished=True,
                                                                                                     maximum_iterations=max_summary_length)

    return training_decoder_output



"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""



tests.test_decoding_layer_train(decoding_layer_train)enter code here

错误消息(屏幕截图):

Screenshot detailing the error

3 个答案:

答案 0 :(得分:0)

我刚刚改变了

training_decoder_output, final_state, final_sequence_lengths = tf.contrib.seq2seq.dynamic_decode(training_decoder...

training_decoder_output, _ = tf.contrib.seq2seq.dynamic_decode(training_decoder...

并且测试通过了1.1。

答案 1 :(得分:0)

“没有足够的值要解压(期望3,得到2)”,这意味着方法返回2个值,而调用方期望3个值。

training_decoder_output,final_state,final_sequence_lengths = tf.contrib.seq2seq.dynamic_decode(training_decoder,impute_finished = True,maximum_iterations = max_summary_length)

以上方法中的返回值期望不正确。 参考文档“ https://www.tensorflow.org/api_guides/python/contrib.seq2seq#Dynamic_Decoding”和“动态解码”,您将得到答案。

答案 2 :(得分:0)

已回答ValueError: not enough values to unpack (expected 3, got 2)的详细说明,请在此处粘贴我的代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-
# Function: Showing how to understand ValueError 'not enough values to unpack (expected 3, got 2)'
# Author: Crifan Li
# Update: 20191212

def notEnoughUnpack():
    """Showing how to understand python error `not enough values to unpack (expected 3, got 2)`"""
    # a dict, which single key's value is two part tuple
    valueIsTwoPartTupleDict = {
        "name1": ("lastname1", "email1"),
        "name2": ("lastname2", "email2"),
    }

    # Test case 1: got value from key
    gotLastname, gotEmail = valueIsTwoPartTupleDict["name1"] # OK
    print("gotLastname=%s, gotEmail=%s" % (gotLastname, gotEmail))
    # gotLastname, gotEmail, gotOtherSomeValue = valueIsTwoPartTupleDict["name1"] # -> ValueError not enough values to unpack (expected 3, got 2)

    # Test case 2: got from dict.items()
    for eachKey, eachValues in valueIsTwoPartTupleDict.items():
        print("eachKey=%s, eachValues=%s" % (eachKey, eachValues))
    # same as following:
    # Background knowledge: each of dict.items() return (key, values)
    # here above eachValues is a tuple of two parts
    for eachKey, (eachValuePart1, eachValuePart2) in valueIsTwoPartTupleDict.items():
        print("eachKey=%s, eachValuePart1=%s, eachValuePart2=%s" % (eachKey, eachValuePart1, eachValuePart2))
    # but following:
    for eachKey, (eachValuePart1, eachValuePart2, eachValuePart3) in valueIsTwoPartTupleDict.items(): # will -> ValueError not enough values to unpack (expected 3, got 2)
        pass

if __name__ == "__main__":
    notEnoughUnpack()

更多详细说明,请参阅another post