加载Keras模型并进行预测 [英] Loading Keras Model and making prediction with it

查看:636
本文介绍了加载Keras模型并进行预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为假新闻检测制作了Keras NN模型,并获得了89,1的验证准确性.我使用了50000个样本进行训练,使用10000个进行测试,并使用2000个进行验证.我已经保存了该模型. 现在,我要加载该模型,并加载要基于该数据进行预测的新数据.

I made a Keras NN model for fake news detection, and I got 89,1 validation accuracy. I used 50 000 samples for training and 10000 for testing and 2000 for validation. 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.

如果我在功能上使用fit_transform,则不会出现错误,但是我的准确度为52%,这太糟糕了(因为我有89.1%).

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 %).

我该如何解决? 我是否还需要加载用于训练模型的数据,或者仅加载模型并传递数据以进行预测?

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?

训练模型时,我将fit_transform用于训练数据,将transform用于测试数据.我想现在,我应该只在数据上使用transform,但是我收到了错误消息

When I trained the model, I used fit_transform for training data and transform for testing data. I guess that now, I should use only transform on my data, but Im getting an error

推荐答案

在使用pickle或joblib库进行训练时,保存缩放比例对象. 加载该缩放器对象,然后将变换函数应用于测试数据(或实时数据).

Save the scaler object while training using either pickle or joblib library. 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.

这篇关于加载Keras模型并进行预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆