Tensorflow:使用经过训练的模型进行预测时出现InvalidArgumentError

时间:2019-12-17 08:59:04

标签: python tensorflow keras predict invalid-argument

我创建了一个CNN模型,以使用API​​序列对恶意软件进行分类。我训练了现在,我想使用我的模型预测恶意软件。但是我遇到了一个错误,该错误说存在一个InvalidArgumentError:[0,295)中没有索引[126,5137] = -2147483648。 谁能帮助我解决这个问题?

train_labels=np.array(train_df.label)
train_seq = pad_sequences(train_df.seq.values, maxlen = 6000)
test_seq = pad_sequences(test_df.seq.values, maxlen = 6000)

skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)

max_len = 6000
max_cnt = 295                    
embed_size = 256
num_filters = 64
kernel_size = [2,4,6,8,10,12,14]
conv_action = 'relu'
mask_zero = False
TRAIN = True

import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
meta_train = np.zeros(shape = (len(train_seq),8))
meta_test = np.zeros(shape = (len(test_seq),8))
FLAG = False
for i,(tr_ind,te_ind) in enumerate(skf.split(train_seq,train_labels)):
    print('FOLD: '.format(i))
    print(len(te_ind),len(tr_ind))
    model = TextCNN(max_len,max_cnt,embed_size,num_filters,kernel_size,conv_action,mask_zero)
    model_name = 'benchmark_textcnn_fold_'+str(i)
    X_train,X_train_label = train_seq[tr_ind],train_labels[tr_ind]
    X_val,X_val_label = train_seq[te_ind],train_labels[te_ind]

    model = TextCNN(max_len,max_cnt,embed_size,
            num_filters,kernel_size,
            conv_action,
            mask_zero)

    model_save_path = '../model_weight_final/%s_%s.hdf5'%(model_name,embed_size)
    early_stopping =EarlyStopping(monitor='val_loss', patience=3)
    model_checkpoint = ModelCheckpoint(model_save_path, save_best_only=True, save_weights_only=True)
    if TRAIN and FLAG:
        model.fit(X_train,X_train_label,
                  validation_data=(X_val,X_val_label),
                  epochs=100,batch_size=64,
                  shuffle=True,
                  callbacks=[early_stopping,model_checkpoint]
                 )
        model.load_weights(model_save_path)
    pred_val = model.predict(X_val,batch_size=128,verbose=1)
    pred_test = model.predict(test_seq,batch_size=128,verbose=1)

    meta_train[te_ind] = pred_val
    meta_test += pred_test
K.clear_session()
meta_test /= 5.0
FOLD: 
2780 11107
2780/2780 [==============================] - ETA: 3: - ETA: 4: - ETA: 4: - ETA: 4: - ETA: 3: - ETA: 3: - ETA: 3: - ETA: 3: - ETA: 3: - ETA: 2: - ETA: 2: - ETA: 2: - ETA: 2: - ETA: 1: - ETA: 1: - ETA: 1: - ETA: 1: - ETA: 55s - ETA: 40 - ETA: 25 - ETA: 10 - 327s 118ms/step
 1664/12955 [==>...........................] - ETA: 24:2 - ETA: 24:1 - ETA: 23:5 - ETA: 23:4 - ETA: 23:3 - ETA: 23:1 - ETA: 23:0 - ETA: 22:4 - ETA: 22:3 - ETA: 22:1 - ETA: 22:0 - ETA: 21:4 - ETA: 21:33
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-33-d1f608365da1> in <module>
     29         model.load_weights(model_save_path)
     30     pred_val = model.predict(X_val,batch_size=128,verbose=1)
---> 31     pred_test = model.predict(test_seq,batch_size=128,verbose=1)
     32 
     33     meta_train[te_ind] = pred_val

c:\users\xie07\appdata\local\programs\python\python37\lib\site-packages\keras\engine\training.py in predict(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)
   1460                                             verbose=verbose,
   1461                                             steps=steps,
-> 1462                                             callbacks=callbacks)
   1463 
   1464     def train_on_batch(self, x, y,

c:\users\xie07\appdata\local\programs\python\python37\lib\site-packages\keras\engine\training_arrays.py in predict_loop(model, f, ins, batch_size, verbose, steps, callbacks)
    322             batch_logs = {'batch': batch_index, 'size': len(batch_ids)}
    323             callbacks._call_batch_hook('predict', 'begin', batch_index, batch_logs)
--> 324             batch_outs = f(ins_batch)
    325             batch_outs = to_list(batch_outs)
    326             if batch_index == 0:

c:\users\xie07\appdata\local\programs\python\python37\lib\site-packages\tensorflow_core\python\keras\backend.py in __call__(self, inputs)
   3738         value = math_ops.cast(value, tensor.dtype)
   3739       converted_inputs.append(value)
-> 3740     outputs = self._graph_fn(*converted_inputs)
   3741 
   3742     # EagerTensor.numpy() will often make a copy to ensure memory safety.

c:\users\xie07\appdata\local\programs\python\python37\lib\site-packages\tensorflow_core\python\eager\function.py in __call__(self, *args, **kwargs)
   1079       TypeError: For invalid positional/keyword argument combinations.
   1080     """
-> 1081     return self._call_impl(args, kwargs)
   1082 
   1083   def _call_impl(self, args, kwargs, cancellation_manager=None):

c:\users\xie07\appdata\local\programs\python\python37\lib\site-packages\tensorflow_core\python\eager\function.py in _call_impl(self, args, kwargs, cancellation_manager)
   1119       raise TypeError("Keyword arguments {} unknown. Expected {}.".format(
   1120           list(kwargs.keys()), list(self._arg_keywords)))
-> 1121     return self._call_flat(args, self.captured_inputs, cancellation_manager)
   1122 
   1123   def _filtered_call(self, args, kwargs):

c:\users\xie07\appdata\local\programs\python\python37\lib\site-packages\tensorflow_core\python\eager\function.py in _call_flat(self, args, captured_inputs, cancellation_manager)
   1222     if executing_eagerly:
   1223       flat_outputs = forward_function.call(
-> 1224           ctx, args, cancellation_manager=cancellation_manager)
   1225     else:
   1226       gradient_name = self._delayed_rewrite_functions.register()

c:\users\xie07\appdata\local\programs\python\python37\lib\site-packages\tensorflow_core\python\eager\function.py in call(self, ctx, args, cancellation_manager)
    509               inputs=args,
    510               attrs=("executor_type", executor_type, "config_proto", config),
--> 511               ctx=ctx)
    512         else:
    513           outputs = execute.execute_with_cancellation(

c:\users\xie07\appdata\local\programs\python\python37\lib\site-packages\tensorflow_core\python\eager\execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     65     else:
     66       message = e.message
---> 67     six.raise_from(core._status_to_exception(e.code, message), None)
     68   except TypeError as e:
     69     keras_symbolic_tensors = [

c:\users\xie07\appdata\local\programs\python\python37\lib\site-packages\six.py in raise_from(value, from_value)

InvalidArgumentError:  indices[126,5137] = -2147483648 is not in [0, 295)
     [[node embedding_4/embedding_lookup (defined at c:\users\xie07\appdata\local\programs\python\python37\lib\site-packages\tensorflow_core\python\framework\ops.py:1751) ]] [Op:__inference_keras_scratch_graph_2602]

Function call stack:
keras_scratch_graph

0 个答案:

没有答案