我正在构建用于馈送DNNRegressor模型的管道,问题是输入管道在前几百步之后变得非常慢。
我有数千个大小不一的TFRecord文件(大小从10MB到3GB)。每个输入有1038列。 我正在尝试从每个TFRecord文件创建具有1个“行”的512大小的批处理(每个文件都具有相关数据,因此我需要从512个被交错的文件中各取1行以具有“改组”的批处理)。去做这个, 我正在使用Data.interleave方法,这对于我的情况而言似乎是完美的。
问题是输入在开始时超快,但在前100步后变得非常慢。 随着管道速度变慢,CPU使用率从开始时的100%迅速增加到接近0%。但是,磁盘读取速度始终保持> 60 MBps。有什么问题?较小的文件是否以某种方式散布在管道中?
我曾尝试使用Parralel交错,但这甚至更慢
def parse_tf(example):
with tf.device('/cpu:0'):
parsed_features = tf.parse_example(example, feature)
feats = parsed_features
labels = parsed_features.pop('labels')
return feats, labels
def tf_input_fn(batch_size):
with tf.device('/cpu:0'):
files = os.listdir('D:/data/data')
files = ['D:/data/data/' + file for file in files]
dataset = (tf.data.Dataset.from_tensor_slices(files)
.interleave(lambda x:
tf.data.TFRecordDataset(x),
cycle_length=batch_size, block_length=1, num_parallel_calls=8)).batch(batch_size).map(
parse_tf, num_parallel_calls=1)
dataset = dataset.prefetch(100)
return dataset
if __name__ == "__main__":
batch_size_ = 512
data = tf_input_fn(batch_size_)
data_next = data.make_one_shot_iterator().get_next()
ctr = 0
with tf.Session() as data_sess:
data_sess.run(tf.global_variables_initializer())
tld_start = dt.datetime.now()
try:
while True:
tld_out = data_sess.run(data_next)
ctr += 1
if ctr % 100 == 0:
tld_end = dt.datetime.now()
print("Time for 100 steps: " + str(tld_end - tld_start))
tld_start = dt.datetime.now()
ctr = 0
except tf.errors.OutOfRangeError:
print("Done")
日志:
Time for 100 steps: 0:00:04.547647
Time for 100 steps: 0:00:03.132636
Time for 100 steps: 0:00:02.839416
Time for 100 steps: 0:00:02.639220
Time for 100 steps: 0:00:02.492228
Time for 100 steps: 0:00:01.991023
Time for 100 steps: 0:00:12.380473
Time for 100 steps: 0:00:29.162056
Time for 100 steps: 0:00:23.747592
Time for 100 steps: 0:00:28.041797
Time for 100 steps: 0:00:28.309055
Time for 100 steps: 0:00:26.240207
Time for 100 steps: 0:00:27.894402
Time for 100 steps: 0:00:25.832465
Time for 100 steps: 0:00:27.795538
Time for 100 steps: 0:00:28.219248
Time for 100 steps: 0:00:26.962029
Time for 100 steps: 0:00:24.568246
Time for 100 steps: 0:00:29.052262
Time for 100 steps: 0:00:27.003928
Time for 100 steps: 0:00:27.839937
Time for 100 steps: 0:00:28.380154
Time for 100 steps: 0:00:27.109850
Time for 100 steps: 0:00:28.234840