我正在尝试使用 numpy 实现线性回归。为此,我需要为损失函数 mae 和 mse 实现导数。这些是我的版本,但它们是不正确的(不幸的是未公开的失败测试)。 有人可以帮助我为 mae 和 mse 实现正确的梯度方法吗?请注意,目标的维度 > 1,这可能在我的版本中未处理。
def mse_derivative(X, Y, w):
"""
X : numpy array of shape (`n_observations`, `n_features`)
Y : numpy array of shape (`n_observations`, `target_dimentionality`) or (`n_observations`,)
w : numpy array of shape (`n_features`, `target_dimentionality`) or (`n_features`,)
Return: NumPy array of the same shape as `w`
"""
return (X.T.dot(X.dot(w) - Y)) * 2 / Y.shape[0]
def mae_derivative(X, Y, w):
"""
X : numpy array of shape (`n_observations`, `n_features`)
Y : numpy array of shape (`n_observations`, `target_dimentionality`) or (`n_observations`,)
w : numpy array of shape (`n_features`, `target_dimentionality`) or (`n_features`,)
Return: NumPy array of the same shape as `w`
"""
return np.array(list(map(lambda diff : 0 if diff == 0 else 1 if diff > 0 else -1, list(X.dot(w) - Y))))
def mse_derivative(X, Y, w):
"""
X : numpy array of shape (`n_observations`, `n_features`)
Y : numpy array of shape (`n_observations`, `target_dimentionality`) or (`n_observations`,)
w : numpy array of shape (`n_features`, `target_dimentionality`) or (`n_features`,)
Return: NumPy array of the same shape as `w`
"""
result = (X.T.dot(X.dot(w) - Y)) * 2 / Y.shape[0]
return result if len(w.shape) == 1 else result / w.shape[1]