行列と行を繰り返して行列を計算するコードがあります。実行された計算の一部は、コサイン距離測定であり、インターネット上で見つけたコードを使用しています(リンクをすぐに取得できませんでした)。このコードをスピードアップする方法は?行列の行と列を反復する計算法
10,000行と鞍部が存在する場合があります。行列は対称ですので、その半分を反復するだけです。値は浮動小数点です。
問題:(それはそう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;
}
}
これは宿題プロジェクトですか? MatLabなどの数学ソフトウェアを使用できませんか? –
これは学問分野のプロフェッショナルなプロジェクトです。私はJavaをそれに使用する必要があります - 自分自身の限界のBCを私は恐れています! – seinecle
アルゴリズムのどの部分が最長時間を要しているかをプロファイルしましたか?操作の始め/終わりに 'new Date()。getTime();'を追加するだけで、それらを減算することで素晴らしい見識を得ることができます。 – Marcelo