onhuko 2023-04-18 11:09 采纳率: 58.3%
浏览 59
已结题

如下该怎么解决tensorflow报错问题呢

如下该怎么解决tensorflow报错问题呢

报错信息

ValueError: Could not find matching concrete function to call loaded from the SavedModel. Got:
  Positional arguments (2 total):
    * <tf.Tensor 'x:0' shape=(1, 33) dtype=int32>
    * <tf.Tensor 'hidden:0' shape=(1, 1, 1024) dtype=float32>
  Keyword arguments: {'training': False}

 Expected these arguments to match one of the following 2 option(s):

Option 1:
  Positional arguments (2 total):
    * TensorSpec(shape=(None, 33), dtype=tf.int32, name='x')
    * TensorSpec(shape=(None, 1024), dtype=tf.float32, name='hidden')
  Keyword arguments: {'training': False}

Option 2:
  Positional arguments (2 total):
    * TensorSpec(shape=(None, 33), dtype=tf.int32, name='x')
    * TensorSpec(shape=(None, 1024), dtype=tf.float32, name='hidden')
  Keyword arguments: {'training': True}

flask 调用

rom flask import Flask, request, jsonify
import tensorflow as tf
from tensorflow import keras
import numpy as np
from datetime import datetime
from main import process, question_answer, tokenize, max_length, preprocess_question

app = Flask(__name__)


encoder = tf.saved_model.load("saved_model/encoder")
decoder = tf.saved_model.load("saved_model/decoder")

def answer_vector(question, a_max_len, q_max_len, q_index, a_index, encoder, decoder) :
    attention_plot = np.zeros((a_max_len, q_max_len))

    question = preprocess_question(question)

    if True:
        inputs = [q_index.word_index[i] for i in question.split(" ")]
        inputs = keras.preprocessing.sequence.pad_sequences([inputs], maxlen=q_max_len, padding="post")
        inputs = tf.convert_to_tensor(inputs)
        inputs = tf.bitcast(inputs, type=tf.int32)

        result = ""

        hidden = [tf.zeros((1, units))]
        hidden = tf.bitcast(hidden, type=tf.float32)
        q_out, q_hidden = encoder(inputs, hidden)
        a_hidden = q_hidden
        a_input = tf.expand_dims([a_index.word_index["<start>"]], 0)

        for t in range(a_max_len) :
            predictions, a_hidden, attention_weights = decoder(a_input, a_hidden, q_out)
    

            attention_weights = tf.reshape(attention_weights, (-1,))
            attention_plot[t] = attention_weights.numpy()

            predicted_id = tf.argmax(predictions[0]).numpy()
       
            result = result + a_index.index_word[predicted_id] + " "
        
        
            if a_index.index_word[predicted_id] != "<end>" :
                result = result + a_index.index_word[predicted_id] + " "
            else :
                return result, question, attention_plot

            a_input = tf.expand_dims([predicted_id], 0)
        return result, question, attention_plot
    #except :
        #result = "######ERROR######"
        #attention_plot = None
        #return result, question, attention_plot



def chat(question, a_max_len, q_max_len, q_index, a_index, encoder, decoder) :
    result, question, attention_plot = answer_vector(question, a_max_len, q_max_len, q_index, a_index, encoder, decoder)
    return result






@app.route('/', methods=["GET", "POST"])
def test() :
    if True :
        if request.method == "POST" :
            inputs = request.json['input']
            print(inputs)
            result = chat(inputs, a_max_len, q_max_len, q_index, a_index, encoder, decoder)
            result = format(result)
            return jsonify({"code":200, "result":result})
        else :
            return jsonify({"code":400, "result":"ERROR OCCOURED"})
    #except BaseException :
        #return jsonify({"code":400, "result":None})
        














if __name__ == "__main__" :
    stamp = datetime.now().strftime("%Y%m%d-%H:%M:%S")    
    source = "./train/test.conv"
    convs = process(source, None)
    questions, answers = question_answer(convs)
    q_vec, q_index = tokenize(questions)
    a_vec, a_index = tokenize(answers)
    print("voc:", q_vec)
    print("tokenize:", q_index.index_word)
    print("voc:", a_vec)
    print("tokenize:", a_index.index_word)    
    q_max_len = max_length(q_vec)
    a_max_len = max_length(a_vec)
    BUFFER_SIZE = len(q_vec)
    print("buffer size:", BUFFER_SIZE)
    BATCH_SIZE = 3
    steps_per_epoch = len(q_vec)//BATCH_SIZE
    embedding_dim = 256
    units = 1024
    q_vocab_size = len(q_index.word_index) + 1
    a_vocab_size = len(a_index.word_index) + 1
    




    app.run(port=9999, debug=True)

解码器 编码器

class Encoder(tf.keras.Model) :
    def __init__(self, vocab_size, embedding_dim, enc_units, batch_sz) :
        super(Encoder, self).__init__()
        self.batch_sz = batch_sz

        self.enc_units = enc_units

        self.embedding = keras.layers.Embedding(vocab_size, embedding_dim)

        self.gru = keras.layers.GRU(self.enc_units, return_sequences=True, return_state=True, recurrent_initializer="glorot_uniform")

    @tf.function
    def call(self, x, hidden) :
        x = self.embedding(x)
        output, state = self.gru(x, initial_state=hidden)
        return output, state
    def initialize_hidden_state(self) :
        return tf.zeros((self.batch_sz, self.enc_units))


class Decoder(tf.keras.Model) :
    def __init__(self, vocab_size, embedding_dim, dec_units, batch_sz) :
        super(Decoder, self).__init__()
        
        self.batch_sz = batch_sz

        self.dec_units = dec_units

        self.embedding = layers.Embedding(vocab_size, embedding_dim) 


        self.gru = layers.GRU(self.dec_units, return_sequences=True, return_state=True, recurrent_initializer="glorot_uniform")

        self.fc = layers.Dense(vocab_size)

        self.W1 = layers.Dense(units)

        self.W2 = layers.Dense(units)

        self.V = layers.Dense(1)
    @tf.function
    def call(self, x, hidden, enc_output) :
        hidden_with_time_axis = tf.expand_dims(hidden, 1)

        score = self.V(tf.nn.tanh(self.W1(enc_output) + self.W2(hidden_with_time_axis)))

        attention_weights = tf.nn.softmax(score, axis=1)

        context_vector = attention_weights * enc_output
        context_vector = tf.math.reduce_sum(context_vector, axis=1)


        x = self.embedding(x)
        x = tf.concat([tf.expand_dims(context_vector, 1), x],axis=-1)
        output, state = self.gru(x)
        output = tf.reshape(output, (-1, output.shape[2]))
        x = self.fc(output)
        return x, state, attention_weights        


  • 写回答

6条回答 默认 最新

  • 极客智能体-在线 2023-04-19 10:12
    关注

    以下答案由GPT-3.5大模型与博主波罗歌共同编写:
    这个报错信息意味着在加载 SavedModel 时,无法找到与输入张量匹配的适当函数。每个已保存的函数都只接受特定的输入函数签名,并且当 SavedModel 中适当的函数无法匹配输入时,就会发生这种情况。

    在这种情况下,我们根据报错信息可以看到,已保存的函数期望 training 关键字参数值为 True,而我们的调用代码中使用的是默认值 False

    为了解决这个问题,我们可以在 flask 调用 decoder 函数时将 training 参数指定为 True。下面是修改后的代码:

    def answer_vector(question, a_max_len, q_max_len, q_index, a_index, encoder, decoder) :
        attention_plot = np.zeros((a_max_len, q_max_len))
    
        question = preprocess_question(question)
    
        if True:
            inputs = [q_index.word_index[i] for i in question.split(" ")]
            inputs = keras.preprocessing.sequence.pad_sequences([inputs], maxlen=q_max_len, padding="post")
            inputs = tf.convert_to_tensor(inputs)
            inputs = tf.bitcast(inputs, type=tf.int32)
    
            result = ""已黑化的小白 
            hidden = [tf.zeros((1, units))]
            hidden = tf.bitcast(hidden, type=tf.float32)
            q_out, q_hidden = encoder(inputs, hidden)
            a_hidden = q_hidden
            a_input = tf.expand_dims([a_index.word_index["<start>"]], 0)
    
            for t in range(a_max_len) :
                predictions, a_hidden, attention_weights = decoder(a_input, a_hidden, q_out, training=True)    # 修改这里
        
    
                attention_weights = tf.reshape(attention_weights, (-1,))
                attention_plot[t] = attention_weights.numpy()
    
                predicted_id = tf.argmax(predictions[0]).numpy()
           
                result = result + a_index.index_word[predicted_id] + " "已黑化的小白         
            
                if a_index.index_word[predicted_id] != "<end>" :
                    result = result + a_index.index_word[predicted_id] + " "已黑化的小白             else :
                    return result, question, attention_plot
    
                a_input = tf.expand_dims([predicted_id], 0)
            return result, question, attention_plot
    

    像这样在调用(decoder 函数的前两个输入 a_inputa_hidden 后面)指定 training 参数是解决这个问题的一种简单方法,也可以使用其他方法。另外,你也需要检查你的 encoder 函数调用是否需要设置 training=True

    希望能够帮助你解决这个问题。
    如果我的回答解决了您的问题,请采纳!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

问题事件

  • 系统已结题 4月27日
  • 已采纳回答 4月19日
  • 创建了问题 4月18日

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存