美文网首页
2018-08-13multi-threads(2)

2018-08-13multi-threads(2)

作者: 镜中无我 | 来源:发表于2019-02-18 17:16 被阅读0次

protocol buffer:data format(like json,xml)

#note the nesting structure
example = tf.train.Example(features=tf.train.Features(feature={
            "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
            'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
        }))
writer.write(example.SerializeToString())  #serialized example to string

read

for serialized_example in tf.python_io.tf_record_iterator("train.tfrecords"):
  # 本段代码来自[TensorFlow高效读取数据]
  example = tf.train.Example()
  # 进行解析
  example.ParseFromString(serialized_example)
  # 逐个读取example对象里封装的东西
  image = example.features.feature['image'].bytes_list.value
  label = example.features.feature['label'].int64_list.value
  # 可以做一些预处理之类的
  print image, labe

note: when the string_input_producer() is called, the queue is still empty util start_queue_runners() is called
cast(data,dtype) is used for translating the type of data

import tensorflow as tf
filenames = ['A.csv', 'B.csv', 'C.csv']
#num_epoch: 设置迭代数
filename_queue = tf.train.string_input_producer(filenames, shuffle=False,num_epochs=3)
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
record_defaults = [['null'], ['null']]
#定义了多种解码器,每个解码器跟一个reader相连
example_list = [tf.decode_csv(value, record_defaults=record_defaults)
                  for _ in range(2)]  # Reader设置为2
# 使用tf.train.batch_join(),可以使用多个reader,并行读取数据。每个Reader使用一个线程。
example_batch, label_batch = tf.train.batch_join(
      example_list, batch_size=1)
#初始化本地变量
init_local_op = tf.initialize_local_variables()
with tf.Session() as sess:
    sess.run(init_local_op)
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)
    try:
        while not coord.should_stop():
            e_val,l_val = sess.run([example_batch,label_batch])
            print e_val,l_val
    except tf.errors.OutOfRangeError:
            print('Epochs Complete!')
    finally:
            coord.request_stop()
    coord.join(threads)
    coord.request_stop()
    coord.join(threads)

multi-reader, multi-threads

import tensorflow as tf
# 生成一个先入先出队列和一个QueueRunner,生成文件名队列
filenames = ['A.csv']
filename_queue = tf.train.string_input_producer(filenames, shuffle=False)
# 定义Reader
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
# 定义Decoder
record_defaults = [[1], [1], [1], [1], [1]]
col1, col2, col3, col4, col5 = tf.decode_csv(value,record_defaults=record_defaults)
features = tf.pack([col1, col2, col3])
label = tf.pack([col4,col5])
example_batch, label_batch = tf.train.shuffle_batch([features,label], batch_size=2, capacity=200, min_after_dequeue=100, num_threads=2)
# 运行Graph
with tf.Session() as sess:
    coord = tf.train.Coordinator()  #创建一个协调器,管理线程
    threads = tf.train.start_queue_runners(coord=coord)  #启动QueueRunner, 此时文件名队列已经进队。
    for i in range(10):
        e_val,l_val = sess.run([example_batch, label_batch])
        print e_val,l_val
    coord.request_stop()
    coord.join(threads)

one-hot encoding

def MyLoop(coord):
  while not coord.should_stop():
    ...do something...
    if ...some condition...:
      coord.request_stop()
 
# Main thread: create a coordinator.
coord = tf.train.Coordinator()
 
# Create 10 threads that run 'MyLoop()'
threads = [threading.Thread(target=MyLoop, args=(coord,)) for i in xrange(10)]
 
# Start the threads and wait for all of them to stop.
for t in threads:
  t.start()
coord.join(threads)
qr = tf.train.QueueRunner(queue, [enqueue_op] * 4)
# Launch the graph.
sess = tf.Session()
# Create a coordinator, launch the queue runner threads.
coord = tf.train.Coordinator()
enqueue_threads = qr.create_threads(sess, coord=coord, start=True)
# Run the training loop, controlling termination with the coordinator.
for step in xrange(1000000):
    if coord.should_stop():
        break
    sess.run(train_op)
# When done, ask the threads to stop.
coord.request_stop()
# And wait for them to actually do it.
coord.join(enqueue_threads)

image.png

相关文章

  • 2018-08-13multi-threads(2)

    protocol buffer:data format(like json,xml) read note: whe...

  • 2018-08-13multi-threads

    settings: pretreatment gonna slower the training proceedi...

  • DAY 2(2/2)

    五彩滩声名在外,但是我们去的时候在休整,我们十分不甘心,根据各种攻略告诉我们在景区出口有村民守着问你要不要去五彩滩...

  • 2-2-2

    自由写作群 转化与蜕变 继续刚才的梦的后记 我想梦是用最形象的比喻告诉我内在正在经历着发生着什么,这是潜意识里已经...

  • 2 (2)

    突然想到Jenny ,那个有些神经质的女孩儿。 对我来说,Jenny 给我最深的印象是作家。作为一个作家,她的灵感...

  • 2-2-2 RelativeLayout

    标注:本文为个人整理,仅做自己学习参考使用,请勿转载和转发2018-06-03: 初稿,参考博主coder-pig...

  • 2️⃣0️⃣2️⃣0️⃣🔚🔜2️⃣0️⃣2️⃣1️⃣

    今天风小了,夕阳很平静,但2020年终究是不平静的一年。 不平静的2020年,第一次有了一张小区出入证。不能飞去热...

  • 2-2

    ❤️起步,️️(若起步的右车道前方无车,可以不用转到左车道; 转发了右车道一定要变更车道) 一段车程 ❤️右转,右...

  • < маленький принц > 2-2

    Итак, в первый вечер я уснул на песке в пустыне, где на...

  • 2-2

    悠闲的一天。

网友评论

      本文标题:2018-08-13multi-threads(2)

      本文链接:https://www.haomeiwen.com/subject/wuvybftx.html