Я пытаюсь предсказать следующее слово с CBOW. Вот модель. Ошибка происходит на шаге model.predict
model = Sequential()
model.add(Embedding(input_dim=num_embeddings, output_dim=embedding_dim, weights=[pretrained_weights]))
model.add(Lambda(lambda x: K.mean(x, axis=1), output_shape=(embedding_dim,)))
model.add(Dense(embedding_dim, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adadelta')
def generate_next(text, num_generated=10):
print(type(text))
print('Generating the next...')
pred_weights = np.zeros([len(text.split()), 50])
for t, word in enumerate(text.split()):
print(word)
pred_weights[t] = word2idx(word.lower())
for i in range(num_generated):
prediction = model.predict(x=pred_weights)
idx = sample(prediction[-1], temperature=0.7)
pred_weights.append(idx)
return ' '.join(idx2word(idx) for idx in word_idxs)
Ошибка:
2020-03-14 14:28:43.748512: W tensorflow/core/common_runtime/base_collective_executor.cc:217] BaseCollectiveExecutor::StartAbort Invalid argument: indices[0,25] = -1 is not in [0, 400000)
[[{{node embedding_1/embedding_lookup}}]]
Traceback (most recent call last):
File "cbow.py", line 107, in <module>
sample = generate_next(text)
File "cbow.py", line 91, in generate_next
prediction = model.predict(x=pred_weights)
File "/home/guzel/Anaconda3-2019.10/lib/python3.7/site-packages/keras/engine/training.py", line 1462, in predict
callbacks=callbacks)
File "/home/guzel/Anaconda3-2019.10/lib/python3.7/site-packages/keras/engine/training_arrays.py", line 324, in predict_loop
batch_outs = f(ins_batch)
File "/home/guzel/Anaconda3-2019.10/lib/python3.7/site-packages/tensorflow_core/python/keras/backend.py", line 3727, in __call__
outputs = self._graph_fn(*converted_inputs)
File "/home/guzel/Anaconda3-2019.10/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py", line 1551, in __call__
return self._call_impl(args, kwargs)
File "/home/guzel/Anaconda3-2019.10/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py", line 1591, in _call_impl
return self._call_flat(args, self.captured_inputs, cancellation_manager)
File "/home/guzel/Anaconda3-2019.10/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py", line 1692, in _call_flat
ctx, args, cancellation_manager=cancellation_manager))
File "/home/guzel/Anaconda3-2019.10/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py", line 545, in call
ctx=ctx)
File "/home/guzel/Anaconda3-2019.10/lib/python3.7/site-packages/tensorflow_core/python/eager/execute.py", line 67, in quick_execute
six.raise_from(core._status_to_exception(e.code, message), None)
File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError: indices[0,25] = -1 is not in [0, 400000)
[[node embedding_1/embedding_lookup (defined at /home/guzel/Anaconda3-2019.10/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:3009) ]] [Op:__inference_keras_scratch_graph_126]
Function call stack:
keras_scratch_graph
pretrained_weights.shape is (400000,50) - используется для встраивания слоя
pred_weights.shape is (6,50) - входные данные
Возможно, причина здесь:
print(np.where(pretrained_weights == -2))
дает
(array([ 18807, 56773, 68753, 116154, 117857, 122205, 151525, 176865,
218782, 235726, 266661, 271962, 272754, 313663, 326581, 342410,
353057, 372100, 391140]), array([16, 13, 16, 34, 26, 13, 41, 30, 28, 30, 30, 45, 30, 1, 34, 30, 30,
30, 30]))
Но
print(np.where(pred_weights == -2))
т
(array([], dtype=int64), array([], dtype=int64))