`ValueError`

问题描述 投票:0回答:1
我正在使用平方指数核在 Python 中实现高斯过程回归 (GPR) 模型。但是,我在

ValueError

 方法的矩阵乘法步骤中遇到 
predict
,特别是在尝试计算平均预测时。

我看到的错误是:

ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 10 is different from 100)
代码详情

以下是此错误中起作用的代码的细分:

import numpy as np class SquaredExponentialKernel: def __init__(self, length_scale=1.0, variance=1.0): self.length_scale = length_scale self.variance = variance def __call__(self, x1, x2): dist_sq = np.sum((x1 - x2)**2) return self.variance * np.exp(-0.5 * dist_sq / self.length_scale**2) def cov_matrix(x1, x2, cov_function) -> np.array: return np.array([[cov_function(a, b) for a in x1] for b in x2]) class GPR: def __init__(self, data_x, data_y, covariance_function=SquaredExponentialKernel(), white_noise_sigma: float = 0): self.noise = white_noise_sigma self.data_x = data_x self.data_y = data_y self.covariance_function = covariance_function self._inverse_of_covariance_matrix_of_input_noise_adj = np.linalg.inv( cov_matrix(data_x, data_x, covariance_function) + self.noise * np.identity(len(self.data_x)) ) self._memory = None def predict(self, test_data: np.ndarray) -> np.ndarray: KXX_star = cov_matrix(test_data, self.data_x, self.covariance_function) KX_starX_star = cov_matrix(test_data, test_data, self.covariance_function) mean_test_data = KXX_star @ (self._inverse_of_covariance_matrix_of_input_noise_adj @ self.data_y) cov_test_data = KX_starX_star - KXX_star @ (self._inverse_of_covariance_matrix_of_input_noise_adj @ KXX_star.T) var_test_data = np.diag(cov_test_data) self._memory = {'mean': mean_test_data, 'covariance_matrix': cov_test_data, 'variance': var_test_data} return mean_test_data # Test data np.random.seed(69) data_x = np.linspace(-5, 5, 10).reshape(-1, 1) data_y = np.sin(data_x) + 0.1 * np.random.randn(10, 1) # Instantiate and predict gpr_se = GPR(data_x, data_y, covariance_function=SquaredExponentialKernel(), white_noise_sigma=0.1) test_data = np.linspace(-6, 6, 100).reshape(-1, 1) mean_predictions = gpr_se.predict(test_data)
维度细分

以下是出现错误的矩阵乘法的维数分析:

  1. KXX_star
     计算为 
    cov_matrix(test_data, self.data_x, self.covariance_function)
    ,得到 
    (100, 10)
     的形状。
  2. self._inverse_of_covariance_matrix_of_input_noise_adj
    __init__
     方法中计算,形状为 
    (10, 10)
  3. self.data_y
     的形状为 
    (10, 1)
有问题的行是:

mean_test_data = KXX_star @ (self._inverse_of_covariance_matrix_of_input_noise_adj @ self.data_y)
这应该会产生形状为 

(100, 1)

 的结果,因为:

  • KXX_star
     具有形状 
    (100, 10)
    ,
  • (self._inverse_of_covariance_matrix_of_input_noise_adj @ self.data_y)
     产生形状 
    (10, 1)

问题:当维度似乎与矩阵乘法对齐时,为什么会出现维度不匹配错误?我该如何解决它?

感谢您的任何见解或建议! 〜最大

我预计这个矩阵乘法能够发挥作用,因为维度在纸上看起来兼容:

KXX_star

 (100, 10) 乘以 
(10, 1)
 应该产生 
(100, 1)
。然而,该错误表明尺寸不匹配,这意味着某些内容未按预期对齐。我检查了 
self.data_y
self._inverse_of_covariance_matrix_of_input_noise_adj
KXX_star
 的形状。还尝试重塑 
data_y
 以确保其一致 (10, 1),但错误仍然存在。我期望得到 
(100, 1)
 形状向量的平均预测,而不存在任何维度问题。
	

这会重现您的错误消息
python numpy machine-learning matrix-multiplication gaussian-process
1个回答
0
投票
test_data

这表明
In [4]: a = np.ones((10,100)); b = np.ones((10,1)); a@b
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[4], line 1
----> 1 a = np.ones((10,100)); b = np.ones((10,1)); a@b

ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0,
with gufunc signature (n?,k),(k,m?)->(n?,m?) 
(size 10 is different from 100)
是你的想法的转置!

在你的问题中,并不总是清楚哪些形状得到了验证,哪些只是“愿望”。

© www.soinside.com 2019 - 2024. All rights reserved.