（嘶嘶声） (whooshing sound)
- 如何实例化 Transformers 模型？ - How to instantiate the Transformers model?
在本视频中，我们将了解如何创建 In this video, we will look at how we can create
并使用 Transformers 库中的模型。 and use a model from the Transformers library.
正如我们之前看到的，TFAutoModel 类 As we've seen before, the TFAutoModel class
允许你实例化预训练模型, allows you to instantiate a pre-trained model
从 Hugging Face Hub 上的任何一个 checkpoint。 from any checkpoint on the Hugging Face Hub.
它将从库中选择正确的模型类 It will pick the right model class from the library
来实例化适当的结构 to instantiate the proper architecture
并在里面加载预训练模型的权重。 and load the weights of the pre-trained model inside.
正如我们所见，当给定一个 BERT 的 checkpoint 时， As we can see, when given a BERT checkpoint,
我们最终得到一个 TFBertModel， we end up with a TFBertModel,
GPT2 或 BART 也类似。 and similarly for GPT2 or BART.
在幕后，这个 API 接受 Behind the scenes, this API can take the name
Hub 上的一个 checkpoint 的名字， of a checkpoint on the Hub,
在这种情况下，它将下载并缓存 in which case it will download and cache
配置文件以及模型权重文件。 the configuration file as well as the model weights file.
你还可以指定本地文件夹的路径 You can also specify the path to a local folder
包含有效的配置文件 that contains a valid configuration file
和模型的权重文件。 and a model weights file.
要实例化预训练模型， To instantiate the pre-trained model,
TFAutoModel API 会首先打开配置文件 the TFAutoModel API will first open the configuration file
以查看应该使用的配置类。 to look at the configuration class that should be used.
配置类取决于模型的类型， The configuration class depends on the type of the model,
例如 BERT、GPT2 或 BART。 BERT, GPT2 or BART for instance.
一旦它有了正确的配置类， Once it has the proper configuration class,
它可以实例化该配置， it can instantiate that configuration,
这是了解如何创建模型的蓝图。 which is a blueprint to know how to create the model.
它也使用这个配置类 It also uses this configuration class
来找到合适的模型类， to find the proper model class,
它与加载的配置相结合 which is combined with the loaded configuration
加载模型。 to load the model.
这个模型还不是我们的预训练模型 This model is not yet our pre-trained model
因为它刚刚用随机权重初始化。 as it has just been initialized with random weights.
最后一步是加载权重 The last step is to load the weights
从该模型中的模型文件中。 from the model file inside this model.
为了轻松加载模型的配置, To easily load the configuration of a model
从任何 checkpoint 或 from any checkpoint or
包含配置文件的文件夹， a folder containing the configuration file,
我们可以使用 AutoConfig 类。 we can use the AutoConfig class.
与 TFAutoModel 类一样， Like the TFAutoModel class,
它将从库中选择正确的配置类。 it will pick the right configuration class from the library.
我们也可以使用特定的类 We can also use the specific class
对应一个 checkpoint ， corresponding to a checkpoint,
但我们需要更改代码 but we will need to change the code
每当我们想尝试不同的模型结构时。 each time we want to try a different model architecture.
正如我们之前所说，模型的配置 As we said before, the configuration of a model
是包含所有必要信息的蓝图 is a blueprint that contains all the information necessary
以创建模型结构。 to create the model architecture.
例如，BERT 模型 For instance, the BERT model
与 bert-base-cased 的 checkpoint 关联 associated with the bert-base-cased checkpoint
有 12 层，隐藏大小为 768， has 12 layers, a hidden size of 768,
词汇量为 28,996。 and a vocabulary size of 28,996.
一旦我们有了配置， Once we have the configuration,
我们可以创建一个具有相同结构的模型 we can create a model that has the same architecture
作为我们的 checkpoint ，但是随机初始化的。 as our checkpoint but is randomly initialized.
我们然后就可以像任何 TensorFlow 模型一样从头开始训练它。 We can then train it from scratch like any TensorFlow model.
我们还可以更改配置的任何部分 We can also change any part of the configuration
通过使用关键字参数。 by using keyword arguments.
第二段代码实例化 The second snippet of code instantiates
一个随机初始化的 BERT 模型 a randomly initialized BERT model
有 10 层而不是 12 层。 with 10 layers instead of 12.
训练或微调后保存模型非常容易。 Saving a model once it's trained or fine-tuned is very easy.
我们只需要使用 save_pretrained 方法。 We just have to use the save_pretrained method.
在这里，模型将保存在一个文件夹中 Here, the model will be saved in a folder
在当前工作目录中命名为 my-bert-model。 named my-bert-model inside the current working directory.
然后可以重新加载这样的模型 Such a model can then be reloaded
使用 from_pretrained 方法。 using the from_pretrained method.
要将其运行到 Hub 的项目模型， To run it to a projects model to the Hub,
查看推送（咕哝）视频。 check out the push (mumbles) video.
（嘶嘶声） (whooshing sound)