我试图在 gpflow 中训练一个 SVGP 模型,其中 X_train 的形状为 (140,1433),y_train 的形状为 (140,)。我从 dispatcher.py 得到一个 KeyError 但错误消息没有指定我的代码的哪一部分调用了该文件,我也无法弄清楚错误消息的其余部分是什么意思。我复制完整的相关代码和完整的错误消息:
import numpy as np
import gpflow
import tensorflow as tf
from gpflow.mean_functions import Constant
from gpflow.models import SVGP
from scipy.cluster.vq import kmeans2
from gpflow import Parameter
from gpflow.inducing_variables.inducing_variables import InducingPointsBase
from gpflow import covariances as cov
num_classes=7
node_feats = np.random.randn(2708, 1433)
node_labels = np.sum(node_feats, axis=1)
node_labels=num_classes*(node_labels-np.min(node_labels))/(np.max(node_labels)-np.min(node_labels))
node_labels=(num_classes-1)*np.floor(node_labels)
idx_train, idx_val, idx_test = np.array(range(140)),np.array(range(140,640)),np.array(range(1708,2708))
idx_train, idx_val, idx_test = tf.constant(idx_train), tf.constant(idx_val), tf.constant(idx_test)
X_train, y_train = node_feats[idx_train], node_labels[idx_train]
X_test, y_test = node_feats[idx_test], node_labels[idx_test]
def training_step(X_train, y_train, optimizer, gprocess):
with tf.GradientTape(watch_accessed_variables=False) as tape:
tape.watch(gprocess.trainable_variables)
data=(X_train, y_train)
objective = -gprocess.elbo(data)
gradients = tape.gradient(objective, gprocess.trainable_variables)
optimizer.apply_gradients(zip(gradients, gprocess.trainable_variables))
return objective
def evaluate(X_val, y_val, gprocess):
pred_y, pred_y_var = gprocess.predict_y(X_val)
pred_classes = np.argmax(pred_y.numpy(), axis=-1)
acc = np.mean(pred_classes == y_val)
return acc
def sparse_mat_to_sparse_tensor(sparse_mat):
"""
Converts a scipy csr_matrix to a tensorflow SparseTensor.
"""
coo = sparse_mat.tocoo()
indices = np.stack([coo.row, coo.col], axis=-1)
tensor = tf.sparse.SparseTensor(indices, sparse_mat.data, sparse_mat.shape)
return tensor
class GraphPolynomial(gpflow.kernels.base.Kernel):
"""
GraphPolynomial kernel for node classification as introduced in
Yin Chen Ng, Nicolo Colombo, Ricardo Silva: "Bayesian Semi-supervised
Learning with Graph Gaussian Processes".
"""
def __init__(self, sparse_adj_mat, feature_mat, degree=3.0, variance=1.0,
offset=1.0):
super().__init__([1])
self.degree = degree
self.offset = Parameter(offset, transform=gpflow.utilities.positive())
self.variance = Parameter(variance, transform=gpflow.utilities.positive())
# Pre-compute the P-matrix for transforming the base covariance matrix
# (c.f. paper for details).
sparse_adj_mat[np.diag_indices(sparse_adj_mat.shape[0])] = 1.0
self.sparse_P = sparse_mat_to_sparse_tensor(sparse_adj_mat)
self.sparse_P = self.sparse_P / sparse_adj_mat.sum(axis=1)
self.feature_mat = feature_mat
def K(self, X, Y=None, presliced=False):
X = tf.reshape(tf.cast(X, tf.int32), [-1])
X2 = tf.reshape(tf.cast(Y, tf.int32), [-1]) if Y is not None else X
base_cov = (self.variance * tf.matmul(self.feature_mat, self.feature_mat, transpose_b=True) + self.offset) ** self.degree
cov = tf.sparse.sparse_dense_matmul(self.sparse_P, base_cov)
cov = tf.sparse.sparse_dense_matmul(self.sparse_P, cov, adjoint_b=True)
cov = tf.gather(tf.gather(cov, X, axis=0), X2, axis=1)
# print(f"Kff: {cov.shape}")
return cov
def K_diag(self, X, presliced=False):
return tf.linalg.diag_part(self.K(X))
class NodeInducingPoints(InducingPointsBase):
"""
Set of real-valued inducing points. See parent-class for details.
"""
pass
# Init inducing points
inducing_points = kmeans2(node_feats, len_train, minit='points')[0] # use as many inducing points as training samples
#inducing_points = NodeInducingPoints(inducing_points)
# Init GP model
mean_function = Constant()
gprocess = SVGP(kernel, gpflow.likelihoods.MultiClass(num_classes),
inducing_points, mean_function=mean_function,
num_latent=num_classes, whiten=True, q_diag=False)
# Init optimizer
optimizer = tf.optimizers.Adam()
for epoch in range(200):
elbo = -training_step(X_train, y_train, optimizer, gprocess)
elbo = elbo.numpy()
acc = evaluate(X_test, y_test, gprocess)
print(f"{epoch}:\tELBO: {elbo:.5f}\tAcc: {acc:.3f}")
我收到以下错误消息:
KeyError Traceback (最近一次调用最后一次) ~\Anaconda3\envs\tf_gpf_env\lib\site-packages\multipledispatch\dispatcher.py in call (self, *args, **kwargs) 268 try: --> 269 func = self. _cache[types] 270 除了 KeyError:
键错误:(,,,)
在处理上述异常的过程中,又出现了一个异常:
NotImplementedError Traceback(最近一次调用最后一次)在 1 中的 epoch 在范围(200):----> 2 elbo = -training_step(X_train,y_train,优化器,gprocess)3 elbo = elbo.numpy() 4 5 acc = 评估(idx_test, node_labels[idx_test], gprocess)
在 training_step(X_train, y_train, optimizer, gprocess) 15 tape.watch(gprocess.trainable_variables) 16 data=(X_train, y_train) ---> 17 Objective = -gprocess.elbo(data) 18 19 #objective = -gprocess。 elbo(X_train, y_train)
c:\users\asus\downloads\gpflow-develop\gpflow-develop\gpflow\models\svgp.py in elbo(self, data) 152 这将返回对数边际似然的证据下限 (ELBO)。153 """ --> 154 return self.log_marginal_likelihood(data) 155 156 def predict_f(self, Xnew: tf.Tensor, full_cov=False, full_output_cov=False) -> tf.Tensor:
c:\users\asus\downloads\gpflow-develop\gpflow-develop\gpflow\models\model.py in log_marginal_likelihood(self, *args, **kwargs) 43 44 def log_marginal_likelihood(self, *args, **kwargs) -> tf.Tensor: ---> 45 return self.log_likelihood(*args, **kwargs) + self.log_prior() 46 47 def log_prior(self) -> tf.Tensor:
c:\users\asus\downloads\gpflow-develop\gpflow-develop\gpflow\models\svgp.py in log_likelihood(self, data) 138 X, Y = data 139 kl = self.prior_kl() --> 140 f_mean , f_var = self.predict_f(X, full_cov=False, full_output_cov=False) 141 var_exp = self.likelihood.variational_expectations(f_mean, f_var, Y) 142 如果 self.num_data 不是 None:
c:\users\asus\downloads\gpflow-develop\gpflow-develop\gpflow\models\svgp.py in predict_f(self, Xnew, full_cov, full_output_cov) 164 full_cov=full_cov, 165 white=self.whiten, --> 166 full_output_cov=full_output_cov) 167 # tf.debugging.assert_positive(var) # 我们真的应该让测试在这里通过 168 return mu + self.mean_function(Xnew), var
~\Anaconda3\envs\tf_gpf_env\lib\site-packages\multipledispatch\dispatcher.py in call (self, *args, **kwargs) 276 self._cache[types] = func 277 try: --> 278 return func( *args, **kwargs) 279 280 MDNotImplementedError 除外:
c:\users\asus\downloads\gpflow-develop\gpflow-develop\gpflow\conditionals\conditionals.py in _conditional(Xnew, inducing_variable, kernel, function, full_cov, full_output_cov, q_sqrt, white) 54 """ 55 Kmm = Kuu(inducing_variable, kernel, jitter=default_jitter()) # [M, M] ---> 56 Kmn = Kuf(inducing_variable, kernel, Xnew) # [M, N] 57 Knn = kernel(Xnew, full=full_cov) 58 fmean, fvar = base_conditional(Kmn,
~\Anaconda3\envs\tf_gpf_env\lib\site-packages\multipledispatch\dispatcher.py in call (self, *args, **kwargs) 273 raise NotImplementedError( 274 'Could not find signature for %s: <%s>' % --> 275 (self.name, str_signature(types))) 276 self._cache[types] = func 277 尝试:
NotImplementedError:找不到 Kuf 的签名: