死区损失函数
点击查看代码
import numpy as np
import matplotlib.pyplot as plt
# Define the parameters
a = 2
b = 5
epsilon = 0.1
# Define the loss function L(x) and its derivative
def L(x, a, b, epsilon):
if x < a:
return (x - a)**2 / (2 * epsilon)
elif x > b:
return (x - b)**2 / (2 * epsilon)
else:
return 0
def dL_dx(x, a, b, epsilon):
if x < a:
return (x - a) / epsilon
elif x > b:
return (x - b) / epsilon
else:
return 0
# Generate x values
x_values = np.linspace(0, 7, 500)
# Compute L(x) and its derivative for all x values
y_values = np.array([L(x, a, b, epsilon) for x in x_values])
dy_values = np.array([dL_dx(x, a, b, epsilon) for x in x_values])
# Plot the function and its derivative
plt.figure(figsize=(10, 6))
plt.subplot(2, 1, 1)
plt.plot(x_values, y_values, label=f'L(x) with a={a}, b={b}, epsilon={epsilon}')
plt.axvline(a, color='red', linestyle='--', label='a')
plt.axvline(b, color='green', linestyle='--', label='b')
plt.xlabel('x')
plt.ylabel('L(x)')
plt.title('Loss Function L(x)')
plt.legend()
plt.grid(True)
plt.subplot(2, 1, 2)
plt.plot(x_values, dy_values, label=f'dL/dx with a={a}, b={b}, epsilon={epsilon}', color='orange')
plt.axvline(a, color='red', linestyle='--', label='a')
plt.axvline(b, color='green', linestyle='--', label='b')
plt.xlabel('x')
plt.ylabel("L'(x)")
plt.title('Derivative of Loss Function L(x)')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
max 损失函数
点击查看代码
import numpy as np
import matplotlib.pyplot as plt
# Define the parameters
k = 10
a = lambda x: x - 2
b = lambda x: -x + 5
c = lambda x: np.sin(x)
d = lambda x: 0.5 * x
# Define the smoothed max function and its derivative
def smooth_max(x, k):
exp_terms = np.exp(k * np.array([a(x), b(x), c(x), d(x)]))
return (1/k) * np.log(np.sum(exp_terms))
def dsmooth_max_dx(x, k):
exp_terms = np.exp(k * np.array([a(x), b(x), c(x), d(x)]))
exp_sum = np.sum(exp_terms)
da_dx = 1
db_dx = -1
dc_dx = np.cos(x)
dd_dx = 0.5
derivs = np.array([da_dx, db_dx, dc_dx, dd_dx])
return np.sum((exp_terms / exp_sum) * derivs)
# Generate x values
x_values = np.linspace(0, 7, 500)
# Compute smooth_max(x) and its derivative for all x values
y_values = np.array([smooth_max(x, k) for x in x_values])
dy_values = np.array([dsmooth_max_dx(x, k) for x in x_values])
# Plot the function and its derivative
plt.figure(figsize=(10, 6))
plt.subplot(2, 1, 1)
plt.plot(x_values, y_values, label=f'smooth_max(x) with k={k}')
plt.xlabel('x')
plt.ylabel('smooth_max(x)')
plt.title('Smoothed Max Function')
plt.legend()
plt.grid(True)
plt.subplot(2, 1, 2)
plt.plot(x_values, dy_values, label=f'd(smooth_max)/dx with k={k}', color='orange')
plt.xlabel('x')
plt.ylabel("d(smooth_max)/dx")
plt.title('Derivative of Smoothed Max Function')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()