【问题标题】:Theano printing probabilities for test-set samples测试集样本的 Theano 打印概率
【发布时间】:2013-12-06 04:16:58
【问题描述】:

在 DL 教程中,我尝试根据What is the prupose/meaning of passing "input" to a function in Theano? 打印测试样本的概率 但我收到以下错误。我需要添加一些 theano_flags 吗?

如何解决问题?

TypeError: 无法将 Type Generic (of Variable ) 转换为 输入 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()  

【问题讨论】:

    标签: generics printing types probability theano


    【解决方案1】:

    当您创建值时,您的代码中有错误。 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')
    

    【讨论】:

    • 谢谢!!我在 LogisticRegression 和logistic_sgd 中尝试过,这终于奏效了!但是我想知道,当将此方法应用于 DBN 或 SdA 时,首先我指定一个 dbn 或 sda 作为分类器,然后从逻辑类调用 p_y_given_x 并定义为一个函数?因为 DBN/SdA 类只处理测试错误...?
    • ***当我尝试按如下方式打印 DBN.py 中 test_set 的概率时;通过添加(就在 test_DBN() 末尾的打印语句之前)classifier=dbn.logLayer( input=dbn.x,n_in=120,n_out=2) f=theano.function([],classifier.p_y_given_x, givens={dbn.x:test_set_x}, on_unused_input='ignore') print f() === ==它给了我一个错误“分类器=dbn.logLayer(input=dbn.x,n_in=120,n_out=2) TypeError: 'LogisticRegression' object is not callable”我需要在logistic_sgd中_def_p_y_given_x吗?
    猜你喜欢
    • 2016-02-07
    • 1970-01-01
    • 1970-01-01
    • 2014-03-31
    • 2010-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多