2017-09-04 92 views
0

我有一個8 GB的RAM和英特爾酷睿I5處理器的聯想IdeaPad筆記本電腦。我每100個維度有60k個數據點。我想做KNN,爲此我運行LMNN算法來查找Mahalanobis度量標準。
問題是在我的Ubuntu上運行一個空白屏幕2小時後出現。我沒有得到什麼問題!我的記憶變滿了還是別的什麼?
那麼有什麼方法可以優化我的代碼?

我的數據集:data
我LMNN實現:如何在平庸的筆記本電腦上成功運行帶中等大小數據集的ML算法?

import numpy as np 
import sys 
from modshogun import LMNN, RealFeatures, MulticlassLabels 
from sklearn.datasets import load_svmlight_file 

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 

    # Cast data to Shogun format to work with LMNN 
    features = RealFeatures(Xtr.T) 
    labels = MulticlassLabels(Ytr.astype(np.float64)) 



    # Number of target neighbours per example - tune this using validation 
    k = 18 

    # 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) 

if __name__ == '__main__': 
    main() 
+0

你確定你得到一個較小的結果? – Julien

+0

是的,我得到的結果較小。即具有大約0.5K的數據點而不是60K。 – Fenil

+0

很明顯,它需要更多的處理器和RAM來處理數據,這不是代碼問題。 –

回答

0

精確K-NN具有可擴展性的問題。

Scikit學習有documentation page(縮放策略)在這種情況下做什麼(許多算法有partial_fit方法,但遺憾的是kNN沒有它)。

如果你願意交易一些準確的速度,你可以運行一些像approximate nearest neighbors

相關問題