基于huggingface transformers快速部署tensorflow serving

1.保存模型

from transformers import TFBertForSequenceClassification

model = TFBertForSequenceClassification.from_pretrained("nateraw/bert-base-uncased-imdb", from_pt=True)
# the saved_model parameter is a flag to create a SavedModel version of the model in same time than the h5 weights
model.save_pretrained("my_model", saved_model=True)

2.使用docker拉取tf-serving并制作serving

 3.使用requests调用Sevring

from transformers import BertTokenizerFast, BertConfig
import requests
import json
import numpy as np

sentence = "I love the new TensorFlow update in transformers."

# Load the corresponding tokenizer of our SavedModel
tokenizer = BertTokenizerFast.from_pretrained("nateraw/bert-base-uncased-imdb")

# Load the model config of our SavedModel
config = BertConfig.from_pretrained("nateraw/bert-base-uncased-imdb")

# Tokenize the sentence
batch = tokenizer(sentence)

# Convert the batch into a proper dict
batch = dict(batch)

# Put the example into a list of size 1, that corresponds to the batch size
batch = [batch]

# The REST API needs a JSON that contains the key instances to declare the examples to process
input_data = {"instances": batch}

# Query the REST API, the path corresponds to http://host:port/model_version/models_root_folder/model_name:method
r = requests.post("http://localhost:8501/v1/models/bert:predict", data=json.dumps(input_data))

# Parse the JSON result. The results are contained in a list with a root key called "predictions"
# and as there is only one example, takes the first element of the list
result = json.loads(r.text)["predictions"][0]

# The returned results are probabilities, that can be positive or negative hence we take their absolute value
abs_scores = np.abs(result)

# Take the argmax that correspond to the index of the max probability.
label_id = np.argmax(abs_scores)

# Print the proper LABEL with its index
print(config.id2label[label_id])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: huggingface transformers 是一个自然语言处理工具包,它提供了各种预训练模型和算法,可以用于文本分类、命名实体识别、机器翻译等任务。它支持多种编程语言,包括Python、Java、JavaScript等,可以方便地集成到各种应用中。 ### 回答2: Hugging Face Transformers是一款先进的自然语言处理(NLP)工具包,旨在使NLP模型的开发和使用更加容易。它提供了多种预训练的语言模型,如BERT、GPT-2和RoBERTa等,这些模型已在许多NLP任务中取得了卓越的成果。与之前的语言模型相比,这些模型具有更快的训练速度和更高的精度。 除了提供预训练模型之外,Hugging Face Transformers还提供了一组方便的API,可以轻松地对这些模型进行微调或非监督训练。用户可以很容易地获得预测结果,对文本进行编码和解码,并使用其他功能快速重用现有的预训练模型。对于NLP开发人员和数据科学家来说,这意味着他们可以更快地建立高质量的NLP应用程序,而无需大量时间和资源。 Hugging Face Transformers提供了丰富的文档和示例代码,帮助用户更好地理解和使用工具包。它还提供了集成到Python环境中的快速部署、计算资源优化和分布式训练的支持。在与其他优秀的开源NLP工具配合使用时,Hugging Face Transformers可以使许多常见的文本分析工作更加容易。 总的来说,Hugging Face Transformers的出现极大地提高了NLP模型的开发效率和准确性,为广大NLP从业者带来了便利和贡献。 ### 回答3: Huggingface transformers是一个基于pytorch和tensorflow的自然语言处理开源工具库,旨在为开发人员和研究者提供高效、易用的语言模型开发和训练框架。该库提供了大量的预训练模型和常用的NLP任务模型,可以帮助开发人员节省训练和调试模型的时间,并提高模型的准确性。 Huggingface transformers的核心是自然语言处理的Transformer模型,包括BERT、GPT、XLNet、RoBERTa等。这些模型在自然语言处理的各种任务中都非常受欢迎,例如问答、文本分类、情感分析、机器翻译等。同时,Huggingface transformers还提供了一系列的自然语言处理管道,包括Tokenization、模型训练、模型推理等。 除了提供现成的预训练模型和任务模型之外,Huggingface transformers还允许用户自定义和训练自己的模型。用户可以使用Huggingface transformers中提供的组件来构建自己的模型,例如transformer层、attention机制等。此外,用户还可以使用预训练模型来进行迁移学习,只需微调模型就能快速获得较好的性能。 总而言之,Huggingface transformers是一个非常实用和方便的自然语言处理工具库,它提供了丰富的预训练模型和任务模型,可以帮助开发人员和研究者快速地训练和测试自然语言处理模型,并取得更好的性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值