简体   繁体   English

加载 Keras 模型并使用它进行预测

[英]Loading Keras Model and making prediction with it

I made a Keras NN model for fake news detection, and I got 89,1 validation accuracy.我为假新闻检测制作了一个 Keras NN 模型,我得到了 89,1 的验证准确率。 I used 50 000 samples for training and 10000 for testing and 2000 for validation.我使用了 50 000 个样本进行训练,10000 个样本用于测试,2000 个样本用于验证。 I have saved that model.我已经保存了那个模型。 Now I want to load that model, load new data that I want to make prediction based on that data.现在我想加载该模型,加载我想基于该数据进行预测的新数据。

import pandas as pd

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, RobustScaler, Normalizer, MinMaxScaler
from sklearn.feature_selection import RFE
from sklearn.metrics import accuracy_score

from tensorflow.python.keras.models import Sequential, load_model
from tensorflow.python.keras.layers import Dense, Dropout, LeakyReLU, Conv2D, LSTM, Flatten

from tensorflow.python.keras import optimizers

from tensorflow.python.keras.regularizers import l2
from tensorflow.python.keras.callbacks import EarlyStopping, ModelCheckpoint

import numpy as np



my_model_1 = load_model("keras fake news acc 89.1.h5")

validation_df = pd.read_csv("validation.csv")
validation_features = validation_df.iloc[:,:-1]
validation_results = validation_df.iloc[:,-1].tolist()

scaler = StandardScaler()
validation_features = scaler.transform(validation_features) #ERROR

The problem is that I get an error:问题是我收到一个错误:

NotFittedError: This StandardScaler instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.

If i use fit_transform on my features, I do not get an error, but I get accuracy of 52%, and that's terrible (because I had 89.1 %).如果我在我的特征上使用fit_transform ,我不会得到错误,但我的准确率为 52%,这太糟糕了(因为我有 89.1%)。

How can I fix this?我怎样才能解决这个问题? Do I need to also load the data that I used for training the model, or I can just load a model and pass the data for prediction?我是否还需要加载用于训练模型的数据,或者我可以只加载模型并传递数据进行预测?

When I trained the model, I used fit_transform for training data and transform for testing data.当我训练模型时,我使用fit_transform训练数据和transform测试数据。 I guess that now, I should use only transform on my data, but Im getting an error我想现在,我应该只对我的数据使用transform ,但我收到一个错误

Save the scaler object while training using either pickle or joblib library.在使用 pickle 或 joblib 库进行训练时保存缩放器对象。 Load this scaler object and then apply transform function on the test data (or real time data).加载此缩放器对象,然后对测试数据(或实时数据)应用变换函数。

You trained the model with a data having a different scaling and trying to do predictions on data with different scaling.您使用具有不同缩放比例的数据训练模型,并尝试对具有不同缩放比例的数据进行预测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM