Overflow in python

Asked 2 years ago, Updated 2 years ago, 29 views

Overflow error.
It seems to be happening in deriv while updating x in GD, but I don't know the solution.Thank you for your cooperation.

OverflowError: (34, 'Result too large')

#y=x^4-x^3
def function(x):
    return x**4-2*(x**3)+1

# minimum: y=-11/16, x=3/2
defderiv(x):
    return4*(x**3) - 6*(x**2)


def GD(deriv, init, eta,iter=100):
    '''
    —param deriv:derivative of the function you want to optimize
    —param init:start point, initial value
    —parameta:leaning rate, step size
    —return:history
    '''
    eps = 1e-5
    x = init
    x_history = [init]
    for i in range (iter):
        x_=x-eta*deriv(x)
        ifabs(x-x_)<eps:#convergence condition
            break
        x = x_
        x_history.append(x)
    return np.array(x_history)


etas = [0.1, 0.2, 0.4, 0.5]
'''
We check the behavior of Gradient Descent combined with the different eta
'''

for i, etain enumerate (etas):
    tracks = GD (deriv, 2.0, eta)

    plt.subplot(2,2,(i+1))

    plt.title("eta:"+str(eta)+", iter:"+str(len(tracks)))))

    x = np.range (-0.3, 1.0, 0.01)
    y = function(x)
    plt.plot(x,y,linestyle="-",c="black")
    plt.plot(tracks, function(tracks), 'x', c="r")

#plt.tight_layout()
plt.show()

python

2022-09-30 21:35

1 Answers

First, OverflowError is sent when you try to handle a very large value.

Sent when the result of an arithmetic operation becomes a large value that cannot be expressed.
https://docs.python.jp/3/library/exceptions.html#OverflowError

As for the solution, I don't know what I want to do from the questionnaire, so I will write an answer that I can understand from the code.

The following is the result of removing unnecessary np (is it numpy?) and inserting print(x) before returning the deriv function:

 2.0
1.2
1.3728000000000002
1.4686874222592001
1.4957044497076786
1.4995483345721634
1.4999545886920822
1.4999954563946214
2.0
0.3999999999999999
0.5407999999999998
0.7652256661503998
1.109435299670023
1.4940155127489982
1.5047018074535845
1.4961854140988777
1.5030167905684966
1.497564703119691
1.5019340154484542
1.4984438048562072
1.5012391469460173
1.4990049957566633
1.5007936281024734
1.499363585488775
1.5005081597589578
1.49959285234464
1.5003253203321703
1.4997394897067555
1.5002082453712682
1.4998332996170376
1.5001332936164338
1.4998933224637065
1.5000853147177737
1.4999317307566415
1.5000546042092862
1.4999563094765538
1.500034947837555
1.4999720387986786
1.5000223670846855
1.4999821051315547
1.500014315126218
1.4999885474072088
1.5000091617594458
1.4999926703909918
1.5000058635582716
1.4999953090708673
2.0
-1.2000000000000002
5.020800000000003
-136.9853594222595
4157744.941243001
-1.1499881294113451e+20
2.4333246462810442e+60
-2.30526123625319e+181

From around here, we can see that the value of x in the for loop of GD is very large in the middle of the loop.


2022-09-30 21:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.