如下该怎么解决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