训练 CBOW 模型时 Keras 中的输入形状错误

Error with input shape in Keras while training CBOW model

我正在为词嵌入训练一个连续的词碗模型,其中每个单热向量的形状都是一个形状为 (V, 1) 的列向量。我正在使用生成器根据语料库生成训练示例和标签,但输入形状有误。

(这里V=5778)

这是我的代码:

def windows(words, C):
    i = C
    while len(words) - i > C:
        center = words[i]
        context_words = words[i-C:i] + words[i+1:i+C+1]
        i += 1
        yield context_words, center

def one_hot_rep(word, word_to_index, V):
    vec = np.zeros((V, 1))
    vec[word_to_index[word]] = 1
    return vec

def context_to_one_hot(words, word_to_index, V):
    arr = [one_hot_rep(w, word_to_index, V) for w in words]
    return np.mean(arr, axis=0)
def get_training_examples(words, C, words_to_index, V):
    for context_words, center_word in windows(words, C):
        yield context_to_one_hot(context_words, words_to_index, V), one_hot_rep(center_word, words_to_index, V)
V = len(vocab)
N = 50

w2i, i2w = build_dict(vocab)

model = keras.models.Sequential([
    keras.layers.Flatten(input_shape=(V, )),
    keras.layers.Dense(units=N, activation='relu'),
    keras.layers.Dense(units=V, activation='softmax')
])

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

model.fit_generator(get_training_examples(data, 2, w2i, V), epochs=5, steps_per_epoch=20)

flatten layer 得到至少 3 维的 numpy 数组,但你给它 2 维的

我弄清楚了导致错误的原因。该模型期望 input_shape = (None, V) 其中 None 在训练开始时为 Keras 保存 batch_size 但我发送的是形状数组 (1, V) 当作为批次发送时会得到一个额外的第一维,例如 (128, 1, V) 正在发送,这与预期的 input_shape.

冲突