【问题标题】:How to add a confusion matrix to Theano examples?如何在 Theano 示例中添加混淆矩阵?
【发布时间】:2015-08-04 08:30:09
【问题描述】:

我想使用 Theano 的逻辑回归分类器,但我想与我之前所做的研究进行苹果对苹果的比较,以了解深度学习是如何叠加的。我承认如果我更精通 Theano,这可能是一个相当简单的任务,但这是我目前所拥有的。从网站上的教程中,我有以下代码:

def errors(self, y):
    # check if y has same dimension of y_pred
    if y.ndim != self.y_pred.ndim:
        raise TypeError(
            'y should have the same shape as self.y_pred',
            ('y', y.type, 'y_pred', self.y_pred.type)
        )
    # check if y is of the correct datatype
    if y.dtype.startswith('int'):
        # the T.neq operator returns a vector of 0s and 1s, where 1
        # represents a mistake in prediction
        return T.mean(T.neq(self.y_pred, y))

我很确定这是我需要添加功能的地方,但我不确定如何去做。我需要的是在每次运行时访问 y_pred 和 y(以在 python 中更新我的混淆矩阵),或者让 C++ 代码处理混淆矩阵并在途中的某个时候返回它。我不认为我可以做前者,我不确定如何做后者。我已经按照以下方式对更新功能进行了一些处理:

def confuMat(self, y):
    x=T.vector('x')
    classes = T.scalar('n_classes')
    onehot = T.eq(x.dimshuffle(0,'x'),T.arange(classes).dimshuffle('x',0))
    oneHot = theano.function([x,classes],onehot)
    yMat = T.matrix('y')
    yPredMat = T.matrix('y_pred')
    confMat = T.dot(yMat.T,yPredMat)
    confusionMatrix = theano.function(inputs=[yMat,yPredMat],outputs=confMat)

    def confusion_matrix(x,y,n_class):
        return confusionMatrix(oneHot(x,n_class),oneHot(y,n_class))

    t = np.asarray(confusion_matrix(y,self.y_pred,self.n_out))
    print (t)

但我并不完全清楚如何让它与相关函数交互并给我一个我可以使用的 numpy 数组。 我对 Theano 很陌生,所以希望这对你们中的一个人来说是一个简单的解决方法。我想在许多配置中使用这个分类器作为我的输出层,所以我可以将混淆矩阵与其他架构一起使用。

【问题讨论】:

    标签: theano deep-learning confusion-matrix


    【解决方案1】:

    我建议使用蛮力之类的方法。您首先需要一个预测的输出。为它创建一个函数。

     prediction = theano.function(
            inputs = [index],
            outputs = MLPlayers.predicts,
            givens={
                    x: test_set_x[index * batch_size: (index + 1) * batch_size]})
    

    在您的测试循环中,收集预测...

    labels = labels + test_set_y.eval().tolist() 
    for mini_batch in xrange(n_test_batches):
        wrong = wrong + int(test_model(mini_batch))   
        predictions = predictions + prediction(mini_batch).tolist()
    

    现在以这种方式创建混淆矩阵:

        correct = 0
        confusion = numpy.zeros((outs,outs), dtype = int)
        for index in xrange(len(predictions)):
            if labels[index] is predictions[index]:
                correct = correct + 1
            confusion[int(predictions[index]),int(labels[index])] = confusion[int(predictions[index]),int(labels[index])] + 1
    

    你可以找到这种实现in this repository

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-28
      • 1970-01-01
      • 1970-01-01
      • 2014-07-09
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 2017-02-25
      相关资源
      最近更新 更多