各种特殊损失函数

死区损失函数

点击查看代码
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()
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值