Theano中的类型匹配错误 [无法将类型Generic(变量)转换为类型TensorType]

thisfile.py

import cPickleimport gzipimport osimport numpyimport theanoimport theano.tensor as Tdef load_data(dataset):    f = gzip.open(dataset, 'rb')    train_set, valid_set, test_set = cPickle.load(f)    f.close()    def shared_dataset(data_xy, borrow=True):        data_x, data_y = data_xy        shared_x = theano.shared(numpy.asarray(data_x,                                               dtype=theano.config.floatX),                                 borrow=borrow)        shared_y = theano.shared(numpy.asarray(data_y,                                               dtype=theano.config.floatX),                                 borrow=borrow)        return shared_x, T.cast(shared_y, 'int32')    test_set_x, test_set_y = shared_dataset(test_set)    valid_set_x, valid_set_y = shared_dataset(valid_set)    train_set_x, train_set_y = shared_dataset(train_set)    rval = [(train_set_x, train_set_y), (valid_set_x, valid_set_y),            (test_set_x, test_set_y)]    return rvalclass PCA(object):    def __init__(self):        self.param = 0           def dimemsion_transform(self, X):        m_mean = T.mean(X, axis=0)        X = X - m_mean          ##################### this line makes error        return Xif __name__ == '__main__':    dataset = 'mnist.pkl.gz'    # load the MNIST data    data = load_data(dataset)    X = T.matrix('X')    m_pca = PCA()    transform = theano.function(        inputs=[],        outputs=m_pca.dimemsion_transform(X),        givens={            X: data        }    )

错误显示如下

Traceback (most recent call last):  File ".../thisfile.py", line 101, in <module>    X: data  File ".../Theano/theano/compile/function.py", line 322, in function    output_keys=output_keys)  File ".../Theano/theano/compile/pfunc.py", line 443, in pfunc    no_default_updates=no_default_updates)  File ".../Theano/theano/compile/pfunc.py", line 219, in rebuild_collect_shared    cloned_v = clone_v_get_shared_updates(v, copy_inputs_over)  File ".../Theano/theano/compile/pfunc.py", line 93, in clone_v_get_shared_updates    clone_v_get_shared_updates(i, copy_inputs_over)  File ".../Theano/theano/compile/pfunc.py", line 93, in clone_v_get_shared_updates    clone_v_get_shared_updates(i, copy_inputs_over)  File ".../Theano/theano/compile/pfunc.py", line 93, in clone_v_get_shared_updates    clone_v_get_shared_updates(i, copy_inputs_over)  File ".../Theano/theano/compile/pfunc.py", line 96, in clone_v_get_shared_updates    [clone_d[i] for i in owner.inputs], strict=rebuild_strict)  File ".../Theano/theano/gof/graph.py", line 242, in clone_with_new_inputs    new_inputs[i] = curr.type.filter_variable(new)  File ".../Theano/theano/tensor/type.py", line 234, in filter_variable    self=self))TypeError: Cannot convert Type Generic (of Variable <Generic>) into Type TensorType(float64, matrix). You can try to manually convert <Generic> into a TensorType(float64, matrix).

我在使用Theano创建PCA函数时遇到了问题。在PCA类中的dimension_transform方法中,从MNIST数据中减去了均值

我不明白为什么会出现类型匹配错误,以及如何修复它


回答:

你的问题出在这些行:

data = load_data(dataset)

这里data是一个列表(因为load_data()返回的是列表)。

transform = theano.function(    inputs=[],    outputs=m_pca.dimemsion_transform(X),    givens={        X: data    })

而在这里你将其作为一个值传递。你需要从load_data()的返回值中提取你想要的项目,像这样:

[(train_set_x, train_set_y), (valid_set_x, valid_set_y),        (test_set_x, test_set_y)] = load_data(dataset)

然后使用

    givens={        X: train_set_x    }

或其他值之一。

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注