【问题标题】:Tensorflow ValueError: Too many vaues to unpack (expected 2)Tensorflow ValueError:解包的值太多(预期为 2)
【发布时间】:2017-12-14 19:03:34
【问题描述】:

我在 Reddit、Stack Overflow、技术论坛、文档、GitHub 问题等上查找过这个问题,但仍然无法解决这个问题。

作为参考,我在 Windows 10 64 位上使用 Python 3 TensorFlow

我正在尝试使用我自己的 Tensorflow 数据集(300 张猫的照片,512x512,.png 格式)来训练它了解猫的样子。如果这行得通,我会用其他动物和最终物体来训练它。

我似乎无法弄清楚为什么会收到错误 ValueError: too many values to unpack (expected 2)。错误出现在images,labal = create_batches(10) 行中,它指向我的函数create_batches(见下文)。我不知道是什么原因造成的,因为我对TensorFlow 还很陌生。我正在尝试基于 MNIST 数据集制作自己的神经网络。代码如下:

import tensorflow as tf
import numpy as np
import os
import sys
import cv2


content = []
labels_list = []
with open("data/cats/files.txt") as ff:
    for line in ff:
        line = line.rstrip()
        content.append(line)

with open("data/cats/labels.txt") as fff:
    for linee in fff:
        linee = linee.rstrip()
        labels_list.append(linee)

def create_batches(batch_size):
    images = []
    for img in content:
        #f = open(img,'rb')
        #thedata = f.read().decode('utf8')
        thedata = cv2.imread(img)
        thedata = tf.contrib.layers.flatten(thedata)
        images.append(thedata)
    images = np.asarray(images)

    labels =tf.convert_to_tensor(labels_list,dtype=tf.string)

    print(content)
    #print(labels_list)

    while(True):
        for i in range(0,298,10):
            yield images[i:i+batch_size],labels_list[i:i+batch_size]


imgs = tf.placeholder(dtype=tf.float32,shape=[None,262144])
lbls = tf.placeholder(dtype=tf.float32,shape=[None,10])

W = tf.Variable(tf.zeros([262144,10]))
b = tf.Variable(tf.zeros([10]))

y_ = tf.nn.softmax(tf.matmul(imgs,W) + b)

cross_entropy = tf.reduce_mean(-tf.reduce_sum(lbls * tf.log(y_),reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.05).minimize(cross_entropy)

sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
for i in range(10000):
    images,labal = create_batches(10)
    sess.run(train_step, feed_dict={imgs:images, lbls: labal})

correct_prediction = tf.equal(tf.argmax(y_,1),tf.argmax(lbls,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

print(sess.run(accuracy, feed_dict={imgs:content, lbls:labels_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

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

【问题讨论】:

    标签: python tensorflow runtime-error generator mnist


    【解决方案1】:

    您以不正确的方式得出结果:

    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)
    

    然而这不是我使用yield 选项的方式;通常迭代 over 你的方法返回生成器,在你的情况下,它应该看起来像这样:

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

    您也可以查看this关于yield用户和生成器的问题。

    【讨论】:

    • 我修改了我的代码,但得到了和以前一样的错误。
    • 您可以在此处添加修改后的行或编辑问题吗?
    • yield(images[i:i+batch_size],labels_list[i:i+batch_size]) 我还编辑了问题
    • 我想我现在看到了。您应该将产量分组在括号中并且也用括号接收它,现在编辑我的答案
    • 好的!这修复了第一个错误,现在我在sess.run(train_step, feed_dict={imgs:images, lbls: labal}) 行上有一个错误,但这是另一个问题。感谢您的帮助:)
    【解决方案2】:

    您注释掉了第二个退货项目。

            yield(images[i:i+batch_size])    #,labels_list[i:i+batch_size])
    

    您生成一个列表以分配给images,而labal 没有任何剩余。如果您处于调试模式,请删除该注释标记,或者生成一个虚拟值。


    更新

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

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

    【讨论】:

    • 哦...忘记了。我必须在家里检查一下,我的工作班次刚刚结束。如果这能解决问题,我会发疯的
    • 如果您进入精神错乱状态,请记得带上有效护照,这样您就可以在参观完毕后回家。这是一个有趣的地方,但不是您想抚养孩子的地方。
    • 不幸的是,这并没有解决问题(我远程桌面进入我的系统)。我得到了和以前一样的错误。
    • 所以我测试了它。结果我的程序可以通过前 3 个批次(它输出“结果”到我的第 30 个图像),然后它决定停止工作。知道为什么吗?
    • 不是没有伴随的数据和结果,没有。我给你的是调试帮助,不是解决方案。
    猜你喜欢
    • 2017-08-29
    • 1970-01-01
    • 2014-07-31
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多