TensorFlow摘要与线程

时间:2017-06-03 10:48:59

标签: python multithreading tensorflow tensorboard

我正在尝试向我的TensorFlow图表添加摘要,这些图表是异步运行的。我已经完成了单线程案例中的所有工作,但是一旦我进入多线程,总结似乎就消失了。这是我正在尝试做的一个玩具示例

import tensorflow as tf  # 1.1.0
import threading


class Worker:
    def __init__(self):
        self.x = tf.Variable([1, -2, 3], tf.float32, name='x')
        self.y = tf.Variable([-1, 2, -3], tf.float32, name='y')
        self.dot_product = tf.reduce_sum(tf.multiply(self.x, self.y))
        tf.summary.scalar("Dot_Product", self.dot_product)

    def work(self):
        for i in range(10):
            SESS.run(self.dot_product)

            # Write summary
            summary_str = SESS.run(tf.summary.merge_all())
            WRITER.add_summary(summary_str, i)
            WRITER.flush()

COORD = tf.train.Coordinator()
SESS = tf.Session()
WRITER = tf.summary.FileWriter(SUMMARY_DIR, SESS.graph)

# Single Thread  case
w = Worker()
SESS.run(tf.global_variables_initializer())
print(tf.get_collection(tf.GraphKeys.SUMMARIES))
w.work()

这很好用。但是,如果我去多线程:

# Multi-thread case
workers = [Worker() for i in range(4)]
SESS.run(tf.global_variables_initializer())
print(tf.get_collection(tf.GraphKeys.SUMMARIES))

worker_threads = []
for worker in workers:
    job = lambda: worker.work()
    t = threading.Thread(target=job)
    t.start()
    worker_threads.append(t)
COORD.join(worker_threads)

每当调用tf.summary.merge_all()时,我都会收到这样的错误,因为它无法看到任何摘要:

Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "/home/anjum/PycharmProjects/junk.py", line 43, in <lambda>
    job = lambda: worker.work()
  File "/home/anjum/PycharmProjects/junk.py", line 22, in work
    summary_str = SESS.run(tf.summary.merge_all())
  File "/usr/local/lib/python3.5/dist-
packages/tensorflow/python/client/session.py", line 778, in run
    run_metadata_ptr)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 969, in _run
fetch_handler = _FetchHandler(self._graph, fetches, feed_dict_string)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 408, in __init__
self._fetch_mapper = _FetchMapper.for_fetch(fetches)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 227, in for_fetch
(fetch, type(fetch)))
TypeError: Fetch argument None has invalid type <class 'NoneType'>

如果我将print(tf.get_collection(tf.GraphKeys.SUMMARIES))放在work()内,则会返回一个空列表。所以这意味着我的摘要在某个地方迷路了。

有人可以解释如何正确使用多线程摘要吗?

1 个答案:

答案 0 :(得分:0)

我想我已经弄明白了 - 摘要必须像这样合并。我不能100%确定TensorFlow为何如此挑剔

class Worker:
    def __init__(self):
        self.x = tf.Variable([1, -2, 3], tf.float32, name='x')
        self.y = tf.Variable([-1, 2, -3], tf.float32, name='y')
        self.dot_product = tf.reduce_sum(tf.multiply(self.x, self.y))
        tf.summary.scalar("Dot_Product", self.dot_product)

        self.summarise = tf.summary.merge_all()

    def work(self):
        for i in range(10):
            SESS.run(self.dot_product)

            # Write summary
            summary = SESS.run(self.summarise)
            WRITER.add_summary(summary, i)
            WRITER.flush()