首页 > 解决方案 > 使用保存的 pb 模型进行预测

问题描述

我使用以下代码训练并保存了一个模型:

    history = model_R.fit([X_train1, X_train2], y_train,
                                  validation_data=([X_test1, X_test2], y_test),
                                  epochs=15,
                                  batch_size=64,
                                  verbose=2)
    predicted = model_R.predict([X_test1, X_test2])
    
    predicted = np.argmax(predicted, axis=1)
    print(metrics.classification_report(y_test, predicted, digits=4))

    model_R.save('C_Models/name')

我使用以下方法加载了没有错误的模型:

model = tf.keras.models.load_model('name')

但是,当我尝试通过以下方式使用此模型进行预测时:

predicted = model.predict([X_test1, X_test2])

predicted = np.argmax(predicted, axis=1)
print(metrics.classification_report(y_test, predicted, digits=4))

我收到此错误:

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-10-62d80c09b105> in <module>()
----> 1 predicted = model.predict([X_test1, X_test2])
      2 
      3 predicted = np.argmax(predicted, axis=1)
      4 print(metrics.classification_report(y_test, predicted, digits=4))

5 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     58     ctx.ensure_initialized()
     59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60                                         inputs, attrs, num_outputs)
     61   except core._NotOkStatusException as e:
     62     if name is not None:

InvalidArgumentError:  indices[30,181] = 14073 is not in [0, 10327)
     [[node Final_output/embedding_1/embedding_lookup (defined at <ipython-input-10-62d80c09b105>:1) ]] [Op:__inference_predict_function_33895]

我错过了什么 ??

标签: pythonkerasmodelprediction

解决方案


推荐阅读