我正在尝试使用tensorboard和tensorflow,我这样做是为了设置:
rand = tf.placeholder(dtype=tf.float32) # this will be visualised in tensorboard later on
tf.summary.image('random_noise_visualisation', rand,max_outputs=5)
merged_summary_op = tf.summary.merge_all() # to me this seems like a helper to
# merge all tensorboard related operations
然后我评估我的merged_summary_op
并为其提供一个非常大的数组,大小约为1 GB。
它似乎没有使用已经使用的内存中的任何额外GPU内存。
我还试图评估我的rand
占位符,认为摘要操作可能会有特殊处理来阻止数据进入GPU。我做了:
random_value = np.random.randn(3000,224,224,1)
sess.run(rand,feed_dict={rand:random_value})
再次,没有额外的GPU利用率。
但是,当我这样做时
sess.run(rand + 2 ,feed_dict={rand:random_value}) # forced to do some calculation
还有额外的GPU利用率,增加了大约1 GB。
对于上述所有实验,我将我的会话用作:
sess = tf.InteractiveSession(graph=tf.Graph())
我的问题是:
答案 0 :(得分:2)
Tensorflow知道什么时候不打算将Tensor发送到GPU吗?
是。
事实上,在您的第一个rand
实验中,tensorflow发现不打扰任何设备,因为提供的提取rand
已经在feed_dict
中。这个相当简单的优化可以在session.py
中找到:
self._final_fetches = [x for x in self._fetches if x not in feeds]
...和later on in the same file:
# We only want to really perform the run if fetches or targets are provided,
# or if the call is a partial run that specifies feeds.
if final_fetches or final_targets or (handle and feed_dict_tensor):
results = self._do_run(handle, final_targets, final_fetches,
feed_dict_tensor, options, run_metadata)
else:
results = []
第二个实验不属于此优化,因此图表是真正的评估。 Tensorflow将占位符固定到可用的GPU上,因此也增加了GPU的使用率。
如果使用log_device_placement=True
:
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
random_value = np.random.randn(300,224,224,1)
print(sess.run(rand + 2, feed_dict={rand: random_value}).shape)
就图像摘要操作而言,它确实很特别:ImageSummary
op 没有GPU 实现。这是源代码(core/kernels/summary_image_op.cc
):
REGISTER_KERNEL_BUILDER(Name("ImageSummary").Device(DEVICE_CPU),
SummaryImageOp);
因此,如果您尝试手动将其置于CPU,session.run()
将抛出错误:
# THIS FAILS!
with tf.device('/gpu:0'):
tf.summary.image('random_noise_visualisation', rand,max_outputs=5)
merged_summary_op = tf.summary.merge_all() # to me this seems like a helper to
# merge all tensorboard related operations
这似乎是合理的,因为摘要操作不会执行任何复杂的计算,并且主要处理磁盘I / O.
ImageSummary
不是唯一的CPU操作,例如,所有摘要操作都是。有一个related GitHub issue,但目前没有更好的方法来检查GPU中是否支持特定操作,其他检查源代码。
通常,tensorflow会尝试尽可能多地利用可用资源,因此当GPU布局成为可能并且不存在其他限制时,引擎倾向于选择GPU而不是CPU。
从交互式会话更改为正常会话是否会影响此行为?
没有。 InteractiveSession
不会影响设备放置逻辑。唯一的区别在于InteractiveSession
在创建时使自己成为默认会话,而Session
仅在with
块内默认为会话。
是否有任何特定的文件?
我害怕在这里犯错,但可能不会。对我来说,最好的事实来源是源代码。