コード例 #1
0
ファイル: MLMNN.1.2.py プロジェクト: PiscesDream/Lab_MMAPM
    def fit(self, x, y, testx, testy, verbose=False, autosave=True):
        self.x = x = x.reshape(x.shape[0], self.M, -1)
        self.y = y
        self.testx = testx = testx.reshape(testx.shape[0], self.M, -1)
        self.testy = y

        self.Ms = []
        self.PCAs = []

        for i in xrange(self.M):
            trainx, testx, _pca = pca(self.x[:, i, :], self.testx[:, i, :], self.Ppca, verbose=verbose, get_pca=True)
            lmnn = LMNN_GPU(K=self.K, mu=self.mu, maxS=self.maxS, dim=trainx.shape[1])

            print '[%d]pre-train-acc: %.3f%% pre-test-acc: %.3f%% %s'%(i, knn(trainx, trainx, y, y, lmnn._M, self.K), knn(trainx, testx, y, testy, lmnn._M, self.K), ' '*30)
            lmnn.fit(trainx, y, lr=self.lr, max_iter=self.max_iter, reset=self.reset, verbose=False)
            print '[%d]post-train-acc: %.3f%% post-test-acc: %.3f%% %s'%(i, knn(trainx, trainx, y, y, lmnn._M, self.K), knn(trainx, testx, y, testy, lmnn._M, self.K), ' '*30)


            self.Ms.append(lmnn)
            self.PCAs.append(_pca)

        trainx = self.transform(self.x)
        testx = self.transform(self.testx)
        dim = trainx.shape[1]
        print 'train-acc: %.3f%% test-acc: %.3f%% %s'%(knn(trainx, trainx, y, y, np.eye(dim), self.K), knn(trainx, testx, y, testy, np.eye(dim), self.K), ' '*30)


        if autosave:
            print 'Auto saving ...'
            cPickle.dump(self, open('temp.MLMNN', 'w'))
        return self
コード例 #2
0
ファイル: MLMNN.1.1.py プロジェクト: PiscesDream/Lab_MMAPM
    def fit(self, x, y, testx, testy, verbose=False, autosave=True):
        self.x = x = x.reshape(x.shape[0], self.M, -1)
        self.y = y
        self.testx = testx = testx.reshape(testx.shape[0], self.M, -1)
        self.testy = y

        self.Ms = []
        self.PCAs = []

        for i in xrange(self.M):
            trainx, testx, _pca = pca(self.x[:, i, :], self.testx[:, i, :], self.Ppca, verbose=verbose, get_pca=True)
            lmnn = LMNN_GPU(K=self.K, mu=self.mu, maxS=self.maxS, dim=trainx.shape[1])

            print '[%d]pre-train-acc: %.3f%% pre-test-acc: %.3f%% %s'%(i, knn(trainx, trainx, y, y, lmnn._M, self.K), knn(trainx, testx, y, testy, lmnn._M, self.K), ' '*30)
            lmnn.fit(trainx, y, lr=self.lr, max_iter=self.max_iter, reset=self.reset, verbose=False)
            print '[%d]post-train-acc: %.3f%% post-test-acc: %.3f%% %s'%(i, knn(trainx, trainx, y, y, lmnn._M, self.K), knn(trainx, testx, y, testy, lmnn._M, self.K), ' '*30)


            self.Ms.append(lmnn)
            self.PCAs.append(_pca)

        trainx = self.transform(self.x)
        testx = self.transform(self.testx)

        trainx[np.isnan(trainx)] = 0
        trainx[np.isinf(trainx)] = 0

        testx[np.isnan(testx)] = 0
        testx[np.isinf(testx)] = 0
        print 'Final train shape:',trainx.shape
        print 'Final test shape:',testx.shape
        trainx, testx = pca(trainx, testx, self.Ppca, verbose=True)
        print 'Final train shape:',trainx.shape
        print 'Final test shape:',testx.shape
    
        # integrated metric
        lmnn = LMNN_GPU(K=self.K, mu=self.mu, maxS=10000, dim=trainx.shape[1])
        for i in xrange(10):
            print '[%d]pre-train-acc: %.3f%% pre-test-acc: %.3f%% %s'%(i, knn(trainx, trainx, y, y, lmnn._M, self.K), knn(trainx, testx, y, testy, lmnn._M, self.K), ' '*30)
            lmnn.fit(trainx, y, lr=2e-5, max_iter=50, reset=50, verbose=False)
            print '[%d]post-train-acc: %.3f%% post-test-acc: %.3f%% %s'%(i, knn(trainx, trainx, y, y, lmnn._M, self.K), knn(trainx, testx, y, testy, lmnn._M, self.K), ' '*30)

        if autosave:
            print 'Auto saving ...'
            cPickle.dump(self, open('temp.MLMNN', 'w'))
        return self