当前位置:网站首页>HuggingFace
HuggingFace
2022-04-23 06:19:00 【qq1033930618】
一、官网
huggingface.co
二、模型下载
环境中安装 transformers包
conda install -n conda虚拟环境名称 transformers
模型自动下载 引号中是模型名称
from transformers import BertTokenizer, BertModel
model = BertModel.from_pretrained('bert-base-chinese', output_hidden_states = True,)
tokenizer = BertTokenizer.from_pretrained('bert-base-chinese')
模型自动下载位置
/home/用户名/.cache/huggingface/transformers
手动下载
网页最上方搜索模型名字
点击Model card右侧的Files and Versions
传入模型保存本地路径
model = BertModel.from_pretrained('./model', output_hidden_states = True,)
tokenizer = BertTokenizer.from_pretrained('./model/vocab.txt')
注意,BertModel.from_pretrained里面输入的是文件夹的路径
BertTokenizer.from_pretrained里面输入的是vocab.txt,而不是tokenizer.json。
加速下载
model = BertModel.from_pretrained('bert-base-chinese', mirror='tuna')
三、管道pipeline
直接使用模型
from transformers import pipeline
classifier = pipeline("sentiment-analysis") # 情感分析模型
classifier("We are very happy to show you the Transformers library.")
'''返回列表(含有一个字典 字典键 为label 和 score)'''
'''多个可以采用列表输入'''
results = classifier(["We are very happy to show you the Transformers library.", "We hope you don't hate it."])
'''返回多字典列表'''
for result in results:
print(f"label: {result['label']}, with score: {round(result['score'], 4)}")
从数据集加载
pip install datasets
'''指定分类和模型(语音识别) 如果只指定分类就会随机挑选模型'''
speech_recognizer = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h", device=0)
files = dataset["file"]
speech_recognizer(files[:4])
四、标记tokenizer
'''用于容纳模型'''
model_name = "nlptown/bert-base-multilingual-uncased-sentiment"
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
'''打印'''
classifier("Nous sommes très heureux de vous présenter la bibliothèque Transformers.")
五、自动类AutoClass
'''预先训练的模型的名称或路径中自动检索该模型的体系结构 关联 AutoTokenizer'''
六、自动词汇AutoTokenizer
文本拆分为多个单词 到文本可理解程度
from transformers import AutoTokenizer
model_name = "nlptown/bert-base-multilingual-uncased-sentiment"
tokenizer = AutoTokenizer.from_pretrained(model_name)
encoding = tokenizer("We are very happy to show you the Transformers library.")
print(encoding)
{
'input_ids': [101, 11312, 10320, 12495, 19308, 10114, 11391, 10855, 10103, 100, 58263, 13299, 119, 102],
'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}
版权声明
本文为[qq1033930618]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_51326691/article/details/123445554
边栏推荐
猜你喜欢
Detailed explanation of unwind stack backtracking
Int8 quantification and inference of onnx model using TRT
使用compressorjs压缩图片,优化功能,压缩所有格式的图片
浅谈BFC(块格式化上下文)
机器视觉系列(01)---综述
DMR system solution of Kaiyuan MINGTING hotel of Fengqiao University
Emergency air space integrated communication system scheme of Guangxi Power Grid
可视化之路(十)分割画布函数详解
el-select 中v-model绑定值,数据回显只显示value,不显示label
免费开源充电桩物联网云平台
随机推荐
Metro wireless intercom system
机器视觉系列(01)---综述
presto日期函数的使用
利用mysql-binlog恢复数据
Wireless communication system for large-scale sports events
hql求一个范围内最大值
Meishe technology launches professional video editing solution for desktop -- Meiying PC version
Take you to travel in space, and American photography technology provides comprehensive technical support for aerospace creative applet
el-date-picker中自定义快捷选项picker-options,动态设置禁用日期
javscript获取文件真实后缀名
golang实现一个带Web界面的五险一金计算器
The people of Beifeng have been taking action
Warning "force fallback to CPU execution for node: gather_191" in onnxruntime GPU 1.7
机器视觉系列(02)---TensorFlow2.3 + win10 + GPU安装
安装tui-editor失败,快速解决方案
使用compressorjs压缩图片,优化功能,压缩所有格式的图片
Error in multi machine and multi card training
Intuitive understanding of torch nn. Unfold
关于短视频平台框架搭建与技术选型探讨
可视化常见问题解决方案(七)画图刻度设置解决方案