keras和python上的InvalidArgumentError

时间:2019-06-12 20:15:34

标签: python tensorflow keras deep-learning

当我尝试训练网络时(在python 3.7中)出现此错误,我从python深度学习书(chollet的书第261页,第7章)中获得了此练习。我试图在text_vocabulary_size上加+1,但它不起作用U.U,这是我的代码和错误

代码:

from keras.models import Model
from keras import layers
from keras import Input
text_vocabulary_size = 10000
question_vocabulary_size = 10000
answer_vocabulary_size = 500
text_input = Input(shape=(None,), dtype='int32', name='text')
embedded_text = layers.Embedding(64, text_vocabulary_size)(text_input)
encoded_text = layers.LSTM(32)(embedded_text)
question_input = Input(shape=(None,), dtype='int32', name='question')
embedded_question = layers.Embedding(32, question_vocabulary_size)(question_input)
encoded_question = layers.LSTM(16)(embedded_question)
concatenated = layers.concatenate([encoded_text, encoded_question], axis=-1)
answer = layers.Dense(answer_vocabulary_size, activation='softmax')(concatenated)
model = Model([text_input, question_input], answer)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['acc'])


import numpy as np
num_samples = 1000
max_length = 100
text = np.random.randint(1, text_vocabulary_size, size=(num_samples, max_length))

question = np.random.randint(1, question_vocabulary_size, size=(num_samples, max_length))
answers = np.random.randint(0, 1, size=(num_samples, answer_vocabulary_size))
model.fit([text, question], answers, epochs=10, batch_size=128)
model.fit({'text': text, 'question': question}, answers, epochs=10, batch_size=128)

错误:

InvalidArgumentError: indices[124,0] = 190 is not in [0, 64)
     [[{{node embedding_33/embedding_lookup}}]]

这是网络的摘要,希望对您有所帮助

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
text (InputLayer)               (None, None)         0                                            
__________________________________________________________________________________________________
question (InputLayer)           (None, None)         0                                            
__________________________________________________________________________________________________
embedding_33 (Embedding)        (None, None, 10000)  640000      text[0][0]                       
__________________________________________________________________________________________________
embedding_34 (Embedding)        (None, None, 10000)  320000      question[0][0]                   
__________________________________________________________________________________________________
lstm_33 (LSTM)                  (None, 32)           1284224     embedding_33[0][0]               
__________________________________________________________________________________________________
lstm_34 (LSTM)                  (None, 16)           641088      embedding_34[0][0]               
__________________________________________________________________________________________________
concatenate_17 (Concatenate)    (None, 48)           0           lstm_33[0][0]                    
                                                                 lstm_34[0][0]                    
__________________________________________________________________________________________________
dense_17 (Dense)                (None, 500)          24500       concatenate_17[0][0]             
==================================================================================================
Total params: 2,909,812
Trainable params: 2,909,812
Non-trainable params: 0

和追溯

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-52-556d838d7025> in <module>
      6 question = np.random.randint(1, question_vocabulary_size, size=(num_samples, max_length))
      7 answers = np.random.randint(0, 1, size=(num_samples, answer_vocabulary_size))
----> 8 model.fit([text, question], answers, epochs=10, batch_size=128)
      9 model.fit({'text': text, 'question': question}, answers, epochs=10, batch_size=128)

~\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
   1037                                         initial_epoch=initial_epoch,
   1038                                         steps_per_epoch=steps_per_epoch,
-> 1039                                         validation_steps=validation_steps)
   1040 
   1041     def evaluate(self, x=None, y=None,

~\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training_arrays.py in fit_loop(model, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps)
    197                     ins_batch[i] = ins_batch[i].toarray()
    198 
--> 199                 outs = f(ins_batch)
    200                 outs = to_list(outs)
    201                 for l, o in zip(out_labels, outs):

~\Anaconda3\envs\tensorflow\lib\site-packages\keras\backend\tensorflow_backend.py in __call__(self, inputs)
   2713                 return self._legacy_call(inputs)
   2714 
-> 2715             return self._call(inputs)
   2716         else:
   2717             if py_any(is_tensor(x) for x in inputs):

~\Anaconda3\envs\tensorflow\lib\site-packages\keras\backend\tensorflow_backend.py in _call(self, inputs)
   2673             fetched = self._callable_fn(*array_vals, run_metadata=self.run_metadata)
   2674         else:
-> 2675             fetched = self._callable_fn(*array_vals)
   2676         return fetched[:len(self.outputs)]
   2677 

~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py in __call__(self, *args, **kwargs)
   1437           ret = tf_session.TF_SessionRunCallable(
   1438               self._session._session, self._handle, args, status,
-> 1439               run_metadata_ptr)
   1440         if run_metadata:
   1441           proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
    526             None, None,
    527             compat.as_text(c_api.TF_Message(self.status.status)),
--> 528             c_api.TF_GetCode(self.status.status))
    529     # Delete the underlying status object from memory otherwise it stays alive
    530     # as there is a reference to status from this from the traceback due to

InvalidArgumentError: indices[124,0] = 190 is not in [0, 64)
     [[{{node embedding_33/embedding_lookup}}]]

先谢谢大家

1 个答案:

答案 0 :(得分:2)

在您的两个嵌入中:

embedded_text = layers.Embedding(64, text_vocabulary_size)(text_input)
embedded_question = layers.Embedding(32, question_vocabulary_size)(question_input)

我认为您将参数以相反的顺序放置,第一个参数是词汇量,第二个参数是输出嵌入量,所以应该是:

embedded_text = layers.Embedding(text_vocabulary_size, 64)(text_input)
embedded_question = layers.Embedding(question_vocabulary_size, 32)(question_input)

您随时可以在keras documentation中进行检查。