5

我正在实施 Yoon Kim 的 CNN ( https://arxiv.org/abs/1408.5882 ) 用于 Julia 中的文本分类,使用 Flux 作为深度学习框架,将单个句子作为输入数据点。模型动物园(https://github.com/FluxML/model-zoo)在一定程度上被证明是有用的,但它没有带有 CNN 的 NLP 示例。我想检查我的输入数据格式是否正确。

1D Conv 的 Flux 中没有明确的实现,所以我使用的是https://github.com/FluxML/Flux.jl/blob/master/src/layers/conv.jl中 的 Conv 这是一部分解释输入数据格式的文档字符串:

Data should be stored in WHCN order (width, height, # channels, # batches).
In other words, a 100×100 RGB image would be a `100×100×3×1` array,
and a batch of 50 would be a `100×100×3×50` array.

我的格式如下:

1. width: since text in a sentence is 1D, the width is always 1 
2. height: this is the maximum number of tokens allowable in a sentence
3. \# of channels: this is the embedding size
4. \# of batches: the number of sentences in each batch

按照模型动物园中的 MNIST 示例,我有

function make_minibatch(X, Y, idxs)
    X_batch = zeros(1, num_sentences, emb_dims, MAX_LEN)

    function get_sentence_matrix(sentence)
        embeddings = Vector{Array{Float64, 1}}()
        for word in sentence
            embedding = get_embedding(word)
            push!(embeddings, embedding)
        end
        embeddings = hcat(embeddings...)
        return embeddings
    end

    for i in 1:length(idxs)
        X_batch[1, i, :, :] = get_sentence_matrix(X[idxs[i]])
    end
    Y_batch = [Flux.onehot(label+1, 1:2) for label in Y[idxs]]
    return (X_batch, Y_batch)
end

其中 X 是单词数组的数组,get_embedding 函数将嵌入作为数组返回。

X_batch然后是一个Array{Float64,4}。这是正确的方法吗?

4

0 回答 0