使用Python / Keras训练长期短期记忆网络时出现InvalidArgumentError(音频分类)

时间:2018-11-24 09:03:13

标签: python tensorflow keras neural-network rnn

我是Keras的新手,我正在尝试建立一个递归神经网络来对音频文件进行分类。

在培训期间,我收到了InvalidArgumentError: indices[28,0] = -711 is not in [0, 20000)

我找到了很多有关此错误的话题,但老实说,我不理解我要传递给网络的参数必须进行哪些更改才能帮助它管理培训中的负值数组。

下面的代码:

from keras.models import Sequential
from keras.layers import Dense, Dropout, Embedding, LSTM, Bidirectional

max_features = 20000
maxlen = 40
batch_size = 32

model = Sequential()
model.add(Embedding(max_features, 128, input_length=maxlen))
model.add(Bidirectional(LSTM(64)))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))

# try using different optimizers and different optimizer configs
model.compile('adam', 'binary_crossentropy', metrics=['accuracy'])

print('Train...')
model.fit(X_train, y_train,
          batch_size=batch_size,
          epochs=4,
          validation_data=[X_test, y_test])

以下错误:

Train...
Train on 964 samples, validate on 476 samples
Epoch 1/4
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-22-3452d23cb8b5> in <module>()
     12           batch_size=batch_size,
     13           epochs=4,
---> 14           validation_data=[X_test, y_test])

/usr/local/lib/python3.6/dist-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,

/usr/local/lib/python3.6/dist-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):

/usr/local/lib/python3.6/dist-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):

/usr/local/lib/python3.6/dist-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 

/usr/local/lib/python3.6/dist-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)

/usr/local/lib/python3.6/dist-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[28,0] = -711 is not in [0, 20000)
     [[{{node embedding_3/embedding_lookup}} = GatherV2[Taxis=DT_INT32, Tindices=DT_INT32, Tparams=DT_FLOAT, _class=["loc:@training_1/Adam/Assign_2"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](embedding_3/embeddings/read, embedding_3/Cast, training_1/Adam/gradients/embedding_3/embedding_lookup_grad/concat/axis)]]

EDIT1 :X_train是下面的float32数组

array([[-5.79938449e+02,  6.63875936e+01, -6.75054944e+00, ...,
        -2.89458464e+00, -2.30009868e+00, -2.34216322e+00],
       [-3.38924973e+02,  1.60668197e+01, -5.39871140e+01, ...,
         1.27180395e+00,  4.28090614e+00,  2.01538667e+00],
       [-5.53199739e+02,  3.45314936e+01, -1.68711443e+01, ...,
        -9.47345310e-02, -1.04780706e-02,  1.69060756e-01],
       ...,
       [-5.91902354e+02,  6.14329122e+01,  1.43761675e+00, ...,
        -4.38644438e+00, -3.67977820e+00, -1.89899207e+00],
       [-7.04889969e+02,  6.24931510e+01,  1.90338300e+01, ...,
        -1.47540089e+00, -1.75498741e+00, -4.55713837e-01],
       [-8.24296641e+02,  7.43124586e+01,  1.43319513e+01, ...,
        -7.60749297e-01, -1.05324700e+00, -8.54044186e-01]])

0 个答案:

没有答案