Buckets:

rtrm's picture
download
raw
10.9 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Transformers 的设计理念&quot;,&quot;local&quot;:&quot;transformers-的设计理念&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;主要概念&quot;,&quot;local&quot;:&quot;主要概念&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}">
<link href="/docs/transformers/main/zh/_app/immutable/assets/0.e3b0c442.css" rel="modulepreload">
<link rel="modulepreload" href="/docs/transformers/main/zh/_app/immutable/entry/start.a61b9c50.js">
<link rel="modulepreload" href="/docs/transformers/main/zh/_app/immutable/chunks/scheduler.9991993c.js">
<link rel="modulepreload" href="/docs/transformers/main/zh/_app/immutable/chunks/singletons.2822fe91.js">
<link rel="modulepreload" href="/docs/transformers/main/zh/_app/immutable/chunks/index.02cfeb18.js">
<link rel="modulepreload" href="/docs/transformers/main/zh/_app/immutable/chunks/paths.d66588b4.js">
<link rel="modulepreload" href="/docs/transformers/main/zh/_app/immutable/entry/app.99775688.js">
<link rel="modulepreload" href="/docs/transformers/main/zh/_app/immutable/chunks/index.7fc9a5e7.js">
<link rel="modulepreload" href="/docs/transformers/main/zh/_app/immutable/nodes/0.f4c5a5c1.js">
<link rel="modulepreload" href="/docs/transformers/main/zh/_app/immutable/chunks/each.e59479a4.js">
<link rel="modulepreload" href="/docs/transformers/main/zh/_app/immutable/nodes/51.dd515450.js">
<link rel="modulepreload" href="/docs/transformers/main/zh/_app/immutable/chunks/EditOnGithub.84ab7f0e.js"><!-- HEAD_svelte-u9bgzb_START --><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Transformers 的设计理念&quot;,&quot;local&quot;:&quot;transformers-的设计理念&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;主要概念&quot;,&quot;local&quot;:&quot;主要概念&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"><!-- HEAD_svelte-u9bgzb_END --> <p></p> <h1 class="relative group"><a id="transformers-的设计理念" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#transformers-的设计理念"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Transformers 的设计理念</span></h1> <p data-svelte-h="svelte-13ap7ii">🤗 Transformers 是一个专为以下用户群体构建的库:</p> <ul data-svelte-h="svelte-1i79vmi"><li>寻求使用、研究或扩展大规模 Transformers 模型的机器学习研究人员和教育者。</li> <li>希望微调这些模型或在生产环境中使用它们(或两者兼而有之)的实际操作者。</li> <li>只想下载预训练模型并将其用于解决给定机器学习任务的工程师。</li></ul> <p data-svelte-h="svelte-u2gegz">Transformers 设计时有两个主要目标:</p> <ol data-svelte-h="svelte-c6w8r"><li><p>尽可能简单快速地使用:</p> <ul><li>我们尽可能地限制用户能接触的抽象层,实际上几乎没有抽象。用户只需学习三个标准类即可使用每个模型:<a href="main_classes/configuration">configuration</a><a href="main_classes/model">models</a> 和一个预处理类(用于 NLP 的 <a href="main_classes/tokenizer">tokenizer</a>,用于视觉的 <a href="main_classes/image_processor">image processor</a>,用于音频的 <a href="main_classes/feature_extractor">feature extractor</a>,以及用于多模态输入的 <a href="main_classes/processors">processor</a>)。</li> <li>所有这些类都可以通过一个通用的 <code>from_pretrained()</code> 方法从预训练实例中简单统一地初始化,该方法会从提供在 <a href="https://huggingface.co/models" rel="nofollow">Hugging Face Hub</a> 上的预训练检查点(如果需要的话)下载、缓存和加载相关类实例及相关数据(配置的超参数、分词器的词汇表和模型的权重)。</li> <li>在这三个基本类之上,该库提供了两种 API:<a href="/docs/transformers/main/zh/main_classes/pipelines#transformers.pipeline">pipeline()</a> 用于快速在给定任务上使用模型进行推断,以及 <code>Trainer</code> 用于快速训练或微调 PyTorch 模型(所有 TensorFlow 模型与 <code>Keras.fit</code> 兼容)。</li> <li>因此,Transformers 不是神经网络的模块化工具箱。如果要基于 Transformers 扩展或搭建新项目,请使用常规的 Python、PyTorch、TensorFlow、Keras 模块,并从 Transformers 的基类继承以重用模型加载和保存等功能。如果想了解更多有关我们的模型代码的设计理念,请查看我们的<a href="https://huggingface.co/blog/transformers-design-philosophy" rel="nofollow">重复自己</a>博文。</li></ul></li> <li><p>提供与原始模型性能尽可能接近的最新模型:</p> <ul><li>我们为每种架构提供至少一个示例,复现了该架构官方作者提供的结果。</li> <li>代码通常尽可能接近原始代码库,这意味着某些 PyTorch 代码可能不够<em>pytorchic</em>,因为它是转换后的 TensorFlow 代码,反之亦然。</li></ul></li></ol> <p data-svelte-h="svelte-17uba39">其他几个目标:</p> <ul data-svelte-h="svelte-11rn4wd"><li><p>尽可能一致地公开模型的内部:</p> <ul><li>我们使用单一 API 提供对完整隐藏状态和注意力权重的访问。</li> <li>预处理类和基本模型 API 标准化,便于在不同模型之间轻松切换。</li></ul></li> <li><p>结合主观选择的有前途的工具进行模型微调和调查:</p> <ul><li>简单一致的方法来向词汇表和嵌入中添加新标记以进行微调。</li> <li>简单的方法来屏蔽和修剪 Transformer 头部。</li></ul></li> <li><p>轻松在 PyTorch、TensorFlow 2.0 和 Flax 之间切换,允许使用一个框架进行训练并使用另一个进行推断。</p></li></ul> <h2 class="relative group"><a id="主要概念" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#主要概念"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>主要概念</span></h2> <p data-svelte-h="svelte-l2e0t">该库围绕每个模型的三类类构建:</p> <ul data-svelte-h="svelte-1mdxomy"><li><strong>模型类</strong> 可以是 PyTorch 模型(<a href="https://pytorch.org/docs/stable/nn.html#torch.nn.Module" rel="nofollow">torch.nn.Module</a>)、Keras 模型(<a href="https://www.tensorflow.org/api_docs/python/tf/keras/Model" rel="nofollow">tf.keras.Model</a>)或 JAX/Flax 模型(<a href="https://flax.readthedocs.io/en/latest/api_reference/flax.linen/module.html" rel="nofollow">flax.linen.Module</a>),这些模型可以使用库中提供的预训练权重。</li> <li><strong>配置类</strong> 存储构建模型所需的超参数(如层数和隐藏大小)。通常情况下,如果您使用不进行任何修改的预训练模型,则创建模型将自动处理配置的实例化(配置是模型的一部分)。</li> <li><strong>预处理类</strong> 将原始数据转换为模型可接受的格式。一个 <a href="main_classes/tokenizer">tokenizer</a> 存储每个模型的词汇表,并提供编码和解码字符串为要馈送到模型的令牌嵌入索引列表的方法。<a href="main_classes/image_processor">Image processors</a> 预处理视觉输入,<a href="main_classes/feature_extractor">feature extractors</a> 预处理音频输入,而 <a href="main_classes/processors">processor</a> 则处理多模态输入。</li></ul> <p data-svelte-h="svelte-1996l5u">所有这些类都可以从预训练实例中实例化、本地保存,并通过以下三种方法与 Hub 共享:</p> <ul data-svelte-h="svelte-1hipn59"><li><code>from_pretrained()</code> 允许您从库自身提供的预训练版本(支持的模型可在 <a href="https://huggingface.co/models" rel="nofollow">Model Hub</a> 上找到)或用户本地(或服务器上)存储的版本实例化模型、配置和预处理类。</li> <li><code>save_pretrained()</code> 允许您本地保存模型、配置和预处理类,以便可以使用 <code>from_pretrained()</code> 重新加载。</li> <li><code>push_to_hub()</code> 允许您将模型、配置和预处理类共享到 Hub,以便所有人都可以轻松访问。</li></ul> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/transformers/blob/main/docs/source/zh/philosophy.md" target="_blank"><span data-svelte-h="svelte-1kd6by1">&lt;</span> <span data-svelte-h="svelte-x0xyl0">&gt;</span> <span data-svelte-h="svelte-1dajgef"><span class="underline ml-1.5">Update</span> on GitHub</span></a> <p></p>
<script>
{
__sveltekit_173uja2 = {
assets: "/docs/transformers/main/zh",
base: "/docs/transformers/main/zh",
env: {}
};
const element = document.currentScript.parentElement;
const data = [null,null];
Promise.all([
import("/docs/transformers/main/zh/_app/immutable/entry/start.a61b9c50.js"),
import("/docs/transformers/main/zh/_app/immutable/entry/app.99775688.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 51],
data,
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
10.9 kB
·
Xet hash:
0f6be63ede3fb42077bf1291c1fe8902f91ce7e9c07aa153e399bddd71f3100a

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.