为TensorFlow.js保存TensorFlow模型

为TensorFlow.js保存TensorFlow模型,tensorflow,tensorflow.js,Tensorflow,Tensorflow.js,我在TensorFlow中培训了一个聊天机器人,并希望保存该模型,以便使用TensorFlow.js将其部署到web。我有以下几点 checkpoint = "./chatbot_weights.ckpt" session = tf.InteractiveSession() session.run(tf.global_variables_initializer()) saver = tf.train.Saver() saver.restore(session, checkpoint) # C

我在TensorFlow中培训了一个聊天机器人,并希望保存该模型,以便使用TensorFlow.js将其部署到web。我有以下几点

checkpoint = "./chatbot_weights.ckpt"
session = tf.InteractiveSession()
session.run(tf.global_variables_initializer())
saver = tf.train.Saver()
saver.restore(session, checkpoint)


# Converting the questions from strings to lists of encoding integers
def convert_string2int(question, word2int):
    question = clean_text(question)
    return [word2int.get(word, word2int['<OUT>']) for word in question.split()]

# Setting up the chat
while(True):
    question = input("You: ")
    if question == 'Goodbye':
        break
    question = convert_string2int(question, questionswords2int)
    question = question + [questionswords2int['<PAD>']] * (25 - len(question))
    fake_batch = np.zeros((batch_size, 25))
    fake_batch[0] = question
    predicted_answer = session.run(test_predictions, {inputs: fake_batch, keep_prob: 0.5})[0]
    answer = ''
    for i in np.argmax(predicted_answer, 1):
        if answersints2word[i] == 'i':
            token = ' I'
        elif answersints2word[i] == '<EOS>':
            token = '.'
        elif answersints2word[i] == '<OUT>':
            token = 'out'
        else:
            token = ' ' + answersints2word[i]
        answer += token
        if token == '.':
            break
    print('ChatBot: ' + answer)
checkpoint=“./chatbot\u weights.ckpt”
session=tf.InteractiveSession()
session.run(tf.global\u variables\u initializer())
saver=tf.train.saver()
saver.restore(会话、检查点)
#将问题从字符串转换为编码整数列表
def convert_string2int(问题,单词2INT):
问题=干净的文本(问题)
返回[word2int.get(word,word2int['']),用于有问题的word.split()]
#设置聊天室
虽然(正确):
问题=输入(“您:”)
如果问题==‘再见’:
打破
问题=转换字符串2int(问题,问题2int)
问题=问题+[问题2输入['']*(25-列(问题))
假批次=np.零((批次大小,25))
假批[0]=问题
predicted_answer=session.run(test_predicts,{inputs:fake_batch,keep_prob:0.5})[0]
答案=“”
对于np.argmax中的i(预测答案,1):
如果回答两个字[i]=“i”:
令牌='I'
elif回答了两个字[i]='':
标记='。'
elif回答了两个字[i]='':
令牌='out'
其他:
token=''+回答2字[i]
应答+=令牌
如果令牌=='。:
打破
打印('ChatBot:'+回答)
它提供了以下文件(我可以在控制台中测试bot):

但是在文档中,它说我应该使用SaveModel或freezed,这里有人能帮忙吗?我不知道如何实施。谢谢

要在浏览器中部署模型,首先需要使用转换。您可以查看以下内容以了解如何继续

要成功转换模型,浏览器中应已支持模型中使用的所有操作。以下是目前支持的所有老年退休金计划

转换模型并拥有模型文件和权重后,可以使用以下方式加载模型:

const model=wait loadFrozenModel(model\u URL,WEIGHTS\u URL);
...
execute({input:the_model}的_input_);