0

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 にまったく慣れていないので、これがあなたにとって簡単な修正になることを願っています。この分類子を多くの構成で出力層として使用したいので、混同行列を他のアーキテクチャで使用できます。

4

1 に答える 1

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

この種の実装は、このリポジトリにあります。

于 2015-08-04T00:30:09.050 に答える