【问题标题】:Prediction failed: Error when checking input: expected dense_input to have shape (2898,) but got array with shape (1,)预测失败:检查输入时出错:预期dense_input 具有形状(2898,)但得到的数组具有形状(1,)
【发布时间】:2021-06-08 12:34:45
【问题描述】:

我正在使用以下脚本 predictor.py 以从托管在 GCP AI Platform 中的 Keras 模型获取预测。

import os
import pickle
import tensorflow as tf
import numpy as np
import logging

class MyPredictor(object):

    def __init__(self, model, bow_model):
        self._model = model
        self._bow_model = bow_model

    def predict(self, instances, **kwargs):
      
        vectors = self.embedding([instances])

        vectors = vectors.tolist()

        output = self._model.predict(vectors)

        return output

    def embedding(self, statement):
        vector = self._bow_model.transform(statement).toarray()
        #vector = vector.to_list()
        return vector


    @classmethod
    def from_path(cls, model_dir):

        model_path = os.path.join(model_dir, 'model.h5')
        model = tf.keras.models.load_model(model_path, compile = False)

        preprocessor_path = os.path.join(model_dir, 'bow.pkl')
        with open(preprocessor_path, 'rb') as f:
            bow_model = pickle.load(f)


        return cls(model, bow_model)

但是我得到了

Prediction failed: Error when checking input: expected dense_input to have shape (2898,) but got array with shape (1,)

问题似乎是由于我在尝试进行实际预测时输入数据的维度,在行输出 = self._model.predict([vectors]) 中。该模型需要一个形状为 (2898, ) 的向量

我觉得这很奇怪......因为当我打印矢量的形状和尺寸时,我得到以下内容

This is the shape
(1, 2898)

This is the dim number
2

This is the vector 
[[0 0 0 ... 0 0 0]]

所以尺寸和形状都很好,应该真的可以工作......

此外,我做了一个测试,将模型的预测结果存储在本地,它运行良好。这是测试文件:

import os
import pickle
import tensorflow as tf
import numpy as np

class MyPredictor(object):

    def __init__(self, model, bow_model):
        self._model = model
        self._bow_model = bow_model

    def predict(self, instances, **kwargs):

        print("These are the instances ", instances)

        vector = self.embedding([instances])

        output = self._model.predict(vector)

        return output

    def embedding(self, statement):
        vector = self._bow_model.transform(statement).toarray()
        #vector = vector.to_list()
        return vector



model_path = 'model.h5'
model = tf.keras.models.load_model(model_path, compile = False)

preprocessor_path = 'bow.pkl'
with open(preprocessor_path, 'rb') as f:
    bow_model = pickle.load(f)


instances = 'test'

predictor = MyPredictor(model, bow_model)

outputs = predictor.predict(instances)

print(outputs)

【问题讨论】:

    标签: python machine-learning keras google-cloud-platform google-ai-platform


    【解决方案1】:

    解决了!

    这就像在output = self._model.predict([vectors])这一行添加一组括号一样愚蠢

    之后,我收到另一个错误,即预测的输出不是 json 可序列化的。我只需将 .tolist() 添加到返回 return output.to_list()

    即可解决这个问题
    import os
    import pickle
    import tensorflow as tf
    import numpy as np
    import logging
    
    class MyPredictor(object):
    
        def __init__(self, model, bow_model):
            self._model = model
            self._bow_model = bow_model
    
        def predict(self, instances, **kwargs):
          
            vectors = self.embedding([instances])
    
            vectors = vectors.tolist()
    
            output = self._model.predict([vectors])
    
            return output.to_list()
    
        def embedding(self, statement):
            vector = self._bow_model.transform(statement).toarray()
            #vector = vector.to_list()
            return vector
    
    
        @classmethod
        def from_path(cls, model_dir):
    
            model_path = os.path.join(model_dir, 'model.h5')
            model = tf.keras.models.load_model(model_path, compile = False)
    
            preprocessor_path = os.path.join(model_dir, 'bow.pkl')
            with open(preprocessor_path, 'rb') as f:
                bow_model = pickle.load(f)
    
    
            return cls(model, bow_model)
    
    

    【讨论】:

      猜你喜欢
      • 2020-12-05
      • 2020-09-01
      • 2020-05-17
      • 1970-01-01
      • 1970-01-01
      • 2019-02-12
      • 1970-01-01
      • 2021-02-26
      • 2022-11-03
      相关资源
      最近更新 更多