我有一个tensorflow模型,我正在google-colab上训练.实际模型更复杂,但我将其浓缩为可重复的示例(删除了保存/恢复,学习速率衰减,断言,张量事件,渐变剪切等).该模型合理地工作(收敛到可接受的损失),我正在寻找一种加速训练的方法(每秒迭代次数).
目前在colab的GPU上,需要10分钟来训练1000次迭代.我目前的批量大小为512,这意味着该模型每秒处理约850个示例(我希望批量大小为512,除非其他大小提供合理的加速.本身改变批量大小不会改变速度).
所以目前我有一个以tfrecord格式存储的数据:这是一个500Mb的示例文件,总数据大小约为0.5Tb.这些数据经过了一个相当繁重的预处理步骤(我不能事先进行预处理,因为它会增加我的tfrecords的大小,超出我能承受的范围).预处理通过tf.data完成,输出张量((batch_size, 8, 8, 24)被视为NHWC (batch_size, 10))被传递到模型中.示例colab不包含简化模型,仅作为示例.
我尝试了一些方法来加速训练:
dataset.prefetch(...)num_parallel_calls到地图tf.contrib.data.map_and_batchparallel_interleave与数据预处理相关的代码在这里(这是一个完整的可重复示例,带有示例数据):
_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 …Run Code Online (Sandbox Code Playgroud) 我正在使用多个 tfRecord 文件并希望从中读取以创建数据集。我正在尝试使用来自_tensor_slices 的路径并使用该数据集进一步读取 TFRecords
(多个 tfRecords 的优点:https ://datascience.stackexchange.com/questions/16318/what-is-the-benefit-of-splitting-tfrecord-file-into-shards )
我想知道是否有更简单且行之有效的方法来做到这一点。
file_names_dataset = tf.data.Dataset.from_tensor_slices(filenames_full)
def read(inp):
return tf.data.TFRecordDataset(inp)
file_content = file_names.map(read)
Run Code Online (Sandbox Code Playgroud)
我的下一步是使用 tf.io.parse_single_example 解析数据集。