【发布时间】:2019-04-24 17:39:43
【问题描述】:
我有一个张量流模型,我正在 google-colab 上进行训练。实际模型更复杂,但我将其浓缩为reproducible example(去掉了保存/恢复、学习率衰减、断言、张量板事件、梯度裁剪等)。该模型工作合理(收敛到可接受的损失),我正在寻找加快训练速度的方法(每秒迭代次数)。
目前在 colab 的 GPU 上训练 1000 次迭代需要 10 分钟。我当前的批量大小为 512,这意味着该模型每秒处理 ~850 个示例(我希望批量大小为 512,除非其他大小提供合理的加速。改变批量大小本身并不会改变速度)。
所以目前我有一个以 tfrecord 格式存储的数据:这是一个500Mb example file,总数据大小约为 0.5Tb。这些数据通过了一个相当繁重的预处理步骤(我不能事先进行预处理,因为它会使我的 tfrecords 的大小大大超出我的承受能力)。预处理通过tf.data 完成,输出张量((batch_size, 8, 8, 24) 被视为 NHWC,(batch_size, 10))被传递到模型中。示例 colab 不包含仅用作示例的简化模型。
我尝试了一些方法来加快训练速度:
- manual device placement(在 cpu 上进行数据预处理,在 gpu 上进行传播),但我所有的尝试都导致速度变差(从 10% 提高到 50%)。
- 改进数据预处理。我查看了tf.data video 和data tutorials。我尝试了该教程中的几乎所有技术都没有任何改进(速度从 0% 降低到 15%)。特别是我尝试过:
dataset.prefetch(...)- 将
num_parallel_calls传递给地图 - 在
tf.contrib.data.map_and_batch中结合map和batch - 使用
parallel_interleave
数据预处理相关的代码在这里(这里是full reproducible example和example data):
_keys_to_map = {
'd': tf.FixedLenFeature([], tf.string), # data
's': tf.FixedLenFeature([], tf.int64), # score
}
def _parser(record):][3]
parsed = tf.parse_single_example(record, _keys_to_map)
return parsed['d'], parsed['s']
def init_tfrecord_dataset():
files_train = glob.glob(DIR_TFRECORDS + '*.tfrecord')
random.shuffle(files_train)
with tf.name_scope('tfr_iterator'):
ds = tf.data.TFRecordDataset(files_train) # define data from randomly ordered files
ds = ds.shuffle(buffer_size=10000) # select elements randomly from the buffer
ds = ds.map(_parser) # map them based on tfrecord format
ds = ds.batch(BATCH_SIZE, drop_remainder=True) # group elements in batch (remove batch of less than BATCH_SIZE)
ds = ds.repeat() # iterate infinitely
return ds.make_initializable_iterator() # initialize the iterator
def iterator_to_data(iterator):
"""Creates a part of the graph which reads the raw data from an iterator and transforms it to a
data ready to be passed to model.
Args:
iterator - iterator. Created by `init_tfrecord_dataset`
Returns:
data_board - (BATCH_SIZE, 8, 8, 24) you can think about as NWHC for images.
data_flags - (BATCH_SIZE, 10)
combined_score - (BATCH_SIZE,)
"""
b = tf.constant((128, 64, 32, 16, 8, 4, 2, 1), dtype=tf.uint8, name='unpacked_const')
with tf.name_scope('tfr_parse'):
with tf.name_scope('packed_data'):
next_element = iterator.get_next()
data_packed, score_int = next_element
score = tf.cast(score_int, tf.float64, name='score_float')
# https://stackoverflow.com/q/45454470/1090562
with tf.name_scope('data_unpacked'):
data_unpacked = tf.reshape(tf.mod(tf.to_int32(tf.decode_raw(data_packed, tf.uint8)[:,:,None] // b), 2), [BATCH_SIZE, 1552], name='data_unpack')
with tf.name_scope('score'):
with tf.name_scope('is_mate'):
score_is_mate = tf.cast(tf.squeeze(tf.slice(data_unpacked, [0, 1546], [BATCH_SIZE, 1])), tf.float64, name='is_mate')
with tf.name_scope('combined'):
combined_score = (1 - score_is_mate) * VALUE_A * tf.tanh(score / VALUE_K) + score_is_mate * tf.sign(score) * (VALUE_A + (1 - VALUE_A) / (VALUE_B - 1) * tf.reduce_max(tf.stack([tf.zeros(BATCH_SIZE, dtype=tf.float64), VALUE_B - tf.abs(score)]), axis=0))
with tf.name_scope('board'):
with tf.name_scope('reshape_layers'):
data_board = tf.reshape(tf.slice(data_unpacked, [0, 0], [BATCH_SIZE, 8 * 8 * 24]), [BATCH_SIZE, 8, 8, 24], name='board_reshape')
with tf.name_scope('combine_layers'):
data_board = tf.cast(tf.stack([
data_board[:,:,:, 0],
data_board[:,:,:, 4],
data_board[:,:,:, 8],
data_board[:,:,:,12],
data_board[:,:,:,16],
data_board[:,:,:,20],
- data_board[:,:,:, 1],
- data_board[:,:,:, 5],
- data_board[:,:,:, 9],
- data_board[:,:,:,13],
- data_board[:,:,:,17],
- data_board[:,:,:,21],
data_board[:,:,:, 2],
data_board[:,:,:, 6],
data_board[:,:,:,10],
data_board[:,:,:,14],
data_board[:,:,:,18],
data_board[:,:,:,22],
- data_board[:,:,:, 3],
- data_board[:,:,:, 7],
- data_board[:,:,:,11],
- data_board[:,:,:,15],
- data_board[:,:,:,19],
- data_board[:,:,:,23],
], axis=3), tf.float64, name='board_compact')
with tf.name_scope('flags'):
data_flags = tf.cast(tf.slice(data_unpacked, [0, 1536], [BATCH_SIZE, 10]), tf.float64, name='flags')
return data_board, data_flags, combined_score
我正在寻找可以提高训练速度(以示例/秒计)的实用解决方案(我已经尝试了大量的理论想法)。我不是在寻找提高模型准确性(或修改模型)的方法,因为这只是一个测试模型。
我花了很多时间尝试优化这个(并且放弃了)。所以我很乐意奖励 200 赏金,奖励一个有很好解释的可行解决方案。
【问题讨论】:
-
你是从驱动器读取 tfrecords 吗?
-
@mlRocks 是的,我正在从 gDrive 读取它。您实际上可以在问题的完整可重现链接中查看完整的实现。
-
@SalvadorDali 这是已知问题。因为它不是连接到计算机的物理驱动器,所以读取它会很慢
标签: python tensorflow tensorflow-datasets