def constructSimilartyMatrixLMNN(self,ks): print 'now doing LMNN for k= ',ks self.y_train=self.y_train.reshape(-1,) lmnn=LMNN(k=ks, learn_rate=1e-7,max_iter=3000) lmnn.fit(self.trainVectorsPCA, self.y_train, verbose=False) self.L_lmnn = lmnn.transformer() name='lmnn/LMNN transformer matrix with dataset shape '+str(self.trainVectorsPCA.shape) np.save(name,self.L_lmnn) print 'L.shape is ',self.L_lmnn.shape,'\n\n' # Input data transformed to the metric space by X*L.T self.transformedTrainLMNN=copy(lmnn.transform(self.trainVectorsPCA)) self.transformedTestLMNN=copy(lmnn.transform(self.testVectorsPCA)) self.transformedAllLMNN=copy(lmnn.transform(self.allDataPCA)) #we compute the pairwise distance on this now projectedDigits = TSNE(random_state=randomState).fit_transform(self.transformedAllLMNN) plt.scatter(projectedDigits[:,0],projectedDigits[:,1],c=self.labels) plt.title('LMNN Transformed ALL set projected to 2 Dimensions by TSNE with k='+str(ks)) plt.savefig(pp,format='pdf') self.pwdis=copy(pairwise_distances(self.transformedAllLMNN,metric='euclidean')) self.D=np.zeros(self.pwdis.shape) for i in range(0,self.pwdis.shape[0]): l1=self.pwdis[i].tolist() #print 'l1 is ',l1,'\n\n' allnearestNeighbours=sorted(range(len(l1)),key=lambda i : l1[i]) #now set the all the weights except for k+1 to 0 self.pwdis[i,allnearestNeighbours[ks:]]=0 self.D[i,i]=sum(self.pwdis[i]) print 'accuracy for LMNN for k= ',ks,'\n' self.labelPropogation()
def constructSimilartyMatrixLMNN(self, ks): print('now doing LMNN for k= ', ks) self.y_train = self.y_train.reshape(-1, ) lmnn = LMNN(k=ks, learn_rate=1e-7, max_iter=1000) lmnn.fit(self.trainVectorsPCA, self.y_train) self.L_lmnn = lmnn.transformer() name = 'lmnn/LMNN transformer matrix with dataset shape ' + str( self.trainVectorsPCA.shape) np.save(name, self.L_lmnn) print('L.shape is ', self.L_lmnn.shape, '\n\n') # Input data transformed to the metric space by X*L.T self.transformedTrainLMNN = copy(lmnn.transform(self.trainVectorsPCA)) self.transformedTestLMNN = copy(lmnn.transform(self.testVectorsPCA)) self.transformedAllLMNN = copy(lmnn.transform( self.allDataPCA)) #we compute the pairwise distance on this now projectedDigits = TSNE(random_state=randomState).fit_transform( self.transformedAllLMNN) self.pwdis = copy( pairwise_distances(self.transformedAllLMNN, metric='euclidean')) self.D = np.zeros(self.pwdis.shape) for i in range(0, self.pwdis.shape[0]): l1 = self.pwdis[i].tolist() #print 'l1 is ',l1,'\n\n' allnearestNeighbours = sorted(range(len(l1)), key=lambda i: l1[i]) #now set the all the weights except for k+1 to 0 self.pwdis[i, allnearestNeighbours[ks:]] = 0 self.D[i, i] = sum(self.pwdis[i]) print('accuracy for LMNN for k= ', ks, '\n') self.labelPropogation()
def process_lmnn(self, **option): '''Metric Learning algorithm: LMNN''' GeneExp = self.GeneExp_train Label = self.Label_train lmnn = LMNN(**option) lmnn.fit(GeneExp, Label) self.Trans['LMNN'] = lmnn.transformer()
vectors = vectorizer.transform(data) print 'vectorizer is ' ,vectors[0].todense() itml=ITML() arr2=copy(vectors.todense()) arr=np.zeros((vectors.shape[0],vectors.shape[1])) for i in range(0,vectors.shape[0]): for j in range(0,vectors.shape[1]): arr[i,j]=arr2[i,j] print 'arr .shape is ',arr.shape target=newsgroups_train.target lab=[] for i in target: lab.append(i) lab=np.asarray(lab) print 'lab is ',(lab) print 'target is ',type(arr) #C=itml.prepare_constraints(target,vectors.shape[0],200) #itml.fit(arr,C,verbose=False) lmnn = LMNN(k=20, learn_rate=1e-3,use_pca=True) lmnn.fit(arr,target,verbose=False) print 'Now doing LMNN' l=lmnn.transformer() np.save('LMNN transformer',l)
def test_lmnn(self): lmnn = LMNN(k=5, learn_rate=1e-6, verbose=False) lmnn.fit(self.X, self.y) L = lmnn.transformer() assert_array_almost_equal(L.T.dot(L), lmnn.metric())
def test_lmnn(self): lmnn = LMNN(k=5, learn_rate=1e-6, verbose=False) lmnn.fit(self.X, self.y) L = lmnn.transformer() assert_array_almost_equal(L.T.dot(L), lmnn.metric())