在摆弄TensorFlow时,我注意到一个相对简单的任务(批处理一些3D加速度计数据并取每个时期的总和)的性能相对较差。这是我运行的东西的精髓,一旦我获得了(难以置信的漂亮!) Timeline功能:
import numpy as np
import tensorflow as tf
from tensorflow.python.client import timeline
# Some dummy functions to compute "features" from the data
def compute_features( data ):
feature_functions = [
lambda x: test_sum( x, axis = 0 ),
lambda x: test_sum( x, axis = 1 ),
lambda x: test_sum( x, axis = 2 ),
]
return tf.convert_to_tensor( [ f( data ) for f in feature_functions ] )
def test_sum( data, axis = 0 ):
t, v = data
return tf.reduce_sum( v[:, axis] )
# Setup for using Timeline
sess = tf.Session()
run_options = tf.RunOptions( trace_level = tf.RunOptions.FULL_TRACE )
run_metadata = tf.RunMetadata()
# Some magic numbers for our dataset
test_sampling_rate = 5000.0
segment_size = int( 60 * test_sampling_rate )
# Load the dataset
with np.load( 'data.npz' ) as data:
t_raw = data['t']
v_raw = data['v']
# Build the iterator
full_dataset = tf.data.Dataset.from_tensor_slices( (t_raw, v_raw) ).batch( segment_size )
dataset_iterator = full_dataset.make_initializable_iterator()
next_datum = dataset_iterator.get_next()
sess.run( dataset_iterator.initializer )
i = 0
while True:
try:
print( sess.run( compute_features( next_datum ), options = run_options,
run_metadata = run_metadata ) )
# Write Timeline data to a file for analysis later
tl = timeline.Timeline( run_metadata.step_stats )
ctf = tl.generate_chrome_trace_format()
with open( 'timeline_{0}.json'.format( i ), 'w' ) as f:
f.write( ctf )
i += 1
except tf.errors.OutOfRangeError:
break在Chrome中,我观察到在每一次迭代中,IteratorGetNext消耗了大部分时间:
Screenshot of Chrome displaying the timeline for one iteration
正如您所看到的,计算的“主要”部分被放在右边的小点中,而这个周期的绝大多数时间都停留在IteratorGetNext中。
我想知道我是否遗漏了任何明显的东西,就像我构建我的图的方式一样,这会导致这一步的性能如此显著地下降。我有点困惑,为什么这个设置的性能如此之差。
发布于 2018-02-10 07:19:03
如果IteratorGetNext在时间线上显示为大型事件,那么您的模型在输入处理方面遇到了瓶颈。在本例中,流水线非常简单,但在将300,000个元素复制到批处理时却是一个瓶颈。您可以通过将Dataset.prefetch(1)转换添加到数据集定义来将此副本移出关键路径:
full_dataset = (tf.data.Dataset.from_tensor_slices((t_raw, v_raw))
.batch(segment_size)
.prefetch(1))有关更多性能建议,请参阅tensorflow.org上的新Input Pipeline Performance Guide。
PS。在循环中调用compute_features(next_datum)会导致图形随着时间的推移而增长,而循环则会变慢。按如下方式重写它会更有效率:
next_computed_features = compute_features(next_datum)
while True:
try:
print(sess.run(next_computed_features, options=run_options,
run_metadata=run_metadata))
# ...
except tf.errors.OutOfRangeError:
breakhttps://stackoverflow.com/questions/48715062
复制相似问题