def main(): # Get training file name from the command line traindatafile = sys.argv[1] # The training file is in libSVM format with open(traindatafile, mode="r") as myFile: lines = myFile.readlines() random.shuffle(lines) open("tempdata.dat", 'w').writelines(lines) tr_data = load_svmlight_file("tempdata.dat") #To randomly select 5000 points Xtr = tr_data[0].toarray() # Converts sparse matrices to dense Ytr = tr_data[1] # The trainig labels Xtr = Xtr[:5000] Ytr = Ytr[:5000] # Cast data to Shogun format to work with LMNN features = RealFeatures(Xtr.T) labels = MulticlassLabels(Ytr.astype(np.float64)) #print(Xtr.shape) ### Do magic stuff here to learn the best metric you can ### kmax = 25 #inductive bias values = list(range(1, kmax + 1)) k = predict(Xtr, Ytr, values) # Number of target neighbours per example - tune this using validation #print(k) # Initialize the LMNN package print("K : "), print(k) k = 5 lmnn = LMNN(features, labels, k) init_transform = np.eye(Xtr.shape[1]) # Choose an appropriate timeout lmnn.set_maxiter(25000) lmnn.train(init_transform) # Let LMNN do its magic and return a linear transformation # corresponding to the Mahalanobis metric it has learnt L = lmnn.get_linear_transform() M = np.matrix(np.dot(L.T, L)) print("LMNN done") #print(M) # Save the model for use in testing phase # Warning: do not change this file name np.save("model.npy", M)
def diagonal_lmnn(features, labels, k=3, max_iter=10000): from modshogun import LMNN, MSG_DEBUG import numpy lmnn = LMNN(features, labels, k) # lmnn.io.set_loglevel(MSG_DEBUG) lmnn.set_diagonal(True) lmnn.set_maxiter(max_iter) lmnn.train(numpy.eye(features.get_num_features())) return lmnn
def diagonal_lmnn(features,labels,k=3,max_iter=10000): from modshogun import LMNN, MSG_DEBUG import numpy lmnn = LMNN(features,labels,k) # lmnn.io.set_loglevel(MSG_DEBUG) lmnn.set_diagonal(True) lmnn.set_maxiter(max_iter) lmnn.train(numpy.eye(features.get_num_features())) return lmnn
def main(): # Get training file name from the command line traindatafile = sys.argv[1] # The training file is in libSVM format tr_data = load_svmlight_file(traindatafile); Xtr = tr_data[0].toarray(); # Converts sparse matrices to dense Ytr = tr_data[1]; # The trainig labels Indices_array = np.arange(Ytr.shape[0]); np.random.shuffle(Indices_array); Xtr = Xtr[Indices_array]; Xtr = Xtr[:6000]; Ytr = Ytr[Indices_array]; Ytr = Ytr[:6000]; # Cast data to Shogun format to work with LMNN features = RealFeatures(Xtr.T) labels = MulticlassLabels(Ytr.astype(np.float64)) ### Do magic stuff here to learn the best metric you can ### # Number of target neighbours per example - tune this using validation k = 10 # Initialize the LMNN package lmnn = LMNN(features, labels, k) init_transform = np.eye(Xtr.shape[1]) # Choose an appropriate timeout lmnn.set_maxiter(200000) lmnn.train(init_transform) # Let LMNN do its magic and return a linear transformation # corresponding to the Mahalanobis metric it has learnt L = lmnn.get_linear_transform() M = np.matrix(np.dot(L.T, L)) # Save the model for use in testing phase # Warning: do not change this file name np.save("model.npy", M)
def RunLMNNShogun(): totalTimer = Timer() # Load input dataset. Log.Info("Loading dataset", self.verbose) # Use the last row of the training set as the responses. X, y = SplitTrainData(self.dataset) try: feat = RealFeatures(X.T) labels = MulticlassLabels(y.astype(np.float64)) with totalTimer: # Get the options for running LMNN. if "k" in options: self.k = int(options.pop("k")) if "maxiter" in options: n = int(options.pop("maxiter")) else: n = 2000 if len(options) > 0: Log.Fatal("Unknown parameters: " + str(options)) raise Exception("unknown parameters") # Perform LMNN. prep = ShogunLMNN(feat, labels, self.k) prep.set_maxiter(n) prep.train() except Exception as e: return [-1, -1] time = totalTimer.ElapsedTime() # Get distance. distance = prep.get_linear_transform() dataList = [X, y] accuracy1NN = Metrics.KNNAccuracy(distance, dataList, 1, False) accuracy3NN = Metrics.KNNAccuracy(distance, dataList, 3, False) accuracy3NNDW = Metrics.KNNAccuracy(distance, dataList, 3, True) accuracy5NN = Metrics.KNNAccuracy(distance, dataList, 5, False) accuracy5NNDW = Metrics.KNNAccuracy(distance, dataList, 5, True) return [time, accuracy1NN, accuracy3NN, accuracy3NNDW, accuracy5NN, accuracy5NNDW]
def main(): # Get training file name from the command line traindatafile = sys.argv[1] # The training file is in libSVM format tr_data = load_svmlight_file(traindatafile) print("loaded data") init_transform = np.eye(tr_data[0].toarray().shape[1]) print(init_transform) Xtr = tr_data[0][:6000].toarray() # Converts sparse matrices to dense Ytr = tr_data[1][:6000] # The trainig labels # Cast data to Shogun format to work with LMNN features = RealFeatures(Xtr.T) labels = MulticlassLabels(Ytr.astype(np.float64)) ### Do magic stuff here to learn the best metric you can ### # Number of target neighbours per example - tune this using validation k = 21 # Initialize the LMNN package print("starting lmnn train....") lmnn = LMNN(features, labels, k) # Choose an appropriate timeout lmnn.set_maxiter(3000) lmnn.train(init_transform) # Let LMNN do its magic and return a linear transformation # corresponding to the Mahalanobis metric it has learnt L = lmnn.get_linear_transform() M = np.matrix(np.dot(L.T, L)) print(M) # Save the model for use in testing phase # Warning: do not change this file name statistics = lmnn.get_statistics() pyplot.plot(statistics.obj.get()) pyplot.grid(True) pyplot.xlabel('Number of iterations') pyplot.ylabel('LMNN objective') pyplot.show() np.save("model.npy", M)
def lmnn_classify(traindat, testdat, k=3): from modshogun import LMNN, KNN, MulticlassAccuracy, MSG_DEBUG train_features, train_labels = traindat.features, traindat.labels lmnn = LMNN(train_features, train_labels, k) lmnn.set_maxiter(1200) lmnn.io.set_loglevel(MSG_DEBUG) lmnn.train() distance = lmnn.get_distance() knn = KNN(k, distance, train_labels) knn.train() test_features, test_labels = testdat.features, testdat.labels predicted_labels = knn.apply(test_features) evaluator = MulticlassAccuracy() acc = evaluator.evaluate(predicted_labels, test_labels) err = 1 - acc return err
def lmnn_classify(traindat, testdat, k=3): from modshogun import LMNN, KNN, MulticlassAccuracy, MSG_DEBUG train_features, train_labels = traindat.features, traindat.labels lmnn = LMNN(train_features, train_labels, k) lmnn.set_maxiter(1200) lmnn.io.set_loglevel(MSG_DEBUG) lmnn.train() distance = lmnn.get_distance() knn = KNN(k, distance, train_labels) knn.train() test_features, test_labels = testdat.features, testdat.labels predicted_labels = knn.apply(test_features) evaluator = MulticlassAccuracy() acc = evaluator.evaluate(predicted_labels, test_labels) err = 1-acc return err
def main(): Xtr, Ytr = gettrainData() Xtr = Xtr[:len(Xtr) // 6] Ytr = Ytr[:len(Ytr) // 6] # Cast data to Shogun format to work with LMNN features = RealFeatures(Xtr.T) labels = MulticlassLabels(Ytr.astype(np.float64)) print(2.1) ### Do magic stuff here to learn the best metric you can ### # Number of target neighbours per example - tune this using validation k = 10 # Initialize the LMNN package lmnn = LMNN(features, labels, k) print(2.2) init_transform = np.eye(Xtr.shape[1]) print(2.3) # Choose an appropriate timeout lmnn.set_maxiter(8000) print(2.4) lmnn.train(init_transform) print(2.5) # Let LMNN do its magic and return a linear transformation # corresponding to the Mahalanobis metric it has learnt L = lmnn.get_linear_transform() print(2.6) M = np.matrix(np.dot(L.T, L)) print(2.7) # Save the model for use in testing phase # Warning: do not change this file name np.save("model2.npy", M)
print('%d vectors with %d features' % (features.get_num_vectors(), features.get_num_features())) assert(features.get_num_vectors() == labels.get_num_labels()) distance = EuclideanDistance(features, features) k = 2 knn = KNN(k, distance, labels) plot_data(x, y, axarr[0]) plot_neighborhood_graph(x, knn.nearest_neighbors(), axarr[0]) axarr[0].set_aspect('equal') axarr[0].set_xlim(-6, 4) axarr[0].set_ylim(-3, 2) lmnn = LMNN(features, labels, k) lmnn.set_maxiter(10000) lmnn.train() L = lmnn.get_linear_transform() knn.set_distance(lmnn.get_distance()) plot_data(x, y, axarr[1]) plot_neighborhood_graph(x, knn.nearest_neighbors(), axarr[1]) axarr[1].set_aspect('equal') axarr[1].set_xlim(-6, 4) axarr[1].set_ylim(-3, 2) xL = numpy.dot(x, L.T) ## to see the data after the linear transformation features = RealFeatures(xL.T) distance = EuclideanDistance(features, features) knn.set_distance(distance)
print('%d vectors with %d features' % (features.get_num_vectors(), features.get_num_features())) assert (features.get_num_vectors() == labels.get_num_labels()) distance = EuclideanDistance(features, features) k = 2 knn = KNN(k, distance, labels) plot_data(x, y, axarr[0]) plot_neighborhood_graph(x, knn.nearest_neighbors(), axarr[0]) axarr[0].set_aspect('equal') axarr[0].set_xlim(-6, 4) axarr[0].set_ylim(-3, 2) lmnn = LMNN(features, labels, k) lmnn.set_maxiter(10000) lmnn.train() L = lmnn.get_linear_transform() knn.set_distance(lmnn.get_distance()) plot_data(x, y, axarr[1]) plot_neighborhood_graph(x, knn.nearest_neighbors(), axarr[1]) axarr[1].set_aspect('equal') axarr[1].set_xlim(-6, 4) axarr[1].set_ylim(-3, 2) xL = numpy.dot(x, L.T) ## to see the data after the linear transformation features = RealFeatures(xL.T) distance = EuclideanDistance(features, features) knn.set_distance(distance)
random.seed(13) subset = random.permutation(len(Y)) Xtrain = X[subset[:30000],:] Ytrain = Y[subset[:30000]] print "Training data used: " + str(Xtrain.shape) prod_features = RealFeatures(Xtrain.T) prod_labels = MulticlassLabels(Ytrain.T) k = 5 # train LMNN sf = SerializableAsciiFile(".lmnn_model30000_5_reg05_cor20", 'w') print "Training LMNN..." #init_t = np.eye(features.shape[1]) lmnn = LMNN(prod_features, prod_labels, k) lmnn.set_maxiter(800) #lmnn.set_diagonal(True) lmnn.set_stepsize_threshold(1e-10) lmnn.set_regularization(0.5) lmnn.set_correction(20) #lmnn.train(init_t) lmnn.train() lmnn.save_serializable(sf) plot_lmnn_statistics(lmnn)