且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Keras 嵌入层掩码.为什么 input_dim 需要是 |vocabulary|+ 2?

更新时间:2023-12-01 23:38:10

我认为那里的文档有点误导.在正常情况下,您将 n 输入数据索引 [0, 1, 2, ..., n-1] 映射到向量,因此您的 input_dim 应该与您拥有的元素一样多

I believe the docs are a bit misleading there. In the normal case you are mapping your n input data indices [0, 1, 2, ..., n-1] to vectors, so your input_dim should be as many elements as you have

input_dim = len(vocabulary_indices)

一种等效的(但有点令人困惑)的表达方式,以及文档的方式,就是说

An equivalent (but slightly confusing) way to say this, and the way the docs do, is to say

1 + 输入数据中出现的最大整数索引.

1 + maximum integer index occurring in the input data.

input_dim = max(vocabulary_indices) + 1

如果您启用屏蔽,值 0 将被区别对待,因此您将 n 索引增加 1:[0, 1, 2, ..., n-1, n],因此你需要

If you enable masking, value 0 is treated differently, so you increment your n indices by one: [0, 1, 2, ..., n-1, n], thus you need

input_dim = len(vocabulary_indices) + 1

或者替代

input_dim = max(vocabulary_indices) + 2

文档在这里变得特别混乱

The docs become especially confusing here as they say

(input_dim 应该等于 |vocabulary| + 2)

(input_dim should equal |vocabulary| + 2)

我将 |x| 解释为集合的基数(相当于 len(x)),但作者似乎是指

where I would interpret |x| as the cardinality of a set (equivalent to len(x)), but the authors seem to mean

2 + 输入数据中出现的最大整数索引.

2 + maximum integer index occurring in the input data.