0

行と列を反復処理することで行列の計算を実行するコードがあります。実行された微積分の一部は、インターネットで見つけたコードを使用したコサイン距離測定です(現在リンクを取得できませんでした)。

10,000行と列が存在する可能性があります。行列は対称なので、半分だけ繰り返す必要があります。値はfloatです。

問題:それは非常に遅いです(それは3から6時間かかるようです)。誰かが私に改善点を指摘できますか?どうも!

コードに関する注意:柔軟性のために抽象クラスを使用します。このように、別のクラスで定義された正弦計算を別のクラスに簡単に置き換えることができます。

コード:

import Jama.Matrix;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.concurrent.ExecutionException;

public abstract class AbstractSimilarity {

    HashSet<Triple<Double, Integer, Integer>> set = new HashSet();
    public ArrayList<Thread> listThreads = new ArrayList();

    public void transform(Matrix matrixToBeTransformed) throws InterruptedException, 
ExecutionException {

        int numDocs = termDocumentMatrix.getColumnDimension();

        Main.similarityMatrix = new Matrix(numDocs, numDocs);

        System.out.println("size of the matrix: " + numDocs + "x " + numDocs);

        //1. iteration through all rows of the matrixToBeTransformed
        for (int i = numDocs - 1; i >0 ; i--) {
            System.out.println("matrix treatment... " + ((float) i / (float) numDocs * 100) + "%");

            //2. isolates the row i of this matrixToBeTransformed
            Matrix sourceDocMatrix = matrixToBeTransformed.getMatrix(
                    0, matrixToBeTransformed.getRowDimension() - 1, i, i);



            // 3. Iterates through all columns of the matrixToBeTransformed
//            for (int j = 0; j < numDocs; j++) {
//                if (j < i) {
//
//                    //4. isolates the column j of this matrixToBeTransformed
//                    Matrix targetDocMatrix = matrixToBeTransformed.getMatrix(
//                            0, matrixToBeTransformed.getRowDimension() - 1, j, j);


                    //5. computes the similarity between this given row and this given column and writes it in a resultMatrix
//                    Main.resultMatrix.set(i, j, computeSimilarity(sourceDocMatrix, targetDocMatrix));
//                } else {
//                    Main.resultMatrix.set(i, j, 0);

//                }
//
//            }
        }

実行する計算を定義するクラス:

import Jama.Matrix;

public class CosineSimilarity extends AbstractSimilarity{

  @Override
  protected double computeSimilarity(Matrix sourceDoc, Matrix targetDoc) {
    double dotProduct = sourceDoc.arrayTimes(targetDoc).norm1();
    double eucledianDist = sourceDoc.normF() * targetDoc.normF();
    return dotProduct / eucledianDist;
  }

}
4

1 に答える 1

2

^3アルゴリズムを扱っているようです。(半分の)行列を埋めているため、n^2。各要素(内積/ fnorm)を埋めるメソッドには時間nがかかるため、もう一度n回かかります。幸いなことに、計算は相互に依存しないため、これをマルチスレッド化して高速化できます。

public class DoCalc extends Thread
{
  public Matrix localM;
  int startRow;
  int endRow;
  public DoCalc(Matrix mArg, int startArg, int endArg)
  {
    localM=mArg;
    startRow=startArg;
    endRow=endArg;
  }

  public void doCalc()
  {
    //Pseudo-code
    for each row from startRow to endRow
      for each column 0 to size-1
        result[i][j] = similarityCalculation
  }
  public void run()
  {
    doCalc();
  }
}

public void transform(Matrix toBeTransformed)
{
  int numDocs = termDocumentMatrix.getColumnDimension();

  Main.similarityMatrix = new Matrix(numDocs, numDocs);
  Vector<DoCalc> running = new Vector<DoCalc>();
  int blockSize = 10;
  for (int x = 0; x < numDocs-1;x+=blockSize)
  {
    DoCalc tempThread = new DoCalc(toBeTransformed,x,(x+blockSize>numDocs-1)?numDocs-1:x+blockSize);
    tempThread.start();
    running.add(tempThread);
  }

  for (DoCalc dc : running)
    dc.join();

}

重要な注意事項:

これは非常に単純な実装です。自分のサイズの配列で実行しようとすると、1000スレッドが生成されます。blockSizeをいじるか、スレッドプールを調べることができます。

せいぜい、これにより速度が4倍、4倍に向上します。桁違いのゲインが必要な場合は、アルゴリズムを適切にプロファイリングおよび/またはより効率的なものに変更する必要があります。実行しようとしているタスク(マトリックス内の各要素で比較的高価なタスクを実行する)を考えると、後者は不可能な場合があります。

編集:マルチスレッドは、CPUにバインドされていて、コアが比較的アイドル状態になっているマルチコアCPUを使用している場合にのみ、速度を大幅に向上させます。

于 2012-03-02T16:17:36.197 に答える