1

我正在尝试为 CNN 实现一个通道,这个通道是将一个句子分成 x 个部分。然后这些部分中的每一个都获得一个情感分数,这些部分被输入到 CNN 中。但是,我不明白如何将这些部分分数转换为 CNN 的 INDArray。

我当前的代码:

public INDarray getFeatureVectors(List<String> sentences) {
    // nParts is the number of parts to split the sentence into
    // sentences are a list of sentences in the current batch

    // int[] featureShape = new int[]{sentences.size(), nParts}; 
    int[] featureShape = new int[4];
    featureShape[0] = sentences.size();
    featureShape[1] = 1;
    featureShape[2] = nParts;
    featureShape[3] = nParts;
    INDArray features = Nd4j.create(sentences.size());
    for (int i = 0; i < sentences.size(); i++) {

        List<String> tokens = // tokenize sentence

        double[] partScores = // calculate the score for each part
                              // e.g. for nParts = 2, partScores = {-1.0, 1.0}

        INDArray vector = Nd4j.create(partScores, featureShape);
        INDArrayIndex[] indices = new INDArrayIndex[4];
        indices[0] = NDArrayIndex.point(i);
        indices[1] = NDArrayIndex.point(0);
        indices[2] = NDArrayIndex.all();
        indices[3] = NDArrayIndex.all();
        features.put(indices, vector);

    }
    return features;
}

我刚刚尝试了不同的特征形状和索引,但我真的不知道我在做什么,所以任何帮助将不胜感激!

我将代码基于 Deeplearning4js CnnSentenceDataSetIterator,它将句子转换为词嵌入。

4

0 回答 0