当前位置:网站首页>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
边栏推荐
猜你喜欢
可视化之路(十)分割画布函数详解
枫桥学院开元名庭酒店DMR系统解决方案
SDC intelligent communication patrol management system of Nanfang investment building
江宁医院DMR系统解决方案
基于openmv的无人机Apriltag动态追踪降落完整项目资料(labview+openmv+apriltag+正点原子四轴)
美摄助力百度“度咔剪辑”,让知识创作更容易
Take you to travel in space, and American photography technology provides comprehensive technical support for aerospace creative applet
可视化常见问题解决方案(九)背景颜色问题
免费开源充电桩物联网云平台
无盲区、长续航|公专融合对讲机如何提升酒店服务效率?
随机推荐
启动mqbroker.cmd失败解决方法
“泉”力以赴·同“州”共济|北峰人一直在行动
vim+ctags+cscpope开发环境搭建指南
Systrace parsing
The people of Beifeng have been taking action
enforce fail at inline_ container. cc:222
H5案例开发
美摄科技云剪辑,助力哔哩哔哩使用体验再升级
SDC intelligent communication patrol management system of Nanfang investment building
广西电网|应急空天一体化通信系统方案
南方投资大厦SDC智能通信巡更管理系统
golang实现一个带Web界面的五险一金计算器
el-select 中v-model绑定值,数据回显只显示value,不显示label
重大安保事件应急通信系统解决方案
浅谈BFC(块格式化上下文)
可视化常见问题解决方案(八)共享绘图区域问题解决方案
PyTorch 9. optimizer
Intuitive understanding of torch nn. Unfold
JDBC连接池
免费开源智能充电桩物联网SAAS云平台