我试着和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
,并给它一个非常大的数组,大约1GB的大小。
它似乎不使用任何额外的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使用,增加了大约1GB。
在所有上述实验中,我将我的会话用作:
sess = tf.InteractiveSession(graph=tf.Graph())
我的问题是:
发布于 2018-01-12 05:35:28
Tensorflow知道什么时候不用给GPU发送张量吗?
是。
事实上,在您的第一次rand
实验中,tensorflow发现不需要打扰任何设备,因为提供的rand
已经在feed_dict
中了。这种相当简单的优化可以在session.py
中看到
self._final_fetches = [x for x in self._fetches if x not in feeds]
..。和稍后在同一文件中
# 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)
就图像摘要op而言,它确实很特殊: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操作,例如,所有的汇总操作都是。有一个相关GitHub问题,但是目前没有更好的方法来检查GPU中是否支持特定的op,还有其他检查源代码的方法。
一般来说,tensorflow试图利用尽可能多的可用资源,因此当GPU的放置是可能的,并且没有其他限制应用时,引擎倾向于选择GPU而不是CPU。
从交互式会话更改为正常会话会影响此行为吗?
不是的。InteractiveSession
不影响设备放置逻辑。唯一的最大区别是,InteractiveSession
在创建时使自己成为默认会话,而Session
仅在with
块中是默认的。
这方面有什么特别的文件吗?
我害怕错在这里,但很可能不会。对我来说,最好的真相来源是源代码。
https://stackoverflow.com/questions/48219522
复制相似问题