Yield ValueError:太多信息无法在python中解包(预期2) [英] Yield ValueError: Too many vaues to unpack (expected 2) in python

查看:373
本文介绍了Yield ValueError:太多信息无法在python中解包(预期2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an issue trying to implement the regression solution proposed in this thread.

在回归模型中使用Keras ImageDataGenerator

另一个堆栈问题也有类似的问题: Tensorflow ValueError:无法解压的问题太多(预期2),但我找不到适合我的情况的解决方案.我经历了说明,没有任何结果.对我来说奇怪的是,前两个循环完成了,但是当输出相同时,它在第三个循环上崩溃了.

Another stack question had a similar issue: Tensorflow ValueError: Too many vaues to unpack (expected 2) but I couldnt find a solution that would work in my case. I went through this explanation for yield without any result. What is odd to me is that the first two loops complete but it crashes on the third when the outputs are identical.

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

For the directory, the folders are labeled 0, 1, and 2 corresponding to the 0.1, 0.3, and 0.5, respectively in the list_of_values.

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)

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

the Error was in returning the function regression_flow_from_directory to two variables (x_train, y_train). Returning only to x_train passes the generator correctly.

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)) 

推荐答案

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

The error has nothing to do with np.asarray. The function regression_flow_from_directory contains a yield statement. Therefore when you call it you get, not a tuple of yielded values, but a generator object. That's just one object, which you are trying to unpack into a two-element tuple. That's the reason for the error message.

这篇关于Yield ValueError:太多信息无法在python中解包(预期2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆