Beispiel #1
0
    def testObjectiveApprox(self): 
        """
        We'll test the case in which we apprormate using a large number of samples 
        for the AUC and see if we get close to the exact objective 
        """
        m = 20 
        n = 30 
        k = 3 
        X = SparseUtils.generateSparseBinaryMatrix((m, n), k, csarray=True)
        
        learner = MaxAUCSquare(k)
        learner.normalise = False
        learner.lmbdaU = 0
        learner.lmbdaV = 0
        learner.rho = 1.0
        learner.numAucSamples = n
        
        indPtr, colInds = SparseUtils.getOmegaListPtr(X)

        U = numpy.random.rand(X.shape[0], k)
        V = numpy.random.rand(X.shape[1], k)
        
        numRuns = 100 
        numTests = 5
        
        gi = numpy.random.rand(m)
        gi /= gi.sum()        
        gp = numpy.random.rand(n)
        gp /= gp.sum()        
        gq = numpy.random.rand(n)
        gq /= gq.sum()
        #gi = numpy.ones(m)
        #gp = numpy.ones(n)
        #gq = numpy.ones(n)

        #Let's compare against using the exact derivative 
        for i in range(numTests): 
            obj = 0

            for j in range(numRuns): 
                obj += learner.objectiveApprox(indPtr, colInds, indPtr, colInds, U, V, gp, gq)
            obj /= numRuns

            obj2 = learner.objective(indPtr, colInds, indPtr, colInds, U, V, gp, gq)    
            self.assertAlmostEquals(obj, obj2, 2)
            
        learner.rho = 0.2

        for i in range(numTests): 
            obj = 0
            for j in range(numRuns): 
                obj += learner.objectiveApprox(indPtr, colInds, indPtr, colInds, U, V, gp, gq)
            obj /= numRuns
            
            obj2 = learner.objective(indPtr, colInds, indPtr, colInds, U, V, gp, gq)    
            self.assertAlmostEquals(obj, obj2, 2)

        learner.lmbdaV = 0.2

        for i in range(numTests): 
            obj = 0
            for j in range(numRuns): 
                obj += learner.objectiveApprox(indPtr, colInds, indPtr, colInds, U, V, gp, gq)
            obj /= numRuns
            
            obj2 = learner.objective(indPtr, colInds, indPtr, colInds, U, V, gp, gq)    
            self.assertAlmostEquals(obj, obj2, 2)
        
        #Check full and summary versions are the same 
        obj = learner.objective(indPtr, colInds, indPtr, colInds, U, V, gp, gq) 
        obj2 = learner.objective(indPtr, colInds, indPtr, colInds, U, V, gp, gq) 
        self.assertAlmostEquals(obj, obj2, 2)
Beispiel #2
0
    def testObjectiveApprox(self):
        """
        We'll test the case in which we apprormate using a large number of samples 
        for the AUC and see if we get close to the exact objective 
        """
        m = 20
        n = 30
        k = 3
        X = SparseUtils.generateSparseBinaryMatrix((m, n), k, csarray=True)

        learner = MaxAUCSquare(k)
        learner.normalise = False
        learner.lmbdaU = 0
        learner.lmbdaV = 0
        learner.rho = 1.0
        learner.numAucSamples = n

        indPtr, colInds = SparseUtils.getOmegaListPtr(X)

        U = numpy.random.rand(X.shape[0], k)
        V = numpy.random.rand(X.shape[1], k)

        numRuns = 100
        numTests = 5

        gi = numpy.random.rand(m)
        gi /= gi.sum()
        gp = numpy.random.rand(n)
        gp /= gp.sum()
        gq = numpy.random.rand(n)
        gq /= gq.sum()
        #gi = numpy.ones(m)
        #gp = numpy.ones(n)
        #gq = numpy.ones(n)

        #Let's compare against using the exact derivative
        for i in range(numTests):
            obj = 0

            for j in range(numRuns):
                obj += learner.objectiveApprox(indPtr, colInds, indPtr,
                                               colInds, U, V, gp, gq)
            obj /= numRuns

            obj2 = learner.objective(indPtr, colInds, indPtr, colInds, U, V,
                                     gp, gq)
            self.assertAlmostEquals(obj, obj2, 2)

        learner.rho = 0.2

        for i in range(numTests):
            obj = 0
            for j in range(numRuns):
                obj += learner.objectiveApprox(indPtr, colInds, indPtr,
                                               colInds, U, V, gp, gq)
            obj /= numRuns

            obj2 = learner.objective(indPtr, colInds, indPtr, colInds, U, V,
                                     gp, gq)
            self.assertAlmostEquals(obj, obj2, 2)

        learner.lmbdaV = 0.2

        for i in range(numTests):
            obj = 0
            for j in range(numRuns):
                obj += learner.objectiveApprox(indPtr, colInds, indPtr,
                                               colInds, U, V, gp, gq)
            obj /= numRuns

            obj2 = learner.objective(indPtr, colInds, indPtr, colInds, U, V,
                                     gp, gq)
            self.assertAlmostEquals(obj, obj2, 2)

        #Check full and summary versions are the same
        obj = learner.objective(indPtr, colInds, indPtr, colInds, U, V, gp, gq)
        obj2 = learner.objective(indPtr, colInds, indPtr, colInds, U, V, gp,
                                 gq)
        self.assertAlmostEquals(obj, obj2, 2)