2017-05-29 5 views
0

私はTensorFlow FIFOQueueに特定の順序で項目をキューイングしていますが、同じ順序でそれらをデキューできると期待していますが、これは私が見ている動作ではありません。TensorFlow FIFOQueueはFIFOではありませんか?

次のスタンドアロンコードを実行すると、アプローチと動作が示されます。これはTensorFlow 1.1でPython 2.7(でもPython 3でも動作するかもしれません)で動作しています。

​​

期待される出力は、実際の出力は、バッチ0と1のステップの順序が正しくないこと

Batch 0, step 0 
[[ 0 1] 
[ 5 6] 
[10 11]] 
Batch 0, step 1 
[[ 4] 
[ 9] 
[14]] 
Batch 0, step 2 
[[ 2 3] 
[ 7 8] 
[12 13]] 
Batch 1, step 0 
[[15 16] 
[20 21] 
[25 26]] 
Batch 1, step 1 
[[19] 
[24] 
[29]] 
Batch 1, step 2 
[[17 18] 
[22 23] 
[27 28]] 
Batch 2, step 0 
[[30 31]] 
Batch 2, step 1 
[[32 33]] 
Batch 2, step 2 
[[34]] 

注ある

Batch 0, step 0 
[[ 0 1] 
[ 5 6] 
[10 11]] 
Batch 0, step 1 
[[ 2 3] 
[ 7 8] 
[12 13]] 
Batch 0, step 2 
[[ 4] 
[ 9] 
[14]] 
Batch 1, step 0 
[[15 16] 
[20 21] 
[25 26]] 
Batch 1, step 1 
[[17 18] 
[22 23] 
[27 28]] 
Batch 1, step 2 
[[19] 
[24] 
[29]] 
Batch 2, step 0 
[[30 31]] 
Batch 2, step 1 
[[32 33]] 
Batch 2, step 2 
[[34]] 

あります。私はステップの順序が何であるかを判断することができませんでした。バッチは常に順番になっているように見えますが、各バッチ内のステップは「ランダム」な順序で出てきます。決定的に見えますが、FIFOではありません。

私は、上記のコードで使用されている明示的な依存関係宣言の有無にかかわらず試しました。私はキュー容量を1に設定しようとしました。私はtf.groupを使用する代わりにenqueue_ops=enqueue_opsを設定しようとしましたが、これらの変更は役に立たず、最後のものは非常に奇妙な出力を引き起こしました。

tf.group多分依存関係を無視しますか?

答えて

0

tensorflow.python.ops.control_flow_ops.with_dependenciesは私が思ったように機能していないか、間違って使用していたようです。私が代わりにtf.control_dependenciesを使用するに切り替えると、私は私が必要とする動作を得る:

from __future__ import division, print_function, unicode_literals 
import math 
import numpy 
import tensorflow as tf 
from tensorflow.python.training import queue_runner 

row_count, column_count = 7, 5 
batch_size, step_size = 3, 2 

# Create some random data 
data = numpy.arange(row_count * column_count).reshape(
    (row_count, column_count)) 
print(data) 

batch_count = int(math.ceil(row_count/batch_size)) 
step_count = int(math.ceil(column_count/step_size)) 
print(batch_count, step_count) 

slices = tf.train.slice_input_producer([data], num_epochs=1, shuffle=False) 
batch = tf.train.batch(slices, batch_size, allow_smaller_final_batch=True) 

queue = tf.FIFOQueue(32, dtypes=[batch.dtype]) 
enqueue_ops = [] 
dependency = None 

for step_index in range(step_count): 
    step = tf.strided_slice(
     batch, [0, step_index * step_size], 
     [tf.shape(batch)[0], (step_index + 1) * step_size]) 

    if dependency is None: 
     dependency = queue.enqueue(step) 
    else: 
     with tf.control_dependencies([dependency]): 
      step = queue.enqueue(step) 
      dependency = step 

    enqueue_ops.append(step) 

queue_runner.add_queue_runner(queue_runner.QueueRunner(
    queue=queue, enqueue_ops=[tf.group(*enqueue_ops)])) 
step = queue.dequeue() 

supervisor = tf.train.Supervisor() 

with supervisor.managed_session() as session: 
    for batch_index in range(batch_count): 
     for step_index in range(step_count): 
      print("Batch %d, step %d" % (batch_index, step_index)) 
      print(session.run(step)) 

この回答はanswer to another SO questionによって動機づけられました。

関連する問題