如何将PVDM生成的矢量与doc2vec的PVDBOW方法相结合?

2024-05-14 22:08:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我有大约2万份60-150字的文件。在这20K个文档中,有400个文档已知类似的文档。这400个文档作为我的测试数据。你知道吗

我正在尝试使用gensim doc2vec查找这400个数据集的类似文档。“句子和文档的分布式表示”一文说,“PV-DM和PV-DBOW的组合通常效果更好(在IMDB中为7.42%),因此建议使用。”

所以我想把这两种方法的向量结合起来,找出所有列车文档的余弦相似度,并选择余弦距离最小的前5个。你知道吗

那么,将这两种方法的向量结合起来的有效方法是什么:加法、平均法还是其他方法???你知道吗

在组合这两个向量之后,我可以对每个向量进行归一化,然后找到余弦距离。你知道吗


Tags: 文件数据方法文档距离分布式dm向量
3条回答

Edit:刚刚看到您想要一个String作为arg

你可以用这个:

public class SO {
    public static void main(String[] args) throws Exception {
        String string = "X......X\n" +
                        "....X..X\n" +
                        "....X..X\n";

        System.out.println(string);
        string = rotateClockwise(string);
        System.out.println(string);
    }

    static String rotateClockwise(String input) {
        String[] arr = input.split("\n");
        int length = arr[0].length();
        String[] ret = new String[length];
        for (int i = 0; i < ret.length; i++) {
            ret[i] = "";
        }

        for (int i = arr.length-1; i >= 0; i ) {
            char[] chars = arr[i].toCharArray();

            for (int j = 0; j < ret.length; j++) {
                ret[j] += chars[j];
            }
        }
        String output = "";
        for (String str: ret)
            output += str + "\n";

        return output;
    }
}

请注意,这有错误检查

public static String[] rotate(String[] originalArray) {
    String[] rotatedArray = new String[originalArray[0].length()];
    for (int i=0;i<rotatedArray.length;i++) {
        rotatedArray[i]="";
    }
    for (int j = 0; j < originalArray[0].length(); j++) {
        for (int i = originalArray.length - 1; i >= 0; i ) {
            rotatedArray[j] += originalArray[i].charAt(j);
        }
    }
    return rotatedArray;
}                

编辑

我修复了OP在下面评论中指出的错误。这应该会产生上述原始问题所要求的结果

 public static String rotateStringMatrixBy90(String matrix) {
    int numberOfRows = 3; // this I leave as an exercise
    int numberOfColumns = 8; // same with this one

    String newMatrix = "";

    int count = 0;
    String[] newMatrixColumns= matrix.split("\n");
    while (count < matrix.split("\n")[0].length()) {
        for (int i = newMatrixColumns.length - 1; i > -1; i ) {
            newMatrix = newMatrix + newMatrixColumns[i].charAt(count);
        }

        newMatrix = newMatrix + "\n";
        count++;
    }

    return newMatrix;
 }

下面是你使用它的方式:

    String m = "X......X\n" +
               "....X..X\n" +
               "....X..X";

    System.out.println(m);

    m = rotateStringMatrixBy90(m);
    System.out.println(m);

(注意:假设您使用\n作为行之间的分隔符):

相关问题 更多 >

    热门问题