I'm currently working through the book Data Science from Scratch by Joel Grus, and I've run across a function that I don't really understand:
def safe(f):
def safe_f(*args, **kwargs):
try:
return f(*args, **kwargs)
except:
return float('inf')
return safe_f
The safe function is called within a Gradient Descent algorithm to remove infinity values.
def minimize_batch(target_fn, gradient_fn, theta_0, tolerance=0.000001):
step_sizes = [100, 10, 1, 0.1, 0.001, 0.0001, 0.00001]
theta = theta_0
target_fn = safe(target_fn)
value = target_fn(theta)
while True:
gradient = gradient_fn(theta)
next_thetas = [step(theta, gradient, -step_size) for step_size in step_sizes]
next_theta = min(next_thetas, key=target_fn)
next_value = target_fn(next_theta)
if abs(value - next_value) < tolerance:
return theta
else:
theta, value = next_theta, next_value
I get the gist of what safe is doing, but I don't understand how it's doing it. For example, how does safe evaluate target_fn if there are no inputs for target_fn? What is safe doing such that it knows how to remove infinity values?
Gradient Descent aside, would this safe function work on a crazy functions that was undefined at uncountably many places?