Tensorflow ValueError:要解压缩的值太多(预期为2) [英] Tensorflow ValueError: Too many vaues to unpack (expected 2)

查看:108
本文介绍了Tensorflow ValueError:要解压缩的值太多(预期为2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Reddit,Stack Overflow,技术论坛,文档,GitHub问题等上进行了查找,但仍然无法解决此问题.

I have looked this up on Reddit, Stack Overflow, tech forums, documentation, GitHub issues etc etc and still can't solve this issue.

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

For reference, I am using Python 3 TensorFlow on Windows 10, 64 Bit.

我正在尝试使用自己的数据集(300张猫的图片,512x512,.png格式)在Tensorflow中进行训练,以了解猫的样子.如果这行得通,我将与其他动物以及最终的物体一起训练.

I am trying to use my own dataset (300 pics of cats, 512x512, .png format) in Tensorflow to train it to know what a cat looks like. If this works I will train it with other animals and eventually objects.

我似乎无法弄清楚为什么出现错误ValueError: too many values to unpack (expected 2).错误出现在images,labal = create_batches(10)行中,该行指向我的函数create_batches(请参见下文).我不知道是什么原因引起的,因为我对TensorFlow相当陌生.我正在尝试根据MNIST数据集创建自己的神经网络.下面的代码:

I can't seem to figure out why I am getting the error ValueError: too many values to unpack (expected 2). The error appears in the line images,labal = create_batches(10), which points to my function create_batches (see below). I don't know what could be causing this as I am fairly new to TensorFlow. I am trying to make my own Neural Network based on the MNIST Dataset. Code below:

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链接链接(如果有人需要的话).项目文件夹是"imgpredict".

My GitHub link link if anyone needs it. The project folder is the "imgpredict".

推荐答案

您以不正确的方式产生结果:

You are yielding your results in an incorrect way:

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

这将为您提供一个产生的值,但是当您调用方法时,您会期望产生两个值:

which gives you one value that is yielded, but when you call you method you are expecting two values yielded:

images,labal = create_batches(10)

要么产生两个值,如:

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

(取消注释)或只期望一个.

(uncomment) or just expect one.

编辑:在收益率和接收结果时都应使用括号,如下所示:

Edit: You should use parentheses on both the yield and when receiving the results like this:

#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选项的方式;通常在您返回返回生成器的方法上遍历 ,在您的情况下,它应该看起来像这样:

However this is not the way I have used the yield option; one usually iterates over your method that returns the generator, in your case it should look something like this:

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

您还可以检查有关用户的问题 yield和生成器.

You can also check this question regarding the user of yield and generators.

这篇关于Tensorflow ValueError:要解压缩的值太多(预期为2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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