我正在关注TensorFlow书籍并遇到如下代码示例的问题:
import tensorflow as tf
# import os
# cwd = os.getcwd()
# print(cwd)
filenames = tf.train.match_filenames_once("../audio_dataset/*.wav")
count_num_files = tf.size(filenames)
filename_queue = tf.train.string_input_producer(filenames)
reader = tf.WholeFileReader()
filename, file_contents = reader.read(filename_queue)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
num_files = sess.run(count_num_files)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for i in range(num_files):
audio_file = sess.run(filename)
print(audio_file)
我收到的错误如下:
FailedPreconditionError Traceback (most recent call last)
FailedPreconditionError: Attempting to use uninitialized value matching_filenames_18
[[Node: matching_filenames_18/read = Identity[T=DT_STRING, _class=["loc:@matching_filenames_18"], _device="/job:localhost/replica:0/task:0/cpu:0"](matching_filenames_18)]]
我错过了什么?
的Marcin
答案 0 :(得分:3)
tf.train.match_filenames_once()
函数在内部创建一个存储匹配文件名的“局部变量”,但必须初始化此局部变量。为此,请按如下所示更改代码:
with tf.Session() as sess:
sess.run([tf.global_variables_initializer(), tf.local_variables_initializer()])
# [As before...]