Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 预测失败:检查输入时出错:预期密集_输入具有形状(2898),但获得具有形状(1)的数组_Python_Machine Learning_Keras_Google Cloud Platform_Google Ai Platform - Fatal编程技术网

Python 预测失败:检查输入时出错:预期密集_输入具有形状(2898),但获得具有形状(1)的数组

Python 预测失败:检查输入时出错:预期密集_输入具有形状(2898),但获得具有形状(1)的数组,python,machine-learning,keras,google-cloud-platform,google-ai-platform,Python,Machine Learning,Keras,Google Cloud Platform,Google Ai Platform,我使用以下脚本predictor.py从GCP AI平台中托管的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

我使用以下脚本predictor.py从GCP AI平台中托管的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.\u 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)
解决了

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

在此之后,我得到了另一个关于预测输出不可json序列化的错误。我只需将.tolist()添加到return
return output.to\u 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)