在Java代码中实现亲和传播

时间:2013-08-05 21:06:11

标签: java algorithm machine-learning cluster-computing

过去一周,我一直在努力在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;
        }

以下是他们如何说他们计算两个数据点(句子)之间的相似性: 句子i与句子k的相似性被设定为 使用单词in中编码句子i中每个单词的信息理论成本(S5) 句子k和手稿中所有单词的字典。对于句子i中的每个单词,如果 该单词与句子k中的单词匹配,该单词的编码成本设置为 句子k中单词数量的对数(编码索引的成本) 五 匹配的单词),否则设置为单词数的负对数 在手稿词典中(编写手稿中单词索引的成本) 字典)。如果任一单词是子字符串,则认为单词与另一个单词匹配 另一个。

我还写了可用性和责任功能。

状况:     public static double updateAvalibility(int datapoint,int candidate,double [] [] a,double [] [] r,double aOld){                 双重可用性;                 // ArrayListtemp = new ArrayList();                 double total = 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

即使我使用论文中列出的调整因素,为什么我会得到糟糕的样本?我做错了什么?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我找不到您的问题,但您可以改用以下库:

Official siteGitHub siteCommunity site

它包含一个良好的亲和力传播算法。