Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python gpflow model.elbo抛出KeyError和NotImplementedError_Python_Gpflow - Fatal编程技术网

Python gpflow model.elbo抛出KeyError和NotImplementedError

Python gpflow model.elbo抛出KeyError和NotImplementedError,python,gpflow,Python,Gpflow,我试图在gpflow中训练SVGP模型,其中X_序列具有形状(1401433),y_序列具有形状(140,)。我从dispatcher.py中得到了一个KeyError,但是错误消息没有指定我的代码的哪一部分调用了该文件,我也不知道错误消息的其余部分是什么意思。我复制完整的相关代码和完整的错误消息: import numpy as np import gpflow import tensorflow as tf from gpflow.mean_functions import Constant

我试图在gpflow中训练SVGP模型,其中X_序列具有形状(1401433),y_序列具有形状(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}")
我收到以下错误消息:

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回溯(最近的呼叫 最后) ~\Anaconda3\envs\tf\u gpf\u env\lib\site packages\multipledispatch\dispatcher.py 在通话中(self,*args,**kwargs) 268试试: -->269 func=self.\u缓存[类型] 270除键错误外:

KeyError:(,)

在处理上述异常期间,发生了另一个异常:

NotImplementedError回溯(最近的调用 最后)在 1表示范围内的历元(200): ---->2 elbo=-训练步骤(X\U训练、y\U训练、优化器、GPProcess) 3 elbo=elbo.numpy() 4. 5 acc=评估(idx_测试,节点_标签[idx_测试],gprocess)

在训练步骤中(X_训练,y_训练, 优化器(gprocess) 15磁带.手表(gprocess.可训练的_变量) 16数据=(X_列,y_列) --->17 objective=-gprocess.elbo(数据) 18 19#objective=-gprocess.elbo(X#train,y#train)

c:\users\asus\downloads\gpflow develop\gpflow develop\gpflow\models\svgp.py 在厄尔博(自我,数据) 152这将返回对数边际可能性的证据下限(ELBO)。 153 """ -->154返回自对数边际可能性(数据) 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 对数边际似然(self,*args,**kwargs) 43 44 def log_边际可能性(self,*args,**kwargs)->tf.张量: --->45返回self.log_似然(*args,**kwargs)+self.log_prior() 46 47 def log_prior(self)->tf.张量:

c:\users\asus\downloads\gpflow develop\gpflow develop\gpflow\models\svgp.py 日志中的可能性(自身、数据) 138 X,Y=数据 139 kl=自我优先权_kl() -->140 f_平均值,f_var=self.predict_f(X,full_cov=False,full_output_cov=False) 141 var_exp=自身可能性变分期望(f_均值,f_var,Y) 142如果self.num_数据不是无:

c:\users\asus\downloads\gpflow develop\gpflow develop\gpflow\models\svgp.py 在预测中(自我、新、完整、完整输出) 164全冠=全冠, 165白色=自白, -->166满输出值=满输出值) 167#tf.debug.assert#正(var)#我们真的应该让测试通过 168返回mu+自平均函数(Xnew),var

~\Anaconda3\envs\tf\u gpf\u env\lib\site packages\multipledispatch\dispatcher.py 在通话中(self,*args,**kwargs) 276 self.\u cache[types]=func 277尝试: -->278返回函数(*args,**kwargs) 279 280 MDNotImplementedError除外:

c:\users\asus\downloads\gpflow develop\gpflow develop\gpflow\conditionals\conditionals.py in_条件(Xnew,including_变量,内核,函数,full_cov, 全输出(cov、q、sqrt、白色) 54 """ 55 Kmm=Kuu(诱导_变量,内核,jitter=default_jitter())#[M,M] --->56 Kmn=Kuf(诱导变量,核,Xnew)#[M,N] 57 Knn=内核(Xnew,full=full_cov) 58 fmean,fvar=基本条件(Kmn

~\Anaconda3\envs\tf\u gpf\u env\lib\site packages\multipledispatch\dispatcher.py 在通话中(self,*args,**kwargs) 273升起未执行错误( 274'找不到%s的签名:'% -->275(姓名、str_签名(类型))) 276 self.\u cache[types]=func 277尝试:

NotImplementedError:找不到Kuf的签名:


请您将您的代码示例转换为一个示例,例如使用玩具数据(例如
Xtrain=np.random.randn(140,33)
)?否则,如果无法按原样运行您的代码,其他人将很难找出您到底出了什么问题。谢谢您,我以应该可以运行的方式对其进行了编辑(它仍然会给出错误)您好@enyhertbalo,我无法重现您的示例-运行您的代码片段会导致
回溯(最近一次调用最后一次):文件“so_7444.py”,第91行,在诱导_points=kmeans2(node_feats,len_train,minit='points')[0]#使用尽可能多的诱导点作为训练样本名称错误:名称“len_train”未定义
。请尝试将您的示例尽可能小,同时仍再现相同的问题。请将您的代码示例转换为一个示例,例如使用玩具数据(例如
Xtrain=np.random.randn(140,33)
)?否则,如果不能按原样运行您的代码,其他人将很难弄清楚您到底出了什么问题。谢谢,我以一种应该可以运行的方式编辑了它(它仍然会出错)。嗨@enyhertbalo,我无法重现您的示例-运行您的代码片段会导致
回溯(最近一次调用):文件“so_7444.py”,第91行,在诱导点=kmeans2(节点专长,len_train,minit='points')[0]#使用尽可能多的诱导点作为训练样本名称错误:名称“len_train”未定义
。请尝试将示例尽可能小,同时仍再现相同的问题。