【问题标题】:SGD to maximize distancesSGD 最大化距离
【发布时间】:2020-01-20 00:31:34
【问题描述】:

我正在尝试解决一个优化问题,即从点列表中找到最远的点。比如说,在一个有效区域内,找到一个最优的x,使得距离D(x, c1) + D(x, c2) + ... D(x, cn) 是最大的。 我被提示通过随机梯度下降来解决它,所以我完成了如下距离函数:

def distance(loc_x, loc_c):
  """
  arg:
    loc_x: 1x2 array: decision variable
    loc_c: 662x2 array: locations x needs to be away as much as possible
  """
  return np.sum(np.power(np.sum(np.power(loc_x - loc_c, 2), axis=1), 1/2))

但我不知道如何以编程方式进行。数学上我知道我想计算距离的导数并在derivative = 0时得到x_opt

【问题讨论】:

    标签: python optimization gradient-descent


    【解决方案1】:

    您必须找到尝试最小化的负距离函数(损失函数)的导数,并从一些随机 x 开始,并使用具有一定 gamma 学习率的增量步骤不断找到下一个 x。

    def df(x):
        # derivative function of your distance function
        return 
    max_iters = 100
    for _i in range(max_iters):
        current_x = next_x
        next_x = current_x - gamma * df(current_x)
        step = next_x - current_x
    print(current_x)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-18
      • 2023-03-22
      • 2020-10-09
      • 2021-03-03
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      • 1970-01-01
      相关资源
      最近更新 更多