2017-10-31 1 views
0

필자는 TFRecord를 작성하고 읽는 다음 스 니펫을 작성했습니다. 마지막 tf.run() 문은 python이 응답하지 않게합니다. 이것에 대한 이유는 무엇입니까? 간단한 tfrecord를 읽는 것이 왜 파이썬 충돌입니까?

fn = 'tmp.tfrecord' 
seqs = [[1,2,3], [0,1,0]] 

writer = tf.python_io.TFRecordWriter(fn) 

for seq in seqs: 
    ex = tf.train.Example(features= 
     tf.train.Features(feature={'seq': tf.train.Feature(int64_list=tf.train.Int64List(value=seq))})) 
    writer.write(ex.SerializeToString()) 

writer.close() 


# Now read the written records: 

filename_queue = tf.train.string_input_producer([fn]) 

reader = tf.TFRecordReader() 
key, serialized_example = reader.read(filename_queue) 

features = { 'seq': tf.FixedLenFeature([], dtype=tf.int64) } 

ex_parsed = tf.parse_single_example(
     serialized=serialized_example, features=features) 

print(ex_parsed) # -> prints a tensor 

with tf.Session() as sess: 
    print(sess.run([ex_parsed['seq']])) 

나는 코드에서 tf.train.Coordinator()를 포함하여 시도했지만 그 중 하나가 작동을 가져올 수 없습니다.

답변

1

tf.TFRecordReader 또는 tf.train.string_input_producer()의 출력을 평가하기 전에 start queue runners이 필요하므로 프로그램이 마지막 줄에 멈추었습니다. 세션을 생성 한 직후 tf.train.start_queue_runners(sess)에 대한 호출을 추가하십시오.

또는 새 tf.data API 사용할 수 있습니다 (TensorFlow 1.4 이상에서를, tf.contrib.data을 TensorFlow 1.2 및 1.3) 큐 주자에 대해 걱정할 필요없이, 데이터를 읽을 :

# A `tf.data.Dataset` containing all of the records in the file named `fn`. 
records = tf.data.TFRecordDataset(fn) 

features = {'seq': tf.FixedLenFeature([], dtype=tf.int64)} 

# A `tf.data.Dataset` whose elements are dictionaries mapping feature names 
# (in this case 'seq') to tensors, based on `features`. 
parsed = records.map(lambda x: tf.parse_single_example(x, features)) 

# Create a `tf.data.Iterator` to access individual elements of a `Dataset`. The 
# system will take care of creating any background threads for you. 
iterator = parsed.make_one_shot_iterator() 

# `ex_parsed` represents the next element of the iterator. It is a dictionary 
# mapping feature names to tensors. 
ex_parsed = iterator.get_next() 

with tf.Session() as sess: 
    print(sess.run(ex_parsed['seq'])) 
+0

큰 회신 감사합니다. tensorflow에서 새로운 작업 흐름의 일정한 흐름에 관해서 - 모범 사례의 이전/새로운 대안을 관련시키는 참조가 있습니까? – LearnOPhile

+1

필자는 이전 코드를 새 코드에 매핑하는 것에 대해서는 잘 알지 못합니다.하지만 유용한 정보는 있지만! [Programmer 's Guide] (https://www.tensorflow.org/programmers_guide/)는 아마도 가장 잘 구성된 것입니다 현재 모범 사례에 대한 출처. – mrry

관련 문제