TensorFlow:tf.Session.run()获取和执行依赖项

时间:2017-09-14 20:06:32

标签: tensorflow

我有典型/通用TF(r1.3)配置,其中读取单个tf.record文件并解码结果以查询某些记录值:

System.Console.WriteLine("What Planet Are You Looking For? 1-8? ");
string planet1 = System.Console.ReadLine();

Planet planet = (Planet)Convert.ToInt32(planet1);

System.Console.WriteLine(planet);

请注意,文件队列被限制在import os, tensorflow as tf # path to TF record file containing single record record_file_path = os.path.join(os.getcwd(),'tf.record') # init a finite file queue with num_epochs=1 file_queue = tf.train.string_input_producer([record_file_path],name='file_queue',num_epochs=1) # init record reader reader = tf.TFRecordReader() # read the record file _, tfrecord_read_op = reader.read(file_queue) tfrecord = tf.parse_single_example( tfrecord_read_op, features={ 'image/height' : tf.FixedLenFeature([], tf.int64 ), 'image/width' : tf.FixedLenFeature([], tf.int64 ), 'image/label' : tf.FixedLenFeature([], tf.int64 ), 'image/encoded': tf.FixedLenFeature([], tf.string) }, name='features' ) # since exported as tf.int64 there is no need for tf.decode_raw heightT = tfrecord['image/height'] widthT = tfrecord['image/width' ] with tf.Session() as sess: # init vars sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer ()) # Start populating the filename queue queue_coordinator = tf.train.Coordinator() queue_worker_threads = tf.train.start_queue_runners(coord=queue_coordinator) # kaboom! height = sess.run(heightT) width = sess.run(widthT ) # why does this work ?? #height, width = sess.run([heightT, widthT]) # close down the queue queue_coordinator.request_stop() queue_coordinator.join(queue_worker_threads) print('image height x width : {}x{}'.format(height,width)) ,因此只会为num_epochs=1生成一次单个文件。

在计算图中,TFRecordReaderheightT张量操作都取决于widthT,这取决于tfrecord

因此,在评估tfrecord_read_op的任何时候,计算图依赖项都应该从tfrecord调用另一个队列。在评估单独 file_queue heightT次来电时widthTtf.Session时确实如此。

另外,如果run()(即单个文件无限次出列),单独的调用将成功(!?)并且num_epoch=None是默认值。

现在,最后,问题是:如何/为什么单个num_epochs=None调用多个提取成功评估两个张量操作?也就是说,为什么

run()

成功但

height,width = sess.run([heightT,widthT])

失败吗

实际上,使用占位符(dtype = tf.string)来保存tfrecord_read_op的结果并在占位符上执行height = sess.run(heightT) width = sess.run(widthT ) 可以减轻这种影响但是并没有真正阐明图形评估中发生的事情。以上突出显示的案例。

1 个答案:

答案 0 :(得分:0)

当运行height,width = sess.run([heightT,widthT]) TensorFlow将计算heightT, widthT的依赖关系时,得到一个将同时计算这两个Tensor的操作。然后Session运行此操作并存储计算结果然后获取它们。