gpt4 book ai didi

python - 值错误 : TensorFlow requires that the following symbols must be defined before the loop

转载 作者:行者123 更新时间:2023-12-04 10:03:06 24 4
gpt4 key购买 nike

我正在尝试使用 tf.data API. 创建一个输入管道我有 3D 数据并使用普通的 NumPy 操作,我最终会得到一个尺寸为 [?,256x256x3x100] 的数组,可以将其视为 100 帧,每个 256x256x3 大小。

import glob
import os
import numpy as np
import tensorflow.compat.v1 as tf

def readfile(filenames):
flag = 0
for name in filenames:
string = tf.read_file(name)
image = tf.image.decode_image(string, channels=3)
if flag == 0:
bunch = image
flag = 1
else:
bunch = tf.concat([bunch,image],1)
return bunch

with tf.device("/cpu:0"):
train_files = []
for s in [x[0] for x in os.walk("path/to/data/folders")]:
if(s == "path/to/data/folders"):
continue
train_files.append(glob.glob(s+"/*.png"))
# shape of train_files is [5,100]
train_dataset = tf.data.Dataset.from_tensor_slices(train_files)
train_dataset = train_dataset.map(readfile, num_parallel_calls=16)

我认为发生错误是因为 'bunch' 正在改变 for 循环中的大小。错误:
ValueError                                Traceback (most recent call last)
<ipython-input-13-c2f88ca344dc> in <module>
22 train_dataset = train_dataset.map(
---> 23 readfile, num_parallel_calls=16)


ValueError: in converted code:

ValueError: TensorFlow requires that the following symbols must be defined before the loop: ('bunch',)

如何正确读取数据?

编辑

什么对我有用:
def readfile(filenames):
flag = 0
name = filenames[0]
string = tf.read_file(name)
image = tf.image.decode_image(string, channels=3)
bunch = image
for name in filenames:
string = tf.read_file(name)
image = tf.image.decode_image(string, channels=3)
if flag == 0:
bunch = image
flag = 1
else:
bunch = tf.concat([bunch,image],1)
return bunch

所以我不确定为什么需要初始化 bunch在循环之前,当第一次迭代时应该注意 bunch = image .可能是因为 flag 没有定义为张量所以 bunch = image从来没有真正运行过?

最佳答案

变量 bunch在函数内创建 readfile()因此出现错误,因为在运行时无法在循环内创建变量。解决方法是移动变量 bunch 的声明。在循环之外。代码示例如下:

import glob
import os
import numpy as np
import tensorflow.compat.v1 as tf

def readfile(filenames):
flag = 0
bunch = <some_appropriate_initialization>
for name in filenames:
string = tf.read_file(name)
image = tf.image.decode_image(string, channels=3)
if flag == 0:
bunch = image
flag = 1
else:
bunch = tf.concat([bunch,image],1)
return bunch

# Rest of the code

关于python - 值错误 : TensorFlow requires that the following symbols must be defined before the loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61735102/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com