I'm trying to use try/except to take care of a divide by zero error, but the except doesn't seem to be catching the error and I can't figure out why.
import numpy as np
A = np.array([[0.5,0,0.25,0.125,0.05],[0.5,0.5,0.5,0.5,0.5]])
Y = np.array([[0,1,0,1,1],[0,0,0,0,1]])
p = np.shape(A)[0]
q = np.shape(A)[1]
m = 2
try:
dA = (-1 / m) * ((Y - A) / (A - np.square(A))) * (A * (1 - A))
except Exception:
dA = np.zeros((p,q))
It returns a divide by zero error and an invalid value encountered in multiply (because of the divide by zero error), but it kills the program instead of doing what's in except.
RuntimeWarning: divide by zero encountered in true_divide
dA = (-1 / m) * ((Y - A) / (A - np.square(A))) * (A * (1 - A))
RuntimeWarning: invalid value encountered in multiply
dA = (-1 / m) * ((Y - A) / (A - np.square(A))) * (A * (1 - A))
I tried specifying the error instead of just Exception, but that doesn't work either. What else could be the problem?