SciPy.optimize.fmin_l_bfgs_b什么时候评估损失质数?

时间:2018-12-22 07:23:55

标签: python scipy

我想使用import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Main { public static void main(String[] args) { List<String> list = Arrays.asList("Rec1 1 1", "Rec2 1 5", "Rec3 1 3", "Rec4 2 6", "Rec5 2 10)"); Collections.sort(list, new Comparator<String>() { @Override public int compare(String one, String two) { return Integer.valueOf(one.split(" ")[1]).compareTo(Integer.valueOf(two.split(" ")[1])); } }); System.out.println(list); } } 来最小化损失函数。因为我的代码中的SciPy.optimize.fmin_l_bfgs_b旨在一次运行返回loss_calculatorloss并执行一次评估是昂贵的,所以我必须以这种方式“欺骗”优化器:

loss_prime

这只是一个玩具代码,用以证明我的想法。如您所见,方法import numpy as np from scipy.optimize import fmin_l_bfgs_b as optimizer x_true = np.arange(0,10,0.1) m_true = 2.5 b_true = 1.0 y_true = m_true*x_true + b_true class test: def __init__(self, x_true, y_true): self.x_true = x_true self.y_true = y_true def func(self, params): x = self.x_true y = self.y_true m, b = params y_model = m*x+b error = y-y_model self.loss = sum(error**2) temp_dm = sum(2*x*(m*x+b-y)) temp_db = sum(2*(m*x+b-y)) self.dloss = np.asarray([temp_dm, temp_db]) def get_loss(self, params): self.params = params self.func(params) return self.loss def get_dloss(self, params): assert (self.params == params).all() return self.dloss temp_instance = test(x_true, y_true) initial_values = np.array([1.0, 0.0]) x = optimizer(temp_instance.get_loss, initial_values, temp_instance.get_dloss) 实际上什么也没做,get_dloss是在我们调用loss_prime时计算出来的。

此玩具代码适用于此玩具示例,但我不确定这在实际实现中是否合法。

我担心的是get_loss首先会评估SciPy.optimize.fmin_l_bfgs_b吗?

0 个答案:

没有答案