gpt4 book ai didi

python - 从 metric_learning LMNN 算法中恢复变换矩阵

转载 作者:行者123 更新时间:2023-12-04 15:28:20 25 4
gpt4 key购买 nike

我正在使用 scikit-learn metric_learning ( http://contrib.scikit-learn.org/metric-learn/index.html ) 中的 LMNN 模块,我正在尝试恢复线性变换矩阵 (L.T) 来自学习的 Mahalanobis (M) 矩阵。

我尝试恢复此线性变换的原因是我使用云计算拟合我的数据集,但在本地机器上对其进行测试。这意味着我无法在云计算上拟合后保存或恢复 LMNN 模型,但我可以保存学习的 M 矩阵并使用分解来找到学习的线性变换。然后,我可以将学到的线性变换应用于我在本地机器上的测试集。

问题是我似乎无法将 LMNN 模块的内置转换结果与从分解的 M 矩阵中学习到的线性转换相协调。这是一个例子:

import numpy as np
from metric_learn import LMNN
from sklearn.datasets import load_iris
iris_data = load_iris()
X = iris_data['data']
Y = iris_data['target']

lmnn = LMNN(k=5, learn_rate=1e-6)
X_transformed = lmnn.fit_transform(X, Y)
M_matrix = lmnn.get_mahalanobis_matrix()
array([[ 2.47937397, 0.36313715, -0.41243858, -0.78715282],
[ 0.36313715, 1.69818843, -0.90042673, -0.0740197 ],
[-0.41243858, -0.90042673, 2.37024271, 2.18292864],
[-0.78715282, -0.0740197 , 2.18292864, 2.9531315 ]])

# cholesky decomp of M_matrix
eigvalues, eigcolvectors = np.linalg.eig(M_matrix)
eigvalues_diag = np.diag(eigvalues)
eigvalues_diag_sqrt = np.sqrt(eigvalues_diag)
L = eigcolvectors.dot(eigvalues_diag_sqrt.dot(np.linalg.inv(eigcolvectors)))
L_transpose = np.transpose(L)
L_transpose.dot(L) # check to confirm that matches M_matrix
array([[ 2.47937397, 0.36313715, -0.41243858, -0.78715282],
[ 0.36313715, 1.69818843, -0.90042673, -0.0740197 ],
[-0.41243858, -0.90042673, 2.37024271, 2.18292864],
[-0.78715282, -0.0740197 , 2.18292864, 2.9531315 ]])

# test fit_transform() vs. transform() using LMNN functions
lmnn.transform(X[0:4, :])
array([[8.2487 , 4.41337015, 0.14988465, 0.52629361],
[7.87314906, 3.77220291, 0.36015873, 0.525688 ],
[7.59410008, 4.03369392, 0.17339877, 0.51350962],
[7.41676205, 3.82012155, 0.47312948, 0.68515535]])

X_transformed[0:4, :]
array([[8.2487 , 4.41337015, 0.14988465, 0.52629361],
[7.87314906, 3.77220291, 0.36015873, 0.525688 ],
[7.59410008, 4.03369392, 0.17339877, 0.51350962],
[7.41676205, 3.82012155, 0.47312948, 0.68515535]])

# test manual transform of X[0:4, :]
X[0:4, :].dot(L_transpose)
array([[8.22608756, 4.45271327, 0.24690081, 0.51206068],
[7.85071271, 3.81054846, 0.45442718, 0.51144826],
[7.57310259, 4.06981377, 0.26240745, 0.50067674],
[7.39356544, 3.85511015, 0.55776916, 0.67615584]])

如上所示,原始数据集 X[0:4, :] 的前四行由 LMNN 模块转换(使用 fit_transform (X, Y)transform(X[0:4, :]) 给出与手动转换不同的结果。

class MetricTransformer(six.with_metaclass(ABCMeta)):

@abstractmethod
def transform(self, X):
"""Applies the metric transformation.
Parameters
----------
X : (n x d) matrix
Data to transform.
Returns
-------
transformed : (n x d) matrix
Input data transformed to the metric space by :math:`XL^{\\top}`

class MahalanobisMixin(six.with_metaclass(ABCMeta, BaseMetricLearner,
MetricTransformer)):
r"""Mahalanobis metric learning algorithms.
Algorithm that learns a Mahalanobis (pseudo) distance :math:`d_M(x, x')`,
defined between two column vectors :math:`x` and :math:`x'` by: :math:`d_M(x,
x') = \sqrt{(x-x')^T M (x-x')}`, where :math:`M` is a learned symmetric
positive semi-definite (PSD) matrix. The metric between points can then be
expressed as the euclidean distance between points embedded in a new space
through a linear transformation. Indeed, the above matrix can be decomposed
into the product of two transpose matrices (through SVD or Cholesky
decomposition): :math:`d_M(x, x')^2 = (x-x')^T M (x-x') = (x-x')^T L^T L
(x-x') = (L x - L x')^T (L x- L x')`
  • 我在这里缺少什么?

谢谢!

最佳答案

metric-learn 贡献者,@BeginnersMindTruly 你是对的,对于 LMNN,我们确实在训练期间直接学习了 L 矩阵,我们从中计算 M 最后,因此从 M 计算回转换 L 可能会导致数值差异。

至于直接访问学习矩阵 L 的特定用例,您应该能够在最后使用度量学习器的 components_ 属性来做到这一点培训。

关于python - 从 metric_learning LMNN 算法中恢复变换矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61807973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com