【问题标题】:Yield ValueError: Too many vaues to unpack (expected 2) in pythonYield ValueError:在 python 中解压的值太多(预期为 2)
【发布时间】:2018-05-05 12:45:19
【问题描述】:

我在尝试实施此线程中提出的回归解决方案时遇到问题。

Using Keras ImageDataGenerator in a regression model

另一个堆栈问题也有类似的问题:Tensorflow ValueError: Too many vaues to unpack (expected 2) 但我找不到适合我的解决方案。我对产量进行了this 解释,但没有任何结果。对我来说奇怪的是,前两个循环完成了,但是当输出相同时它在第三个循环中崩溃。

对于目录,文件夹标记为 0、1 和 2,分别对应于 list_of_values 中的 0.1、0.3 和 0.5。

import numpy as np
from keras.preprocessing.image import ImageDataGenerator      
train_datagen = ImageDataGenerator(
            rescale=1./255,
            height_shift_range=0.15,
            shear_range=0.2)
def regression_flow_from_directory(flow_from_directory_gen, list_of_values):
    for x, y in flow_from_directory_gen:
        print (list_of_values[y], list_of_values,y)
        yield (x, list_of_values[y])
batch_size=3
list_of_values=[0.1,0.3,0.5]
(x_train,y_train) = regression_flow_from_directory(train_datagen.flow_from_directory(
                'figs/train',  # this is the target directory
                batch_size=batch_size,
                class_mode='sparse'),
                np.asarray(list_of_values)) 

输出

Found 9 images belonging to 3 classes.
[ 0.5  0.3  0.1] [ 0.1  0.3  0.5] [2 1 0]
[ 0.3  0.1  0.3] [ 0.1  0.3  0.5] [1 0 1]
[ 0.5  0.5  0.1] [ 0.1  0.3  0.5] [2 2 0]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-179-3cf97453bd05> in <module>()
      5         batch_size=batch_size,
      6         class_mode='sparse'),
----> 7         np.asarray(list_of_values))  

ValueError: too many values to unpack (expected 2)

编辑:错误在于将函数regression_flow_from_directory 返回到两个变量(x_train,y_train)。仅返回 x_train 会正确通过生成器。

x_train = regression_flow_from_directory(train_datagen.flow_from_directory(
        'figs/train',  # this is the target directory
        batch_size=batch_size,
        class_mode='sparse'),
        np.asarray(list_of_values)) 

【问题讨论】:

  • 欢迎来到 StackOverflow。请阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。我们应该能够将您发布的代码粘贴到文本文件中并重现您描述的问题。

标签: python tensorflow runtime-error keras generator


【解决方案1】:
(x_train,y_train) = regression_flow_from_directory(
    train_datagen.flow_from_directory(
        'figs/train',  # this is the target directory
        batch_size=batch_size,
        class_mode='sparse'),
        np.asarray(list_of_values))

问题似乎是您的例程regression_flow_from_directory 返回两个以上的值。您在该分配的左侧有一对,因此您必须在右侧恰好有两个值。尝试打印您的实际返回值,而不是组件。例如:

result = regression_flow_from_directory(...)
print (result)
(x,y) = result

你会发现问题:你必须用这些参数迭代regression_flow_from_directory

主体的简单示例:

>>> (x, y) = 1, 2
>>> x
1
>>> y
2
>>> (x, y) = 1, 2, 3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack

【讨论】:

  • 为什么你认为regression_flow_from_directory 是一个未定义的函数?它的定义对我来说看起来不错。
【解决方案2】:

该错误与np.asarray 无关。函数regression_flow_from_directory 包含一个yield 语句。因此,当你调用它时,你得到的不是一个产生值的元组,而是一个生成器对象。这只是一个对象,您试图将其解压缩为一个二元素元组。这就是错误消息的原因。

【讨论】:

  • 是的,错误在于试图解压到 (x_train,y_train)。将其更改为 x_train 有效。 x_train = regression_flow_from_directory(train_datagen.flow_from_directory( 'figs/train', # this is the target directory batch_size=batch_size, class_mode='sparse'), np.asarray(list_of_values))
猜你喜欢
  • 2016-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-09
  • 2017-08-29
  • 1970-01-01
相关资源
最近更新 更多