Transformer实战(5)——Hugging Face环境配置与应用详解

AI摘要

本文详述Hugging Face Transformers库的本地环境搭建流程,涵盖Anaconda安装、PyTorch/TensorFlow依赖配置、hf-mirror加速模型下载;重点讲解分词器与预训练语言模型(如BERT)的加载与推理、pipeline零样本任务调用、社区模型复用方法,以及CLIP等多模态Transformer在零样本图像分类中的端到端实践。

0. 前言

我们已经了解了使用深度学习 (Deep Learning, DL) 方法的自然语言处理 (Natural Language Processing, NLP) 技术演进,学习了关于 Transformer 及其架构的一些基本信息。在进行实践之前,我们将首先了解所需环境的安装步骤,介绍如何在 PyTorch 和 TensorFlow 框架中使用模型。
Hugging Face 模型库是本节的一个重要部分,我们将讨论如何获取不同的模型以及使用不同管道的步骤。例如,双向自回归 Transformer (Bidirectional and Auto-regressive Transformer, BART)、BERT 和 TAPAS (TAble PArSing) 等模型,同时介绍 GPT-2 (Generative Pretrained Transformer 2) 文本生成。需要注意的是,本节主要集中在如何准备环境以及如何使用预训练模型,而不是模型训练。模型训练将在后续学习中详细讨论。

1. 安装

Transformer架构和Transformers 库和Hugging Face 和之间的关系非常紧密,Hugging Face 是推动 Transformer 架构普及和应用的重要力量。 阅读详情

相关推荐

Transformer与Hugging Face:NLP工程范式迁移实战指南

Transformer架构标志着自然语言处理从序列状态机向并行张量流编程的根本转变,其核心价值在于确定性低延迟、长程依赖建模能力与工业级可扩展性;Hugging Face则将这一能力封装为开箱即用的NLP操作系统,涵盖模型加载、Tokenizer行为控制、Pipeline推理封装及生产部署全链路。理解自注意力机制与位置编码的协同原理、掌握tokenizer的padding/truncation静默行为、识别pipeline背后的状态管理逻辑,是构建高可用文本智能服务的技术基石。本文聚焦真实业务场景中的典型问

weixin_30325487的博客 416

Transformer与Hugging Face实战指南:从显存优化到生产部署

Transformer架构是现代NLP的基石,其自注意力机制通过并行化计算彻底替代了RNN的序列依赖,显著提升长文本建模能力;Hugging Face则将预训练-微调范式工程化落地,提供标准化tokenizer、模型Hub和Trainer等核心组件。理解attention mask机制与tokenization策略,直接关系到法律文书截断、金融新闻误判等真实问题的解决效率。在资源受限场景下(如16GB GPU),合理配置gradient checkpointing、fp16及Flash Attention,

weixin_34032621的博客 439

如何粗暴地下载huggingface_hub指定数据文件

snapshot_download 函数有点sb的一点是,你的本地目录已经存在了某个文件,你运行snapshot_download之后就重复下载。一看介绍:如果local_dir_use_symlinks=False并且blob文件不在缓存目录中,那么文件会被下载并直接放在local_dir下。snapshot_download 会利用 hf_hub_download 函数实现下载所有文件,hf_hub_download是负责下载单个文件的。可见下载单个文件,下载整个仓库文件都是可行的。

q742971636的博客 3453

transformers环境配置+文本分类实例

注意 transformers==4.33.2 的下载版本,下载太高容易出错。点击生成的网址,在网页中弹出如下结果验证transformers环境搭建成功!最好选用python3.9因为3.8会导致jupyterlab下载失败。这样的话默认使用清华镜像下载,就不会每次都去在指令后添加了。这里选择11以上的且小于你电脑配置的就可以。这里下载所有实验中常见需要的包。

LZL2020LZL的博客 899

TensorFlow深度学习实战——使用Hugging Face构建Transformer模型

本节介绍了如何利用Hugging Face库高效实现Transformer模型的应用,Hugging Face提供了丰富的预训练模型和工具,支持多种自然语言处理任务。首先说明了安装Hugging Face库的方法,并通过情感分析示例验证安装。随后,详细展示了文本生成、自动模型选择与分词、命名实体识别以及摘要生成等任务的实现步骤,均通过Hugging Face的pipeline或相关API轻松完成。最后,重点介绍了模型微调的方法,演示了如何加载数据集、转换为TensorFlow格式、选择预训练模型并进行训练。

盼小辉丶的博客 4077

PyTorch生成式人工智能——Hugging Face环境配置与应用详解

在本节中,我们将介绍如何在 PyTorch 和 TensorFlow 框架中使用 Hugging Face 模型库,我们将讨论如何获取不同的模型以及使用不同管道的步骤,例如,双向自回归 Transformer (BART)、BERT 和 TAPAS 等模型,同时介绍 GPT-2 (Generative Pretrained Transformer 2) 文本生成。需要注意的是,本节主要集中在如何准备环境以及如何使用预训练模型,而不是模型训练,模型训练将在后续学习中详细讨论。

盼小辉丶的博客 2316

Hugging Face Transformer 的APIs应用实例

Hugging Face 的变压器库提供了广泛的 API,可用于处理各种 NLP 任务的预训练变压器模型。在本教程中,我们将探讨主要 API 并提供示例来帮助你了解它们的用法。

gongdiwudu的专栏 6491

Hugging Face模型训练实战详解(NLP+Transformer+PyTorch)下篇

Hugging Face作为现代自然语言处理的基础设施,构建了以库为核心、涵盖模型、数据集和部署工具的完整开发生态。其核心设计理念是“民主化AI”,通过开源促进模型共享与协作创新。# 快速体验NLP任务的极简接口")print(result) # 输出: [{'label': 'POSITIVE', 'score': 0.9998}]该平台不仅提供统一API访问数万个预训练模型,还通过Model Hub实现版本控制、可复现性与社区协作,显著降低NLP应用门槛。

weixin_36064575的博客 1160

【FPGA】安路FPGA下载器驱动安装教程

#来自学渣的问候 最近正在参加19年的FPGA创新设计大赛,然后我们选择了安路(全名:上海安路信息科技有限公司)的 FPGA 开发板。安路算是国产FPGA中还不错的一家,和紫光一样,除了自主生产的FPGA,也开始拥有了自己的编译环境。 好了。言归正传,写这篇博客的原因是,前几天在安装 安路 的编译环境和下载器驱动的时候,出了点问题,弄得心烦意乱。所以打算解决之后写篇博客,避开这个神奇的问题。 首先...

Ninquelote的博客 2万+

Vivado实战:AXI转PCIe IP核配置全流程(含性能优化技巧)

本文详细介绍了在Vivado环境中配置AXI转PCIe IP核的全流程,包括工程准备、IP核参数设置、BAR空间配置、中断系统优化以及TLP传输效率提升等关键步骤。特别分享了经过验证的性能优化技巧,帮助开发者显著提升PCIe链路的吞吐量和稳定性,适用于视频采集、高速数据记录等应用场景。

weixin_30892037的博客 555

OTA(Over-The-Air)升级

OTA(Over-the-Air)是一种通过无线方式进行数据传输和更新的技术,通常用于电子设备(如智能手机、汽车、物联网设备等)的软件、固件或配置更新。OTA可以在设备与服务器之间进行远程传输,用户无需将设备接入电脑或进行有线操作。通过OTA,用户只需要通过Wi-Fi或移动网络连接,便能自动接收并安装最新的更新,而无需手动干预。OTA的优点是便捷和高效,能够确保设备随时保持最新状态,同时减少用户的操作负担。:如智能家居设备、路由器、汽车等硬件设备的固件更新。:确保设备上的应用保持最新版本。

weixin_50127894的博客 5073

matlab2021a版本条件下安装spm

SPM (Statistical Parametric Mapping),中文名叫统计参数映射。统计参数映射是指构建和评估空间扩展的统计过程,用于测试关于功能成像数据的假设。这些想法已经在名为SPM的免费开源软件中得到了实例化。SPM软件包用于脑成像数据序列分析。目前的版本可以分析fMRI, PET, SPECT, EEG和MEG。 可能由于matlab版本更新的问题,按照之前教程所写的安装方法会出现错误,踩了一些坑,但好在最终解决了。。。 1、下载链接,如下 SPM12 Software -...

qq_42356925的博客 8407

Apple 网页 SRP 登录系列文章(四):Hashcash 工作量证明与完整登录流程

本文是Apple SRP登录系列文章的收官之作,重点解析了Apple独有的Hashcash工作量证明机制并展示了完整的登录流程实现。文章详细介绍了Hashcash的安全目的、格式标准、计算核心算法及难度评估方法,特别提供了Python实现代码。同时还讲解了如何动态获取服务器Hashcash参数以及建立容错机制。最后呈现了完整的Apple SRP登录主函数架构,为开发者提供了从理论到实践的全面指导。全文通过代码示例和流程分析,系统性地阐述了Apple安全登录机制的技术细节。

Sayid_Des的专栏 633

YOLO11改进|卷积篇|引入SPDConv

YOLO11中添加SPDConv卷积

A1983Z的博客 3700

【SigmaStudio】自定义算法组件之XML编写规范(一)

SigmaStudio生成自定义算法组件时,需要XML和DLB文件,本文主要记录XML文件的编码规范

liang_song的博客 1142

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

上一篇: TensorFlow深度学习实战——Transformer模型评价指标
下一篇: PyTorch生成式人工智能(11)——神经风格迁移
盼小辉丶
盼小辉丶 优质创作者: 人工智能技术领域 优质创作者: 人工智能技术领域 领域专家: 数据科学与机器学习技术领域 领域专家: 数据科学与机器学习技术领域
博客等级 码龄15年 14万+粉丝 903原创
评论 186
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盼小辉丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值