我刚刚接触数据科学,对这方面有很多知识,所以我只训练了一个模型,如果不创建类和函数,就可以用普通python正确预测。
但当我像这样加载和预测时
class DeepLearningBot:
modelBlstm = tf.keras.models.load_model(modelPath,
custom_objects={
'elmo': hub.Module("https://tfhub.dev/google/elmo/2", trainable=False),
'tf': tf
})
modelLabel = joblib.load(labelModelPath)
@classmethod
def process(cls, query: str) -> Intent:
intent = DeepLearningBot.__predict(query)
return intent
def __predict(query: str) -> Intent:
with tf.Session() as session:
K.set_session(session)
session.run(tf.global_variables_initializer())
session.run(tf.tables_initializer())
predictionQuery = np.array([query])
predictions = DeepLearningBot.modelBlstm.predict(predictionQuery)
# choose the best/max probability intent
validPrediction = np.argmax(predictions, axis=1)
confidence = np.amax(predictions)*100
# get the exact name instead of position number
predictedLabel = DeepLearningBot.modelLabel.inverse_transform(
validPrediction)
intent = Intent(**{
"name": predictedLabel[0],
"confidence": confidence
})
return intent
这不是正确的预测。但是现在如果我像这样加载和预测
elmo = hub.Module("https://tfhub.dev/google/elmo/2", trainable=False)
with tf.Session() as session:
K.set_session(session)
session.run(tf.global_variables_initializer())
session.run(tf.tables_initializer())
model_elmo = tf.keras.models.load_model(model_path)
predicts = model_elmo.predict(predictionQuery)
它工作完美,预测意图准确。
在每个查询的第二种方法中,它在会话中加载我想要忽略的模型。
你知道我该怎么解决这个问题吗?
暂无答案!
目前还没有任何答案,快来回答吧!