为批插入计算最佳批大小的库
batch-size-automator的Python项目详细描述
批量自动售货机
batch_size_automator
是一个python库,它允许自动检测最佳批处理大小,以优化批处理数据操作(例如,数据库摄取)。在
为什么要用批处理自动机而不是其他库
还有什么图书馆?在
使用批处理自动机
安装此库:
pip install batch_size_automator
关于如何使用BSA的最基本版本。这将照顾到您的批处理大小,并随着时间的推移优化它以获得最大的性能。在
^{pr2}$设置
本章概述了构造函数参数及其如何影响BSA的行为。在
批次大小
当给定大于0的值时,将禁用自动批处理大小优化,并在调用get_next_batch_size
时始终返回此值。在
frombatch_size_automatorimportBatchSizeAutomatorimporttimebsa=BatchSizeAutomator(batch_size=100)whileTrue:batch_size=bsa.get_next_batch_size()# will always return 100 start=time.monotonic()# do your batch operation here using the batch_size variableduration=time.monotonic()-startbsa.insert_batch_time(duration)# will not do anything
数据批次大小
将要处理的数据的最小批处理大小,例如,如果一个数据集总是由5个数据点组成,而这些数据点在下一个独立于返回的批处理大小data_batch_size
将被设置为5
,这样可以确保返回的批处理大小始终是5的倍数。在
frombatch_size_automatorimportBatchSizeAutomatorimporttimebsa=BatchSizeAutomator(data_batch_size=5)whileTrue:batch_size=bsa.get_next_batch_size()# will always return a multitude of 5 (e.g. 5, 10, 20, 100, 555, ...) start=time.monotonic()# do your batch operation here using the batch_size variableduration=time.monotonic()-startbsa.insert_batch_time(duration)
主动
如果设置为False
将禁用自动批处理大小优化的布尔值。在没有改变的情况下,可以减少对其他代码的影响。在
frombatch_size_automatorimportBatchSizeAutomatorimporttimebsa=BatchSizeAutomator(active=False)whileTrue:batch_size=bsa.get_next_batch_size()# will always return 0 (or `batch_size` if set)start=time.monotonic()# do your batch operation here using the batch_size variableduration=time.monotonic()-startbsa.insert_batch_time(duration)# will not do anything
测试尺寸
用于确定经过多少次操作后将比较当前和最佳批次大小,从而计算新的批次大小。不应低于20(以减少异常值的影响),将其设置为大值将增加找到最佳批大小所需的时间。在
frombatch_size_automatorimportBatchSizeAutomatorimporttimebsa=BatchSizeAutomator(test_size=40)whileTrue:batch_size=bsa.get_next_batch_size()start=time.monotonic()# do your batch operation here using the batch_size variableduration=time.monotonic()-startbsa.insert_batch_time(duration)# will trigger recalculation of batch size after 40 iterations
设置尺寸
设置用于在操作之间更改批处理大小的初始步长。如果它小于data_batch_size
,data_batch_size
将被用作初始步长
frombatch_size_automatorimportBatchSizeAutomatorimporttimebsa=BatchSizeAutomator(step_size=300)whileTrue:batch_size=bsa.get_next_batch_size()# will return initial batch_size + 300 after the first recalculation start=time.monotonic()# do your batch operation here using the batch_size variableduration=time.monotonic()-startbsa.insert_batch_time(duration)
简化的功能
本章以一个简单的解释说明了BSA是如何工作的。在
模式
BSA包括两种模式:
- 寻找最佳批量大小
- 监视
寻找最佳批量大小
- BSA计算在最后一个测试周期内每秒插入多少行(
test_size
插入) - BSA比较当前结果是否优于最佳结果 2.a.如果电流更好,批量大小可根据步长进行调整 2.b.如果电流更差,批量大小将按上次调整的相反方向进行调整,并减小步长
- 重复步骤1到2,直到步骤大小低于阈值(这意味着我们经常输入2.b.并且应该已经找到了最佳的批量大小)
- 切换到监视模式
监视模式
- BSA将测试循环的长度增加到1000个插入
- 插入1000次后,BSA计算性能是否变差 2.a.如果性能更差,测试周期长度设置为20,我们切换到寻找最佳批量大小模式 2.b.如果性能相同或更好,则重复步骤1至2
- 项目
标签: