You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何加速Scipy Optimize中矢量化对数运算的优化速度?

优化任务中复合弹性计算导致运算耗时剧增的优化方案咨询

我正在执行一项优化任务,需要通过弹性系数计算实现转化率变化对应的金额。最初使用简单弹性计算时,优化耗时仅3分钟,但切换为复合弹性计算后,耗时直接飙升至数天。

计算公式

简单弹性计算

(ChangeInConversion * 100 / Elasticity) * -1

复合弹性计算

(np.log(1 + ChangeInConversion ) / np.log(1 + Elasticity)) * 100 * -1 

我使用Scipy Optimize包进行优化,其中ChangeInConversion和Elasticity均为numpy数组形式的向量。想请教:

  • 是否有方法可以加速复合弹性计算的运算速度?
  • 优化函数中引入对数运算后出现如此大幅的耗时增长是否属于正常现象?

补充代码与变量说明

核心函数代码

def FDollarChange(OptimizedUnitCount): 
    _OptimizedUnitCount = np.array(OptimizedUnitCount)
    _OptimizedUnitCountByChannel = np.dot(ChannelMatrix.toarray(), _OptimizedUnitCount)
    _ChangeInUnitCountByChannel = _OptimizedUnitCountByChannel - CurrentUnitCountByChannel
    ChangeInConversion  = _ChangeInUnitCountByChannel / CurrentUnitCountByChannel

    # 扩展回原始向量维度
    ChangeInConversion  = np.dot(ChannelMatrixT.toarray(), ChangeInConversion)
    DollarChange = (ChangeInConversion * 100 / Elasticity) * -1 # 简单方法
    # DollarChange = (np.log(1 + ChangeInConversion) / np.log(1 + Elasticity)) * 100 * -1 # 复合方法
    
    return DollarChange, ChangeInConversion

def FTotalProfit(OptimizedUnitCount):
    _OptimizedUnitCount = np.array(OptimizedUnitCount)
    _DollarChange, _ChangeInConversion = FDollarChange(OptimizedUnitCount)
    PostProfit = PreProfit + _DollarChange
    TotalProfit = np.dot(_OptimizedUnitCount, TotalProfit)

    return PostProfit, TotalProfit

def ObjectiveFunction(OptimizedUnitCount):
    _OptimizedUnitCount = np.array(OptimizedUnitCount)
    _PostProfit, _TotalProfit = FTotalProfit(_OptimizedUnitCount)

    return (_TotalProfit * -1)

关键变量说明

  • OptimizedUnitCount:优化器中的自变量x
  • ChannelMatrix:用于分组求和的稀疏矩阵,可计算分组后的OptimizedUnitCount
  • ChannelMatrixT:ChannelMatrix的转置矩阵,用于将数据转换回原始向量尺寸

内容的提问来源于stack exchange,提问作者we_are_all_in_this_together

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 00:21:04