python - 如何使函数可挑选,以便可以通过 scipy.optimize.differential_evolution 的并行版本将其最小化
问题描述
我需要使用差分进化的 scipy 实现来最小化一个函数。我想利用并行性来加快计算速度,我尝试设置workers=-1。
我收到一个错误并搜索我发现问题是我试图最小化的功能不可选择。我需要帮助来了解如何让它变得可挑选。
最小化函数的工作方式如下:
- 一个类对象有一个属性向量,即观察到的数据。
- 该类的一种方法采用一些参数并计算向量的估计值。
- 最小化函数计算向量和计算估计之间的均方误差。
该函数的伪代码可能是这样的:
def function_to_minimize(self, parameters):
true_vector = self.true_vector
estimated_vector = self.estimate_vector(parameters)
return mse(true_vector, estimated_vector)
解决方案
像这样的东西应该工作:
class Objective(object):
def __init__(self, data):
self.measured_data = data
def __call__(self, parameters):
# need to return a scalar value
estimated_vector = self.estimate_vector(parameters)
return np.sum(np.power(self.measured_data - estimated_vector, 2))
def estimate_vector(parameters):
# calculate what you expect to happen with the parameters
pass
你应该传递Objective(data)
给differential_evolution
函数来最小化。在用作创建新进程的默认方式的 macOS 和 Windows 上,spawn
此函数应定义在可导入的文件中。
推荐阅读
- c++ - InitializeCriticalSectionEx 不在 KERNEL32.Dll 中
- apache-spark - SparkSQL:如何对具有不同时间戳的两个时间序列数据集求和
- javascript - CouchDB 的业务逻辑
- printf - CamlinternalFormatBasics.fmt 的类型错误
- c++ - 遍历二维数组的所有子数组
- relational-database - 追逐法
- reactjs - Reactstrap 表单没有响应
- python - 使用生成器表达式时如何从字典列表中获取项目的键
- php - gd-text PHP 库不将文本包装在框中
- android - 如何到达存储/模拟/0