Esempio n. 1
0
    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
Esempio n. 2
0
    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
Esempio n. 3
0
        variance += ele
        if variance > 0.95: break
    components += 1
    print 'n_components=%d'%components
    pca.set_params(n_components=components)
    pca.fit(trainx)

    trainx = pca.transform(trainx)
    testx = pca.transform(testx)

    K = 9 
    dim = components
#   knn(trainx, testx, trainy, testy, np.eye(dim), K)
#   knn(trainx, testx, trainy, testy, np.random.rand(dim, dim), K)

    # My LMNN
    lmnn = LMNN_GPU(K=K, mu=0.5, maxS=10000, dim=dim)
    print 'pre-train-acc: %.3f%% pre-test-acc: %.3f%% %s'%(knn(trainx, trainx, trainy, trainy, lmnn._M, K), knn(trainx, testx, trainy, testy, lmnn._M, K), ' '*30)
    lmnn.fit(trainx, trainy, lr=5e-6, max_iter=1000, reset=50, verbose=True)
    print 'pre-train-acc: %.3f%% pre-test-acc: %.3f%% %s'%(knn(trainx, trainx, trainy, trainy, lmnn._M, K), knn(trainx, testx, trainy, testy, lmnn._M, K), ' '*30)
    #knn(trainx, testx, trainy, testy, lmnn.M, K)
#   L = lmnn.L
#   knn(trainx.dot(L), testx.dot(L), trainy, testy, np.eye(L.shape[1]), K)


#   # metric learn
#   lmnn = _LMNN(k=K, learn_rate=1e-5, max_iter=200) 
#   L = lmnn.fit(trainx, trainy, verbose=True).L
#   knn(trainx, testx, trainy, testy, L.dot(L), K)