如何在Keras中使用数组列表指定一个嵌入层的输入?

问题描述 投票:1回答:1

我正在尝试做一些单词级文本生成并遇到以下问题:

我的输入如下:

   tokenized_seq = [[w2v_model.wv.vocab[word].index for word in w2v_data[i]] for i in range(len(w2v_data))]
   x_seq = []
   y_seq = []

   for seq in tokenized_seq:
      x_seq.append(seq[:-1])
      y_seq.append([seq[-1]])

所以,我沿着序列(使用word2vec编码的单词)和固定大小的滚动窗口(tokenized _seq是具有固定长度的序列列表)。

看一下这个例子:

代码块:

print(x_seq[0], '->', y_seq[0])  
print(' '.join([w2v_model.wv.index2word[i] for i in x_seq[0]]), '->', w2v_model.wv.index2word[y_seq[0].pop()]) 

输出:

[608, 1661, 1, 4260, 1, 3, 2978, 741, 0, 153, 740, 1, 12004] -> [109]
часть первая . i . — eh bien , mon prince . gênes -> et

那么,我正在尝试将以上所有内容输入到嵌入层。

model = Sequential()
model.add(Embedding(input_dim=vocab_size,
                    output_dim=emdedding_size,
                    input_length=avg_sent_len-1,
                    weights=[predtrained_weights]
                    trainable=False))

model.add(Bidirectional(LSTM(units=128)))

model.add(Dense(units=vocab_size, activation='softmax'))

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

history = model.fit(x_seq, y_seq,
                   epochs=10,
                   batch_size=128,
                   validation_split=0.2,
                   verbose=2)

嵌入参数是:

predtrained_weights = w2v_model.wv.vectors
vocab_size, emdedding_size = w2v_model.wv.vectors.shape

qazxsw poi是qazxsw poi中每个序列的len

该模型编译得很好,但在拟合时我得到以下错误:

avg_sent_len

(31412,)是x_seq 223396是ValueError: Error when checking target: expected dense_40 to have shape (31412,) but got array with shape (223396,) vocab_size长度(输入序列的数量)那么,有人可以帮助我吗?

python tensorflow keras word2vec word-embedding
1个回答
0
投票

你输入x_seq应该是一个numpy数组形状y_seq。尝试添加x_seq

© www.soinside.com 2019 - 2024. All rights reserved.