Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tensorflow值错误:要解包的值太多(预期为2个)_Python_Tensorflow_Runtime Error_Generator_Mnist - Fatal编程技术网

Python Tensorflow值错误:要解包的值太多(预期为2个)

Python Tensorflow值错误:要解包的值太多(预期为2个),python,tensorflow,runtime-error,generator,mnist,Python,Tensorflow,Runtime Error,Generator,Mnist,我已经在Reddit、堆栈溢出、技术论坛、文档、GitHub问题等上查找了这个问题,但仍然无法解决这个问题 作为参考,我在Windows10上使用的是64位的Python3TensorFlow 我试图用我自己的数据集(300张猫的图片,512x512,.png格式)在Tensorflow中训练它知道猫的样子。如果这能奏效,我将用其他动物和最终的物体来训练它 我似乎不明白为什么会出现错误ValueError:太多的值无法解压缩(预期为2个)。错误出现在第行图像中,labal=create_batc

我已经在Reddit、堆栈溢出、技术论坛、文档、GitHub问题等上查找了这个问题,但仍然无法解决这个问题

作为参考,我在Windows10上使用的是64位的Python3TensorFlow

我试图用我自己的数据集(300张猫的图片,512x512,.png格式)在
Tensorflow
中训练它知道猫的样子。如果这能奏效,我将用其他动物和最终的物体来训练它

我似乎不明白为什么会出现错误
ValueError:太多的值无法解压缩(预期为2个)
。错误出现在第
行图像中,labal=create_batches(10)
,它指向我的函数
create_batches
(见下文)。我不知道是什么原因导致了这种情况,因为我对
TensorFlow
相当陌生。我正试图在MNIST数据集的基础上建立自己的神经网络。代码如下:

将tensorflow导入为tf
将numpy作为np导入
导入操作系统
导入系统
进口cv2
内容=[]
标签列表=[]
打开(“data/cats/files.txt”)作为ff:
对于ff中的行:
line=line.rstrip()
content.append(第行)
打开(“data/cats/labels.txt”)作为fff:
对于fff中的linee:
linee=linee.rstrip()
标签\u列表。附加(linee)
def创建批次(批次大小):
图像=[]
对于内容中的img:
#f=打开(img,'rb')
#thedata=f.read().decode('utf8')
数据=cv2.imread(img)
thedata=tf.contrib.layers.flatten(thedata)
images.append(数据)
图像=np.asarray(图像)
labels=tf.convert_to_tensor(labels_列表,dtype=tf.string)
打印(内容)
#打印(标签列表)
虽然(正确):
对于范围(0298,10)内的i:
产量图像[i:i+批次大小],标签列表[i:i+批次大小]
imgs=tf.placeholder(dtype=tf.float32,shape=[None,262144])
lbls=tf.placeholder(dtype=tf.float32,shape=[None,10])
W=tf.变量(tf.零([262144,10]))
b=tf.变量(tf.零([10]))
y=tf.nn.softmax(tf.matmul(imgs,W)+b)
交叉熵=tf.reduce\u均值(-tf.reduce\u和(lbls*tf.log(y),reduce\u指数=[1]))
训练步长=tf.训练梯度降熵器(0.05).最小化(交叉熵)
sess=tf.InteractiveSession()
tf.global_variables_initializer().run()
对于范围(10000)内的i:
图像,labal=创建批次(10)
sess.run(train_step,feed_dict={imgs:images,lbls:labal})
正确的预测=tf.equal(tf.argmax(y,1),tf.argmax(lbls,1))
准确度=tf.reduce_平均值(tf.cast(正确的预测,tf.float32))
打印(sess.run(准确性、提要内容={imgs:content、lbls:labels\u list}))
错误是:

Traceback (most recent call last):
  File "B:\Josh\Programming\Python\imgpredict\predict.py", line 54, in <module>

    images,labal = create_batches(2)
ValueError: too many values to unpack (expected 2)
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
(A few hundred lines of this)
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
回溯(最近一次呼叫最后一次):
文件“B:\Josh\Programming\Python\imgpredict\predict.py”,第54行,在
图像,labal=创建批(2)
ValueError:要解压缩的值太多(应为2个)
libpng警告:iCCP:已知错误的sRGB配置文件
libpng警告:iCCP:已知错误的sRGB配置文件
libpng警告:iCCP:已知错误的sRGB配置文件
libpng警告:iCCP:已知错误的sRGB配置文件
(本书几百行)
libpng警告:iCCP:已知错误的sRGB配置文件
libpng警告:iCCP:已知错误的sRGB配置文件
libpng警告:iCCP:已知错误的sRGB配置文件

如果有人需要我的链接。项目文件夹是“imgpredict”。

您注释掉了第二个返回项

        yield(images[i:i+batch_size])    #,labels_list[i:i+batch_size])
您将生成一个列表来分配给
图像
,而
labal
则没有剩余内容。删除注释标记,或者在调试模式下生成一个伪值


更新

分隔此行并检查您要返回的内容:

result = (images[i:i+batch_size],
          labels_list[i:i+batch_size])
print len(result), result
return result

您正在以错误的方式产生结果:

yield(images[i:i+batch_size]) #,labels_list[i:i+batch_size])
这将为您提供一个已生成的值,但当您调用方法时,您希望生成两个值:

images,labal = create_batches(10)
其中一个产生两个值,如:

yield (images[i:i+batch_size] , labels_list[i:i+batch_size])
(取消注释)或仅期望一个

编辑:您应该在收益率和收到如下结果时使用括号:

#when yielding, remember that yield returns a Generator, therefore the ()
yield (images[i:i+batch_size] , labels_list[i:i+batch_size])

#When receiving also, even though this is not correct
(images,labal) = create_batches(10)
#do the training several times as you have
for i in range(10000):
    #now here you should iterate over your generator, in order to gain its benefits
    #that is you dont load the entire result set into memory
    #remember to receive with () as mentioned
    for (images, labal) in create_batches(10):
        #do whatever you want with that data
        sess.run(train_step, feed_dict={imgs:images, lbls: labal})
然而,这不是我使用
收益率
选项的方式;通常会迭代返回生成器的方法,在您的情况下,它应该如下所示:

#when yielding, remember that yield returns a Generator, therefore the ()
yield (images[i:i+batch_size] , labels_list[i:i+batch_size])

#When receiving also, even though this is not correct
(images,labal) = create_batches(10)
#do the training several times as you have
for i in range(10000):
    #now here you should iterate over your generator, in order to gain its benefits
    #that is you dont load the entire result set into memory
    #remember to receive with () as mentioned
    for (images, labal) in create_batches(10):
        #do whatever you want with that data
        sess.run(train_step, feed_dict={imgs:images, lbls: labal})

您还可以检查有关
产量
用户和生成器的问题。

哦。。。忘了吧。我得在家检查一下,我的轮班刚刚结束。如果这能解决问题,我会发疯的如果你进入精神错乱状态,记得带上有效护照,这样你参观完后就可以回家了。这是一个有趣的地方,但不是一个你想抚养孩子的地方。不幸的是,这并没有解决这个问题(我把远程桌面接入我的系统)。我得到了和以前一样的错误,所以我测试了它。结果证明我的程序可以通过前3批(它将“结果”输出到我的第30张图像),然后它决定停止工作。你知道为什么吗?没有附带的数据和结果,没有。我给你的是调试方面的帮助,不是解决方案。我修改了我的代码,但得到了与以前相同的错误。你能在这里添加修改的行或编辑问题吗?
yield(图像[I:I+batch\u size],标签列表[I:I+batch\u size])
我还编辑了问题我想我现在看到了。你应该把收益率分组在括号里,并用括号接收,现在编辑我的答案好的!这修复了第一个错误,现在我在
sess.run(train\u step,feed\u dict={imgs:images,lbls:labal})
行中有一个错误,但这是另一个问题。谢谢你的帮助:)