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 testDerivativeUiApprox(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 derivative 
        """
        m = 20 
        n = 30 
        k = 3 
        X = SparseUtils.generateSparseBinaryMatrix((m, n), k, csarray=True)
        
        w = 0.1
        learner = MaxAUCSquare(k, w)
        learner.normalise = False
        learner.lmbdaU = 0
        learner.lmbdaV = 0
        learner.rho = 1.0
        learner.numAucSamples = 10

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

        gp = numpy.random.rand(n)
        gp /= gp.sum()        
        gq = numpy.random.rand(n)
        gq /= gq.sum()     

        
        numRuns = 200 
        numTests = 5
        
        indPtr, colInds = SparseUtils.getOmegaListPtr(X)
        permutedRowInds = numpy.arange(m, dtype=numpy.uint32)
        permutedColInds = numpy.arange(n, dtype=numpy.uint32)

        #Test with small number of AUC samples, but normalise 
        learner.numAucSamples = 50
        numRuns = 200
        
        for i in numpy.random.permutation(m)[0:numTests]:  
            U = numpy.random.rand(X.shape[0], k)
            V = numpy.random.rand(X.shape[1], k)    
            
            
            
            du1 = numpy.zeros(k)
            for j in range(numRuns): 
                VDot, VDotDot, WDot, WDotDot = learner.computeMeansVW(indPtr, colInds, U, V, permutedRowInds, permutedColInds, gp, gq)
                du1 += learner.derivativeUiApprox(U, V, VDot, VDotDot, WDot, WDotDot, i)
            du1 /= numRuns
            du2 = learner.derivativeUi(indPtr, colInds, U, V, gp, gq, i) 
            #print(du1, du2)
            print(du1/numpy.linalg.norm(du1), du2/numpy.linalg.norm(du2))
            #print(numpy.linalg.norm(du1 - du2)/numpy.linalg.norm(du1))
            nptst.assert_array_almost_equal(du1, du2, 2)

        #Let's compare against using the exact derivative 
        for i in numpy.random.permutation(m)[0:numTests]:  
            U = numpy.random.rand(X.shape[0], k)
            V = numpy.random.rand(X.shape[1], k)            
            
            du1 = numpy.zeros(k)
            for j in range(numRuns): 
                VDot, VDotDot, WDot, WDotDot = learner.computeMeansVW(indPtr, colInds, U, V, permutedRowInds, permutedColInds, gp, gq)
                du1 += learner.derivativeUiApprox(U, V, VDot, VDotDot, WDot, WDotDot, i)
            du1 /= numRuns
            du2 = learner.derivativeUi(indPtr, colInds, U, V, gp, gq, i)   
            
            print(du1/numpy.linalg.norm(du1), du2/numpy.linalg.norm(du2))
            nptst.assert_array_almost_equal(du1, du2, 2)
            
            
        learner.lmbdaV = 0.5 
        
        for i in numpy.random.permutation(m)[0:numTests]:  
            U = numpy.random.rand(X.shape[0], k)
            V = numpy.random.rand(X.shape[1], k)            
            
            du1 = numpy.zeros(k)
            for j in range(numRuns): 
                VDot, VDotDot, WDot, WDotDot = learner.computeMeansVW(indPtr, colInds, U, V, permutedRowInds, permutedColInds, gp, gq)
                du1 += learner.derivativeUiApprox(U, V, VDot, VDotDot, WDot, WDotDot, i)
            du1 /= numRuns
            du2 = learner.derivativeUi(indPtr, colInds, U, V, gp, gq, i)   
            nptst.assert_array_almost_equal(du1, du2, 2)
            print(du1/numpy.linalg.norm(du1), du2/numpy.linalg.norm(du2))
Beispiel #3
0
    def testDerivativeV(self): 
        m = 10 
        n = 20 
        nnzPerRow = 5 
        X = SparseUtils.generateSparseBinaryMatrix((m, n), nnzPerRow, csarray=True)
        
        for i in range(m):
            X[i, 0] = 1
            X[i, 1] = 0
        
        k = 5
        u = 0.1
        w = 1-u
        eps = 0.05
        learner = MaxAUCSquare(k, w)
        learner.normalise = False
        learner.lmbdaU = 0
        learner.lmbdaV = 0
        learner.rho = 1.0
        learner.numAucSamples = 100

        numRuns = 20
        indPtr, colInds = SparseUtils.getOmegaListPtr(X)
            
        gp = numpy.random.rand(n)
        gp /= gp.sum()        
        gq = numpy.random.rand(n)
        gq /= gq.sum()            
        
        for s in range(numRuns):
            U = numpy.random.randn(m, k)
            V = numpy.random.randn(n, k)            
            
            deltaV = numpy.zeros(V.shape)
            for j in range(n): 
                deltaV[j, :] = learner.derivativeVi(indPtr, colInds, U, V, gp, gq, j)   
            
            deltaV2 = numpy.zeros(V.shape)    
            
            eps = 0.00001        
            
            for i in range(n): 
                for j in range(k):
                    tempV = V.copy() 
                    tempV[i,j] += eps
                    obj1 = learner.objective(indPtr, colInds, indPtr, colInds, U, tempV, gp, gq)
                    
                    tempV = V.copy() 
                    tempV[i,j] -= eps
                    obj2 = learner.objective(indPtr, colInds, indPtr, colInds, U, tempV, gp, gq)
                    
                    deltaV2[i,j] = (obj1-obj2)/(2*eps)
                #deltaV2[i,:] = deltaV2[i,:]/numpy.linalg.norm(deltaV2[i,:])                   
                        

            nptst.assert_almost_equal(deltaV, deltaV2, 3)

        #Try r != 0 and rho > 0
        for s in range(numRuns):
            U = numpy.random.randn(m, k)
            V = numpy.random.randn(n, k)   
            learner.rho = 1.0    
            
            deltaV = numpy.zeros(V.shape)
            for j in range(n): 
                deltaV[j, :] = learner.derivativeVi(indPtr, colInds, U, V, gp, gq, j)    
            
            deltaV2 = numpy.zeros(V.shape)
            
            for i in range(n): 
                for j in range(k):
                    tempV = V.copy() 
                    tempV[i,j] += eps
                    obj1 = learner.objective(indPtr, colInds, indPtr, colInds, U, tempV, gp, gq)
                    
                    tempV = V.copy() 
                    tempV[i,j] -= eps
                    obj2 = learner.objective(indPtr, colInds, indPtr, colInds, U, tempV, gp, gq)
                    
                    deltaV2[i,j] = (obj1-obj2)/(2*eps)
                #deltaV2[i,:] = deltaV2[i,:]/numpy.linalg.norm(deltaV2[i,:])
                           
            nptst.assert_almost_equal(deltaV, deltaV2, 3)
        
        
        #Try r != 0 and rho > 0
        for s in range(numRuns):
            U = numpy.random.randn(m, k)
            V = numpy.random.randn(n, k)              
            
            learner.lmbdaV = 100    
            
            deltaV = numpy.zeros(V.shape)
            for j in range(n): 
                deltaV[j, :] = learner.derivativeVi(indPtr, colInds, U, V, gp, gq, j)
            
            deltaV2 = numpy.zeros(V.shape)
            
            for i in range(n): 
                for j in range(k):
                    tempV = V.copy() 
                    tempV[i,j] += eps
                    obj1 = learner.objective(indPtr, colInds, indPtr, colInds, U, tempV, gp, gq)
                    
                    tempV = V.copy() 
                    tempV[i,j] -= eps
                    obj2 = learner.objective(indPtr, colInds, indPtr, colInds, U, tempV,  gp, gq)
                    
                    deltaV2[i,j] = (obj1-obj2)/(2*eps)
                #deltaV2[i,:] = deltaV2[i,:]/numpy.linalg.norm(deltaV2[i,:])
              
            nptst.assert_almost_equal(deltaV, deltaV2, 3)         
Beispiel #4
0
    def testDerivativeU(self): 
        m = 10 
        n = 20 
        nnzPerRow = 5 
        X = SparseUtils.generateSparseBinaryMatrix((m, n), nnzPerRow, csarray=True)
        
        k = 5
        u = 0.1
        eps = 0.05
        learner = MaxAUCSquare(k)
        learner.normalise = False
        learner.lmbdaU = 0
        learner.lmbdaV = 0
        learner.rho = 1.0
        learner.numAucSamples = n

        numRuns = 20
        gi = numpy.random.rand(m)
        gi /= gi.sum()        
        gp = numpy.random.rand(n)
        gp /= gp.sum()        
        gq = numpy.random.rand(n)
        gq /= gq.sum()     
        
        indPtr, colInds = SparseUtils.getOmegaListPtr(X)

        for s in range(numRuns):
            U = numpy.random.randn(m, k)
            V = numpy.random.randn(n, k)
            deltaU = numpy.zeros(U.shape)
            for i in range(X.shape[0]): 
                deltaU[i, :] = learner.derivativeUi(indPtr, colInds, U, V, gp, gq, i)      
    
            deltaU2 = numpy.zeros(U.shape) 
            eps = 10**-8         
            
            for i in range(m): 
                for j in range(k):
                    tempU = U.copy() 
                    tempU[i,j] += eps
                    obj1 = learner.objective(indPtr, colInds, indPtr, colInds, tempU, V, gp, gq)
                    
                    tempU = U.copy() 
                    tempU[i,j] -= eps
                    obj2 = learner.objective(indPtr, colInds, indPtr, colInds, tempU, V, gp, gq)
                    
                    deltaU2[i,j] = (obj1-obj2)/(2*eps)
    
                #deltaU2[i,:] = deltaU2[i,:]/numpy.linalg.norm(deltaU2[i,:])
            
            #print(deltaU*100)
            #print(deltaU2*100)
            nptst.assert_almost_equal(deltaU, deltaU2, 3)
        
        #Try r != 0 and rho > 0
        for s in range(numRuns):
            U = numpy.random.randn(m, k)
            V = numpy.random.randn(n, k)
            learner.rho = 0.1
            
            deltaU = numpy.zeros(U.shape)
            for i in range(X.shape[0]): 
                deltaU[i, :] = learner.derivativeUi(indPtr, colInds, U, V, gp, gq, i)
            
            deltaU2 = numpy.zeros(U.shape) 
            eps = 10**-9        
            
            for i in range(m): 
                for j in range(k):
                    tempU = U.copy() 
                    tempU[i,j] += eps
                    obj1 = learner.objective(indPtr, colInds, indPtr, colInds, tempU, V, gp, gq)
                    
                    tempU = U.copy() 
                    tempU[i,j] -= eps
                    obj2 = learner.objective(indPtr, colInds, indPtr, colInds, tempU, V, gp, gq)
                    
                    deltaU2[i,j] = (obj1-obj2)/(2*eps)
                                
            nptst.assert_almost_equal(deltaU, deltaU2, 3)
        
        #Try lmbda > 0
        
        for s in range(numRuns):
            U = numpy.random.randn(m, k)
            V = numpy.random.randn(n, k)
            learner.lmbdaU = 0.5
            
            deltaU = numpy.zeros(U.shape)
            for i in range(X.shape[0]): 
                deltaU[i, :] = learner.derivativeUi(indPtr, colInds, U, V, gp, gq, i) 
            
            deltaU2 = numpy.zeros(U.shape) 
            eps = 10**-9        
            
            for i in range(m): 
                for j in range(k):
                    tempU = U.copy() 
                    tempU[i,j] += eps
                    obj1 = learner.objective(indPtr, colInds, indPtr, colInds, tempU, V, gp, gq)
                    
                    tempU = U.copy() 
                    tempU[i,j] -= eps
                    obj2 = learner.objective(indPtr, colInds, indPtr, colInds, tempU, V, gp, gq)
                    
                    deltaU2[i,j] = (obj1-obj2)/(2*eps)
                                
            nptst.assert_almost_equal(deltaU, deltaU2, 3)
Beispiel #5
0
    def testComputeV1V2(self): 
        m = 10 
        n = 20 
        nnzPerRow = 5 
        X = SparseUtils.generateSparseBinaryMatrix((m, n), nnzPerRow, csarray=True)
        
        k = 5
        learner = MaxAUCSquare(k)
        learner.normalise = False
        learner.lmbdaU = 0
        learner.lmbdaV = 0
        learner.rho = 1.0
        learner.numAucSamples = 20
        
        numRuns = 500      
        gp = numpy.random.rand(n)
        gp /= gp.sum()        
        gq = numpy.random.rand(n)
        gq /= gq.sum()    

        permutedRowInds = numpy.arange(m, dtype=numpy.uint32)        
        permutedColInds = numpy.arange(n, dtype=numpy.uint32)
        
        indPtr, colInds = SparseUtils.getOmegaListPtr(X)        
        
        U = numpy.random.randn(m, k)
        V = numpy.random.randn(n, k)        
        
        V11 = numpy.zeros((m, k))
        V21 = numpy.zeros((m, k))    
        
        W11 = numpy.zeros((m, k))
        W21 = numpy.zeros((m, k)) 
        
        for i in range(numRuns):
            tempV1, tempV2, tempW1, tempW2 = learner.computeMeansVW(indPtr, colInds, U, V, permutedRowInds, permutedColInds, gp, gq)
            V11 += tempV1
            V21 += tempV2
            
            W11 += tempW1 
            W21 += tempW2
        
        V11 /= numRuns 
        V21 /= numRuns 

        W11 /= numRuns 
        W21 /= numRuns          
        
        #print(V11)
        #print(V21)        
        
        #Now compute real solution 
        V12 = numpy.zeros((m, k))
        V22 = numpy.zeros((m, k))   
        W12 = numpy.zeros((m, k))
        W22 = numpy.zeros((m, k))                 
                 
                 
        #The bootstrap sampling causes slight errors 
        for i in range(m):
            normGp = 0
            omegai = colInds[indPtr[i]:indPtr[i+1]]
            for j in omegai: 
                V12[i, :] += V[j, :]*gp[j]
                W12[i, :] += V[j, :]*gp[j]*(U[i, :].dot(V[j, :]))
                normGp += gp[j]
                
            V12[i, :] /= normGp
            W12[i, :] /= normGp


            normGq = 0             
            omegaBari = numpy.setdiff1d(numpy.arange(n, dtype=numpy.uint32), omegai, assume_unique=True)
            
            for j in omegaBari: 
                V22[i, :] += V[j, :]*gq[j]
                W22[i, :] += V[j, :]*gq[j]*(U[i, :].dot(V[j, :]))
                normGq += gq[j]
                
            V22[i, :] /= normGq
            W22[i, :] /= normGq
                

        #print(W21)
        #print(W22)
        nptst.assert_array_almost_equal(V11, V12, 1)
        nptst.assert_array_almost_equal(V21, V22, 1)
        
        nptst.assert_array_almost_equal(W11, W12, 1)
        nptst.assert_array_almost_equal(W21, W22, 1)
Beispiel #6
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 #7
0
    def testDerivativeV(self):
        m = 10
        n = 20
        nnzPerRow = 5
        X = SparseUtils.generateSparseBinaryMatrix((m, n),
                                                   nnzPerRow,
                                                   csarray=True)

        for i in range(m):
            X[i, 0] = 1
            X[i, 1] = 0

        k = 5
        u = 0.1
        w = 1 - u
        eps = 0.05
        learner = MaxAUCSquare(k, w)
        learner.normalise = False
        learner.lmbdaU = 0
        learner.lmbdaV = 0
        learner.rho = 1.0
        learner.numAucSamples = 100

        numRuns = 20
        indPtr, colInds = SparseUtils.getOmegaListPtr(X)

        gp = numpy.random.rand(n)
        gp /= gp.sum()
        gq = numpy.random.rand(n)
        gq /= gq.sum()

        for s in range(numRuns):
            U = numpy.random.randn(m, k)
            V = numpy.random.randn(n, k)

            deltaV = numpy.zeros(V.shape)
            for j in range(n):
                deltaV[j, :] = learner.derivativeVi(indPtr, colInds, U, V, gp,
                                                    gq, j)

            deltaV2 = numpy.zeros(V.shape)

            eps = 0.00001

            for i in range(n):
                for j in range(k):
                    tempV = V.copy()
                    tempV[i, j] += eps
                    obj1 = learner.objective(indPtr, colInds, indPtr, colInds,
                                             U, tempV, gp, gq)

                    tempV = V.copy()
                    tempV[i, j] -= eps
                    obj2 = learner.objective(indPtr, colInds, indPtr, colInds,
                                             U, tempV, gp, gq)

                    deltaV2[i, j] = (obj1 - obj2) / (2 * eps)
                #deltaV2[i,:] = deltaV2[i,:]/numpy.linalg.norm(deltaV2[i,:])

            nptst.assert_almost_equal(deltaV, deltaV2, 3)

        #Try r != 0 and rho > 0
        for s in range(numRuns):
            U = numpy.random.randn(m, k)
            V = numpy.random.randn(n, k)
            learner.rho = 1.0

            deltaV = numpy.zeros(V.shape)
            for j in range(n):
                deltaV[j, :] = learner.derivativeVi(indPtr, colInds, U, V, gp,
                                                    gq, j)

            deltaV2 = numpy.zeros(V.shape)

            for i in range(n):
                for j in range(k):
                    tempV = V.copy()
                    tempV[i, j] += eps
                    obj1 = learner.objective(indPtr, colInds, indPtr, colInds,
                                             U, tempV, gp, gq)

                    tempV = V.copy()
                    tempV[i, j] -= eps
                    obj2 = learner.objective(indPtr, colInds, indPtr, colInds,
                                             U, tempV, gp, gq)

                    deltaV2[i, j] = (obj1 - obj2) / (2 * eps)
                #deltaV2[i,:] = deltaV2[i,:]/numpy.linalg.norm(deltaV2[i,:])

            nptst.assert_almost_equal(deltaV, deltaV2, 3)

        #Try r != 0 and rho > 0
        for s in range(numRuns):
            U = numpy.random.randn(m, k)
            V = numpy.random.randn(n, k)

            learner.lmbdaV = 100

            deltaV = numpy.zeros(V.shape)
            for j in range(n):
                deltaV[j, :] = learner.derivativeVi(indPtr, colInds, U, V, gp,
                                                    gq, j)

            deltaV2 = numpy.zeros(V.shape)

            for i in range(n):
                for j in range(k):
                    tempV = V.copy()
                    tempV[i, j] += eps
                    obj1 = learner.objective(indPtr, colInds, indPtr, colInds,
                                             U, tempV, gp, gq)

                    tempV = V.copy()
                    tempV[i, j] -= eps
                    obj2 = learner.objective(indPtr, colInds, indPtr, colInds,
                                             U, tempV, gp, gq)

                    deltaV2[i, j] = (obj1 - obj2) / (2 * eps)
                #deltaV2[i,:] = deltaV2[i,:]/numpy.linalg.norm(deltaV2[i,:])

            nptst.assert_almost_equal(deltaV, deltaV2, 3)
Beispiel #8
0
    def testDerivativeUiApprox(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 derivative 
        """
        m = 20
        n = 30
        k = 3
        X = SparseUtils.generateSparseBinaryMatrix((m, n), k, csarray=True)

        w = 0.1
        learner = MaxAUCSquare(k, w)
        learner.normalise = False
        learner.lmbdaU = 0
        learner.lmbdaV = 0
        learner.rho = 1.0
        learner.numAucSamples = 10

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

        gp = numpy.random.rand(n)
        gp /= gp.sum()
        gq = numpy.random.rand(n)
        gq /= gq.sum()

        numRuns = 200
        numTests = 5

        indPtr, colInds = SparseUtils.getOmegaListPtr(X)
        permutedRowInds = numpy.arange(m, dtype=numpy.uint32)
        permutedColInds = numpy.arange(n, dtype=numpy.uint32)

        #Test with small number of AUC samples, but normalise
        learner.numAucSamples = 50
        numRuns = 200

        for i in numpy.random.permutation(m)[0:numTests]:
            U = numpy.random.rand(X.shape[0], k)
            V = numpy.random.rand(X.shape[1], k)

            du1 = numpy.zeros(k)
            for j in range(numRuns):
                VDot, VDotDot, WDot, WDotDot = learner.computeMeansVW(
                    indPtr, colInds, U, V, permutedRowInds, permutedColInds,
                    gp, gq)
                du1 += learner.derivativeUiApprox(U, V, VDot, VDotDot, WDot,
                                                  WDotDot, i)
            du1 /= numRuns
            du2 = learner.derivativeUi(indPtr, colInds, U, V, gp, gq, i)
            #print(du1, du2)
            print(du1 / numpy.linalg.norm(du1), du2 / numpy.linalg.norm(du2))
            #print(numpy.linalg.norm(du1 - du2)/numpy.linalg.norm(du1))
            nptst.assert_array_almost_equal(du1, du2, 2)

        #Let's compare against using the exact derivative
        for i in numpy.random.permutation(m)[0:numTests]:
            U = numpy.random.rand(X.shape[0], k)
            V = numpy.random.rand(X.shape[1], k)

            du1 = numpy.zeros(k)
            for j in range(numRuns):
                VDot, VDotDot, WDot, WDotDot = learner.computeMeansVW(
                    indPtr, colInds, U, V, permutedRowInds, permutedColInds,
                    gp, gq)
                du1 += learner.derivativeUiApprox(U, V, VDot, VDotDot, WDot,
                                                  WDotDot, i)
            du1 /= numRuns
            du2 = learner.derivativeUi(indPtr, colInds, U, V, gp, gq, i)

            print(du1 / numpy.linalg.norm(du1), du2 / numpy.linalg.norm(du2))
            nptst.assert_array_almost_equal(du1, du2, 2)

        learner.lmbdaV = 0.5

        for i in numpy.random.permutation(m)[0:numTests]:
            U = numpy.random.rand(X.shape[0], k)
            V = numpy.random.rand(X.shape[1], k)

            du1 = numpy.zeros(k)
            for j in range(numRuns):
                VDot, VDotDot, WDot, WDotDot = learner.computeMeansVW(
                    indPtr, colInds, U, V, permutedRowInds, permutedColInds,
                    gp, gq)
                du1 += learner.derivativeUiApprox(U, V, VDot, VDotDot, WDot,
                                                  WDotDot, i)
            du1 /= numRuns
            du2 = learner.derivativeUi(indPtr, colInds, U, V, gp, gq, i)
            nptst.assert_array_almost_equal(du1, du2, 2)
            print(du1 / numpy.linalg.norm(du1), du2 / numpy.linalg.norm(du2))
Beispiel #9
0
    def testComputeV1V2(self):
        m = 10
        n = 20
        nnzPerRow = 5
        X = SparseUtils.generateSparseBinaryMatrix((m, n),
                                                   nnzPerRow,
                                                   csarray=True)

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

        numRuns = 500
        gp = numpy.random.rand(n)
        gp /= gp.sum()
        gq = numpy.random.rand(n)
        gq /= gq.sum()

        permutedRowInds = numpy.arange(m, dtype=numpy.uint32)
        permutedColInds = numpy.arange(n, dtype=numpy.uint32)

        indPtr, colInds = SparseUtils.getOmegaListPtr(X)

        U = numpy.random.randn(m, k)
        V = numpy.random.randn(n, k)

        V11 = numpy.zeros((m, k))
        V21 = numpy.zeros((m, k))

        W11 = numpy.zeros((m, k))
        W21 = numpy.zeros((m, k))

        for i in range(numRuns):
            tempV1, tempV2, tempW1, tempW2 = learner.computeMeansVW(
                indPtr, colInds, U, V, permutedRowInds, permutedColInds, gp,
                gq)
            V11 += tempV1
            V21 += tempV2

            W11 += tempW1
            W21 += tempW2

        V11 /= numRuns
        V21 /= numRuns

        W11 /= numRuns
        W21 /= numRuns

        #print(V11)
        #print(V21)

        #Now compute real solution
        V12 = numpy.zeros((m, k))
        V22 = numpy.zeros((m, k))
        W12 = numpy.zeros((m, k))
        W22 = numpy.zeros((m, k))

        #The bootstrap sampling causes slight errors
        for i in range(m):
            normGp = 0
            omegai = colInds[indPtr[i]:indPtr[i + 1]]
            for j in omegai:
                V12[i, :] += V[j, :] * gp[j]
                W12[i, :] += V[j, :] * gp[j] * (U[i, :].dot(V[j, :]))
                normGp += gp[j]

            V12[i, :] /= normGp
            W12[i, :] /= normGp

            normGq = 0
            omegaBari = numpy.setdiff1d(numpy.arange(n, dtype=numpy.uint32),
                                        omegai,
                                        assume_unique=True)

            for j in omegaBari:
                V22[i, :] += V[j, :] * gq[j]
                W22[i, :] += V[j, :] * gq[j] * (U[i, :].dot(V[j, :]))
                normGq += gq[j]

            V22[i, :] /= normGq
            W22[i, :] /= normGq

        #print(W21)
        #print(W22)
        nptst.assert_array_almost_equal(V11, V12, 1)
        nptst.assert_array_almost_equal(V21, V22, 1)

        nptst.assert_array_almost_equal(W11, W12, 1)
        nptst.assert_array_almost_equal(W21, W22, 1)
Beispiel #10
0
    def testDerivativeU(self):
        m = 10
        n = 20
        nnzPerRow = 5
        X = SparseUtils.generateSparseBinaryMatrix((m, n),
                                                   nnzPerRow,
                                                   csarray=True)

        k = 5
        u = 0.1
        eps = 0.05
        learner = MaxAUCSquare(k)
        learner.normalise = False
        learner.lmbdaU = 0
        learner.lmbdaV = 0
        learner.rho = 1.0
        learner.numAucSamples = n

        numRuns = 20
        gi = numpy.random.rand(m)
        gi /= gi.sum()
        gp = numpy.random.rand(n)
        gp /= gp.sum()
        gq = numpy.random.rand(n)
        gq /= gq.sum()

        indPtr, colInds = SparseUtils.getOmegaListPtr(X)

        for s in range(numRuns):
            U = numpy.random.randn(m, k)
            V = numpy.random.randn(n, k)
            deltaU = numpy.zeros(U.shape)
            for i in range(X.shape[0]):
                deltaU[i, :] = learner.derivativeUi(indPtr, colInds, U, V, gp,
                                                    gq, i)

            deltaU2 = numpy.zeros(U.shape)
            eps = 10**-8

            for i in range(m):
                for j in range(k):
                    tempU = U.copy()
                    tempU[i, j] += eps
                    obj1 = learner.objective(indPtr, colInds, indPtr, colInds,
                                             tempU, V, gp, gq)

                    tempU = U.copy()
                    tempU[i, j] -= eps
                    obj2 = learner.objective(indPtr, colInds, indPtr, colInds,
                                             tempU, V, gp, gq)

                    deltaU2[i, j] = (obj1 - obj2) / (2 * eps)

                #deltaU2[i,:] = deltaU2[i,:]/numpy.linalg.norm(deltaU2[i,:])

            #print(deltaU*100)
            #print(deltaU2*100)
            nptst.assert_almost_equal(deltaU, deltaU2, 3)

        #Try r != 0 and rho > 0
        for s in range(numRuns):
            U = numpy.random.randn(m, k)
            V = numpy.random.randn(n, k)
            learner.rho = 0.1

            deltaU = numpy.zeros(U.shape)
            for i in range(X.shape[0]):
                deltaU[i, :] = learner.derivativeUi(indPtr, colInds, U, V, gp,
                                                    gq, i)

            deltaU2 = numpy.zeros(U.shape)
            eps = 10**-9

            for i in range(m):
                for j in range(k):
                    tempU = U.copy()
                    tempU[i, j] += eps
                    obj1 = learner.objective(indPtr, colInds, indPtr, colInds,
                                             tempU, V, gp, gq)

                    tempU = U.copy()
                    tempU[i, j] -= eps
                    obj2 = learner.objective(indPtr, colInds, indPtr, colInds,
                                             tempU, V, gp, gq)

                    deltaU2[i, j] = (obj1 - obj2) / (2 * eps)

            nptst.assert_almost_equal(deltaU, deltaU2, 3)

        #Try lmbda > 0

        for s in range(numRuns):
            U = numpy.random.randn(m, k)
            V = numpy.random.randn(n, k)
            learner.lmbdaU = 0.5

            deltaU = numpy.zeros(U.shape)
            for i in range(X.shape[0]):
                deltaU[i, :] = learner.derivativeUi(indPtr, colInds, U, V, gp,
                                                    gq, i)

            deltaU2 = numpy.zeros(U.shape)
            eps = 10**-9

            for i in range(m):
                for j in range(k):
                    tempU = U.copy()
                    tempU[i, j] += eps
                    obj1 = learner.objective(indPtr, colInds, indPtr, colInds,
                                             tempU, V, gp, gq)

                    tempU = U.copy()
                    tempU[i, j] -= eps
                    obj2 = learner.objective(indPtr, colInds, indPtr, colInds,
                                             tempU, V, gp, gq)

                    deltaU2[i, j] = (obj1 - obj2) / (2 * eps)

            nptst.assert_almost_equal(deltaU, deltaU2, 3)