我制作了Baysien II型模型,以找到估计超参数“最可能”值的最大可能性。在我尝试运行下面的代码之后,我得到了compute_函数中显示的一些错误。可能是形状有问题
def compute_posterior(PHI, t, alph, s2):
M = PHI.shape[1]
beta = 1/s2
H = beta*(PHI.T @ PHI) + alph*np.eye(M)
SIGMA = np.linalg.inv(H)
Mu = beta * (SIGMA @ (PHI.T @ t))
#
return Mu, SIGMA
# Marginal log likelihood
#
# Version 1 Log Marginal (ideal)
#
def compute_log_marginal(PHI, t, alph, s2):
#
# Exploit the shape of C and the fact that M < N (usually)
#
N, M = PHI.shape
beta = 1 / s2
Mu, SIGMA = compute_posterior(PHI, t, alph, s2)
#
# Constant factor
#
logML = -N * np.log(2 * np.pi)
#
# log determinant factor (log|C|)
#
# If SIGMA becomes singular, sgn<0
#
sgn, logdet = np.linalg.slogdet(SIGMA)
#
if sgn < 0:
print("Error with alpha={0}, s2={1}".format(alph, s2))
raise np.linalg.LinAlgError("logdet sign is negative - something is wrong!")
#
logML += logdet + N*np.log(beta) + M*np.log(alph)
#
# data term (t'Ct)
#
logML -= beta * (t.T @ (t - PHI @ Mu))
#
logML = logML / 2.0
#
return logML
log_alph = np.linspace(-3, 6, n)
log_s2 = np.linspace(-4, 4, n)
x = df.values[:, 0:8]
t = df.values[:, 8]
n = 100
Z = np.empty((n, n))
Z_max = []
al = []
rr = []
for i, a in enumerate(log_alph):
for j, r in enumerate(log_s2):
Z[i, j] = compute_log_marginal(x,t,log_alph,log_s2)
Z_max.append(Z[i, j])
maximum = max(Z_max)
print(maximum)
# if Z[i, j] == -11.627510277032485:
# al.append(a)
# rr.append(r)
# maximum = max(Z_max)
# print(al)
# print(rr)
# print(maximum)
# maximum = -11.627510277032485
plt.contourf(log_a, log_r, Z.T)
print('The maximum point is:',maximum )
print('The max log_alpha is:',al[0] )
print('The max log_r is:',rr[0] )
plt.xlabel('log alpha')
plt.ylabel('log r')
plt.title('Contour of log alpha and log r')
在我编译之后,我得到了这个错误,我仍然不知道如何找出它
'
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-260-c7f0396b4046> in <module>
14 for i, a in enumerate(log_alph):
15 for j, r in enumerate(log_s2):
---> 16 Z[i, j] = compute_log_marginal(x,t,log_alph,log_s2)
17 Z_max.append(Z[i, j])
18 maximum = max(Z_max)
<ipython-input-233-45cfae272a38> in compute_log_marginal(PHI, t, alph, s2)
19 N, M = PHI.shape
20 beta = 1 / s2
---> 21 Mu, SIGMA = compute_posterior(PHI, t, alph, s2)
22 #
23 # Constant factor
<ipython-input-233-45cfae272a38> in compute_posterior(PHI, t, alph, s2)
2 M = PHI.shape[1]
3 beta = 1/s2
----> 4 H = beta*(PHI.T @ PHI) + alph*np.eye(M)
5 SIGMA = np.linalg.inv(H)
6 Mu = beta * (SIGMA @ (PHI.T @ t))
ValueError: operands could not be broadcast together with shapes (100,) (8,8)
'
拆分此行中的操作并找出哪个操作抛出错误 如果您的逻辑正常,请检查所有矩阵是否都具有您期望的形状。 如果形状合适,请注意@是矩阵乘法,*是元素乘法。 下一次发布代码时,请尝试包含可读性更强的较小部分,或者包含每个人都可以执行的代码,以对其进行调试
相关问题 更多 >
编程相关推荐