簡體   English   中英

預測失敗:檢查輸入時出錯:預期dense_input 具有形狀(2898,)但得到的數組具有形狀(1,)

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

我正在使用以下腳本 predictor.py 以從 GCP AI 平台中托管的 Keras model 獲取預測。

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,)

問題似乎是由於我在嘗試進行實際預測時輸入數據的維度,在 output = self._model.predict([vectors]) 行中。 model 需要一個形狀為 (2898, ) 的向量

我覺得這很奇怪......因為當我打印矢量的形狀和尺寸時,我得到以下信息

This is the shape
(1, 2898)

This is the dim number
2

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

所以尺寸和形狀都很好,它應該真的可以工作......

此外,我進行了測試以獲取本地存儲的 model 的預測,它工作正常。 這是測試文件:

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._model.predict([vectors])

之后,我收到另一個關於預測的 output 不是 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM