Tensorflow报错:Python: FailedPreconditionError: Attempting to use uninitialized value

Python: FailedPreconditionError (see above for traceback): Attempting to use uninitialized value matching_filenames_7

《machine learning with tensorflow》上第5章有这样一段代码,

原来的代码是这样的,

import tensorflow as tf    
​
filenames = tf.train.match_filenames_once("./audio_dataset/*.wav")
count_num_files = tf.size(filenames)
print(count_num_files)
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 (see above for traceback): Attempting to use uninitialized value matching_filenames_7
     [[Node: matching_filenames_7/read = Identity[T=DT_STRING, _device="/job:localhost/replica:0/task:0/device:CPU:0"](matching_filenames_7)]]

解决办法

init = (tf.global_variables_initializer(), tf.local_variables_initializer())
with tf.Session() as sess:
    sess.run(init) 

我这里用的python3.6.5, 估计python版本更新后,对局部变量有了严格的初始化要求。

posted @ 2018-08-31 15:55  SpaceVision  阅读(92)  评论(0编辑  收藏  举报