【问题标题】:Getting error 'Too Many Values To Unpack' while looping over tensors; unable to use tf.train.batch循环张量时出现错误“太多值无法解包”;无法使用 tf.train.batch
【发布时间】:2017-09-03 17:16:59
【问题描述】:

当我初始化我的神经网络时:

print('Checking the Training on a Single Batch...')
with tf.Session() as sess:
    # Initializing the variables
    sess.run(tf.global_variables_initializer())

    # Training cycle
    for epoch in range(epochs):
        batch_i = 1
        for batch_features, batch_labels in (input_data, input_labels):
            train_neural_network(sess, optimizer, keep_probability, batch_features, batch_labels)
        print('Epoch {:>2}, Batch {}:  '.format(epoch + 1, batch_i), end='')
        print_stats(sess, batch_features, batch_labels, cost, accuracy)

我在 For 循环中得到 ValueError: too many values to unpack (expected 2)

我想可能是因为我没有创建批次,所以我创建了:

tf.train.batch([input_data, input_labels], batch_size, num_threads=1, capacity=32)

但我得到错误:

TypeError: Cannot convert a list containing a tensor of dtype <dtype: 'uint8'> to <dtype: 'float32'> (Tensor is: <tf.Tensor 'stack_16495:0' shape=(280, 440, 3) dtype=uint8>)

input_data / input_labels 都是使用tf.stack 创建的数组张量列表。

【问题讨论】:

    标签: python image-processing tensorflow neural-network


    【解决方案1】:

    ValueError:声明引起的

    for batch_features, batch_labels in (input_data, input_labels):
    

    你需要

    for batch_features, batch_labels in zip(input_data, input_labels):
    

    相反。 (input_data, input_labels) 产生包含两个元素的元组 - input_datainput_labelszip 使用来自 input_datainput_labels 的元素创建 tupleslist
    带代码的小例子

    a = [1,2,3]
    b = ['a', 'b', 'c']
    c = (a, b)
    d = zip(a, b)
    c
    Out[16]: ([1, 2, 3], ['a', 'b', 'c'])
    d
    Out[17]: [(1, 'a'), (2, 'b'), (3, 'c')]
    

    【讨论】:

    • 现在我得到了:ValueError: Cannot feed value of shape (0,) for Tensor 'x:0', which has shape '(?, 400, 600, 1)' 我知道这是因为我的标签形状是 1d... 改变这种情况的最佳方法是什么?我的标签目前是二进制(1 或 0)
    • @raph 要回答这个问题,我需要更多代码——例如train_neural_network 函数——以及更多数据——batch_features 的形状、batch_labels、NN 的尺寸等。因为这是不同的问题,所以这是一个很好的提出新问题的想法。
    • 好的,非常感谢。我创建了一个新问题并添加了我的代码的 github 要点:stackoverflow.com/questions/43295607/…
    猜你喜欢
    • 2018-10-07
    • 2023-03-17
    • 2018-04-06
    • 2013-06-06
    • 2021-04-28
    • 1970-01-01
    • 2020-12-30
    • 2020-04-25
    • 2021-01-16
    相关资源
    最近更新 更多