私はこの 1 週間、Java で Affinity Propagation を実装しようとしてきました。frey と dueck による元の論文に記載されているとおりに行いましたが、良い手本が得られませんでした。
研究論文はここにあります: http://www.psi.toronto.edu/affinitypropagation/FreyDueckScience07.pdf
類似度関数 (研究論文からの文のクラスタリング) のために私が書いたコードは次のとおりです。
public static void calculateSimilarity(){
try{
for(int i=0; i<tweets.size(); i++){//For each tweet
for(int j=0; j<tweets.size(); j++){//and the one next to it, split both into tokens
String[]firstTokens=tweets.get(i).toLowerCase().split(" ");
String[]secondTokens=tweets.get(j).toLowerCase().split(" ");//tokenize it
//store summed cost in respective matrix.
if(i==j){//calculate self similarity{
similarity[i][j]=firstTokens.length*NEGATIVE_LOG_OF_DICTIONARY+ADJUSTMENT_FACTOR;
System.out.println(similarity[i][j]);
}
else{
//The costC per word. These will be summed
double Cost=compare(firstTokens, secondTokens);//compare
similarity[i][j]=Cost;//assign the similarity
}
}//end inner for
}//end outer for
}//end try
catch(Exception e){
System.out.println(temp);
e.printStackTrace();
}//end catch
}//end method
public static double compare(String[]firstString,String[]secondString){
double Cost=0;
for(int k=0; k<firstString.length; k++){//for first tweet tokens
for(int l=0; l<secondString.length;l++){//compare to second tweet tokens
//Look at words that are greater than 2 characters
if(firstString[k].length()>=5 &&secondString[l].length()>=5){
if(firstString[k].contains(secondString[l])){
//increment the cost
Cost+=-Math.log10(secondString.length);
}
else//Cost of the word if no word is similar
{
Cost+=NEGATIVE_LOG_OF_DICTIONARY;
}
}//end big if
}//end l for loop
}//end inner inner for
return Cost;
}
2 つのデータ ポイント (文) 間の類似度を計算した方法は次のとおりです 。 文 i と文 k の類似度は、文 i のすべての単語を単語文 k および原稿内のすべての単語の辞書。文 i の各単語について、その単語が文 k の単語と一致した場合、その単語のコーディング コストは、文 k の単語数の負の対数に設定されました (一致した 5 つの単語のインデックスをコーディングするコストそれ以外の場合は、原稿辞書の単語数の負の対数 (原稿辞書の単語のインデックスをコーディングするコスト) に設定されました。いずれかの単語が他の単語の部分文字列である場合、その単語は別の単語と一致すると見なされました。
可用性と責任機能も書きました。
可用性: public static double updateAvalibility(int データポイント、int 候補、double[][] a、double[][] r、double aOld){ double availibity; //ArrayListtemp=新しいArrayList(); ダブル合計 = 0;
//*For self availibility
if(datapoint==candidate){
for(int j=0; j<tweets.size(); j++){
if(j==datapoint)
continue;
else if(r[j][candidate]<0)//skip negative terms
continue;
else
total+=(r[j][candidate]);//sum up r of rows
}//end for
availibity=total;//The total becjomes the A
System.out.println("Availibility :"+availibity);
}//end if
else{//else
for(int j=0; j<tweets.size(); j++){
if(j==candidate||j==datapoint)
continue;
else if(r[j][candidate]<0)//skip negative terms
continue;
else
total+=r[j][candidate];//else sum all R of all rows
}//end for
availibity=(r[candidate][candidate]+total);//A is set to self R + the sum
if(availibity<0)//if not positive ignore
availibity=0;
}//end else
return (1-LAM)*availibity+(LAM*aOld);//Return with Adjustment factor
}
責任:
//updates responsibility. Takes the two competeing datapoints, s, r, and a
//returns the responsibility of i to k
public static double updateResponsibility(int datapoint, int candidate, double[][] s, double[][] a,double rOld){
double responsibility;
//A temporary array
ArrayList<Double>temp=new ArrayList<Double>();
double max;//The max of the a(i,k')+r(i,k')
//################################
//SETTING THE SELF RESPONSIBILITY
if(datapoint==candidate){
for(int k=0;k<tweets.size(); k++){
if(k==candidate)
continue;
else
temp.add(s[datapoint][k]);//store all the similarites b/w this point
//others
}
max=Collections.max(temp);//The max of the similarity
responsibility=(similarity[datapoint][candidate])-max;
System.out.println("s:"+similarity[datapoint][candidate]+"- m:"+max+"= responsibility: "+responsibility);
}
else{
for(int j=0; j<tweets.size();j++){
//store the A + S
if(j==candidate)
continue;
else
temp.add(a[datapoint][j]+s[datapoint][j]);// a(i,k')+r(i,k') Max will be calculated later
}//end inner for
//Max of the a+r of other k's.
max=Collections.max(temp);//Then get the max
responsibility=s[datapoint][candidate]-max;//then the similarity - the max
}//end else
return ((1-LAM)*responsibility)-(LAM*rOld);//Dampen responsibility and return
}//end method
論文に記載されている調整係数を使用しても、お粗末な手本が得られるのはなぜですか? 私は何を間違っていますか?
どんな助けでも大歓迎です。