1

在 DL 教程中,我试图根据 将“输入”传递给 Theano 中的函数的目的/意义是什么来打印测试样本的概率? 但我收到以下错误。我需要添加一些 theano_flags 吗?

如何解决问题?

TypeError: 无法将 Type Generic (of Variable ) 转换为 Type TensorType(float64, matrix)。您可以尝试手动转换为 TensorType(float64, matrix)。

(我的数据的特征数=120,classes=2,test_set batch size=1)

部分代码是:

从theano导入pp

classifier = LogisticRegression(input=x, n_in=120, n_out=2)

print " Theano builds graphs for the expressions it computes before evaluating them:that is..."
print pp(classifier.p_y_given_x)

             .........................


     # test it on the test set

                test_losses = [test_model(i)
                               for i in xrange(n_test_batches)]
                test_score = numpy.mean(test_losses)


                values=theano.shared(value=test_set_x.get_value)
                f=theano.function([],classifier.p_y_given_x, 
                                  givens={x:values},on_unused_input='ignore')
                print f()  
4

1 回答 1

2

创建值时,您的代码中有错误。test_set_x.get_value 是一个 python 函数。所以它应该是 test_set_x.get_value() 你想要的值,而不是返回它的可调用对象。由于 theano.shared() 接收可调用的输入作为输入,它创建了一个不是张量的通用 Theano 变量。因此,当您尝试用泛型变量替换张量变量 x 时,会引发错误,因为它不是允许的替换。

但更好的是,您不需要创建新的共享变量,只需像这样编译函数:

            f=theano.function([],classifier.p_y_given_x, 
                              givens={x:test_set_x},on_unused_input='ignore')
于 2013-12-09T21:52:39.857 回答