代码之家  ›  专栏  ›  技术社区  ›  user5593270

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

  •  2
  • user5593270  · 技术社区  · 7 年前

    作为参考,我使用 Python 3 TensorFlow

    我正在尝试使用我自己的数据集(300张猫图片,512x512.png格式) Tensorflow

    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”。

    2 回复  |  直到 7 年前
        1
  •  2
  •   DarkCygnus    7 年前

    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 结束 返回生成器的方法,在您的示例中应该如下所示:

    #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 关于用户的问题 产量

        2
  •  0
  •   Prune    7 年前

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

            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