Python RuntimeWarning:在对线性搜索建模时双_标量中遇到溢出

Python RuntimeWarning:在对线性搜索建模时双_标量中遇到溢出,python,linear-gradients,Python,Linear Gradients,我的密码在这里。我想使用linearsearch获得结果,但我有溢出问题 import numpy as np from matplotlib import pyplot as plt def f(x): return (x[0] - 1) ** 2 + 2 * (x[0] ** 2 - x[1]) ** 2 # gradient def dot_f(x): dx = np.array([8 * (x[0]) ** 3 + 2 * x[0] - 8 * x[0] * x[

我的密码在这里。我想使用linearsearch获得结果,但我有溢出问题

import numpy as np
from matplotlib import pyplot as plt


def f(x):
    return (x[0] - 1) ** 2 + 2 * (x[0] ** 2 - x[1]) ** 2


# gradient
def dot_f(x):
    dx = np.array([8 * (x[0]) ** 3 + 2 * x[0] - 8 * x[0] * x[1] - 2,
                   4 * x[1] - 4 * (x[0]) ** 2])
    return dx


# hessian matrix
def ddot_f(x):
    return np.array([24 * (x[0]) ** 2 + 2 - 8 * x[1], -8 * x[0]],
                    [-8 * x[0], 4])


def fs(x, y):
    return (x - 1) ** 2 * 2 + 2 * (x ** 2 - y) ** 2


def exactlinearsearch(ax, epsilon, x0, lr):
    xs = [x0]
    x = x0
    it_times = 0
    while (np.linalg.norm(dot_f(x)) > epsilon):
        it_times += 1
        x = x - dot_f(x) * lr
        xs.append(x)

    xs = np.array(xs)
    n = 1000
    x = np.linspace(0, 2, n)
    y = np.linspace(0, 2, n)
    X, Y = np.meshgrid(x, y)
    ax.contourf(X, Y, fs(X, Y))
    ax.plot(xs[:, 0], xs[:, 1], color="white")
    ax.scatter(xs[:, 0], xs[:, 1], color="red")
    ax.set_title("exact")
    tt = "it_time: " + str(it_times)
    ax.text(0, 0, tt)


if __name__ == "__main__":
    x0 = np.array([0, 0])
    fig = plt.figure()
    ax1 = plt.subplot(1, 1, 1)
    exactlinearsearch(ax1, 1e-2, x0, 0.5)
    plt.show()
当我运行这个程序时,我得到了:

RuntimeWarning: overflow encountered in double_scalars
  dx = np.array([8* (x[0])**3+2*x[0]-8*x[0]*x[1]-2, 4*x[1]-4*(x[0])**2])