[0:00] hi everyone today i show you how to get [0:01] started with hacking face and the [0:03] transformers library the hacking face [0:05] transformers library is the most popular [0:08] nlp library in python with over 60 000 [0:11] stars on github it provides state of the [0:13] art natural language processing models [0:15] and a very clean api that makes it super [0:18] simple to build powerful nlp pipelines [0:20] even for beginners so today i show you [0:22] how to get started with it i show you [0:24] how to use the pipeline how to use model [0:27] and tokenizer how to combine it with [0:29] pytorch or tensorflow how to save and [0:32] load models how to use models from the [0:34] official model hub and also how to fine [0:36] tune your own models so let's get [0:38] started [0:40] so first of all how do we install the [0:42] transformers library so the transformers [0:45] library should be combined with your [0:46] favorite deep learning library so this [0:48] could be pytorch or tensorflow or even [0:51] flex [0:52] so go ahead and install these first and [0:55] then you can install the transformers [0:57] library by saying pip install [0:59] transformers and that's all you need to [1:01] do [1:03] first let's have a look at the pipeline [1:06] so a pipeline makes it super simple to [1:08] apply an nlp task because it abstracts a [1:11] lot of things away for us and the way it [1:14] works is that we say from transformers [1:16] import pipeline then we create a [1:19] pipeline object so we say classifier [1:21] equals pipeline and here we put in a [1:24] task so in this case we want to do [1:26] sentiment analysis there are a lot of [1:29] more tasks available and we will have a [1:31] look at them in a moment but for now [1:33] let's do the sentiment analysis so we [1:36] create our object and then we apply this [1:39] classifier and here we put in the data [1:41] that we want to test so in this case we [1:44] only put in one string and the string is [1:46] i've been waiting for a hugging phase [1:48] course my whole life and then we print [1:50] the results so now let's run this and [1:52] see how the result looks like [1:54] all right and here's the result so we [1:56] see the label which is positive and we [1:58] also get a score so almost 96 percent [2:02] so yeah this is super cool and the way [2:04] this pipeline works is that it will do [2:07] three things for us so the first one is [2:10] the pre-processing so it's [2:12] pre-processing the text so in this case [2:15] it's applying a tokenizer then it feeds [2:18] the pre-processed text to the model then [2:20] it applies the model and then it also [2:22] does the post-processing so [2:24] post-processing means it will show us [2:27] the result how we would expect it so in [2:29] this case of a sentiment analysis [2:31] pipeline it for example shows us the [2:34] label positive or negative but it can [2:36] also look different for different tasks [2:39] so yeah that's how it works and now [2:40] let's look at a few other examples of [2:42] pipelines for example we can also use a [2:45] text generation pipeline and we can also [2:48] give it a specific model so in the first [2:51] example we just used the default model [2:54] which you can also see here in the [2:56] output but you can give it a specific [2:59] model either one that you have saved [3:01] locally or one from the model hub so we [3:04] will also have a look at this in a [3:06] moment so yeah let's apply this example [3:09] to generate some text and you can also [3:12] see there are different available [3:14] arguments so for this i just recommend [3:16] to check out the documentation so yeah [3:19] here's the result so we wanted to have [3:21] two possible [3:23] return sequences so the first generated [3:26] text is this one in this course we will [3:28] teach you how to play chess [3:30] or here's a second one in this course we [3:33] will teach you how to use a combination [3:35] of a traditional and simple blah blah [3:37] blah so yeah this also works and now [3:40] let's have a look at a third example for [3:42] example we can do zero shot [3:44] classification this means we can give it [3:46] a text without knowing the corresponding [3:49] label and then we put different [3:51] candidate labels for example this text [3:54] can be education politics or business [3:57] and then let's run this and see the [3:59] result and here we get the results so [4:01] all the different labels and the [4:03] different scores and the highest scores [4:05] with over 96 [4:07] is the education which is correct so [4:10] let's have a look at the different other [4:11] available pipelines so for this i [4:13] recommend to go to the official [4:15] documentation and here you see all the [4:18] available tasks for example we can do [4:20] audio classification we can do automatic [4:22] speech recognition we can do image [4:25] classification question answering [4:28] and translation summarization so yeah [4:31] this is super cool and yeah i just [4:32] recommend to play around with different [4:34] ones and see how it looks like [4:37] now let's have a look behind the [4:39] pipeline and understand the different [4:41] steps a little bit better so for this we [4:43] have a look at a tokenizer and the model [4:45] class [4:46] so we can say [4:48] from transformers import auto tokenizer [4:51] and auto model for sequence [4:53] classification so this one here is a [4:56] very generic class and this is also a [4:59] generic class but a little bit more [5:00] specified for the sequence [5:02] classification task so for this i just [5:04] recommend to have a look at the official [5:06] documentation but for example if you [5:08] know you want a specific one there's for [5:11] example also a bird tokenizer class and [5:14] a bird model class [5:17] so [5:18] yeah so we import those classes and then [5:20] we create instances of this [5:23] so for this we specify a model name so [5:27] in this case this is just the default [5:29] model that is used for this pipeline and [5:32] then we call the model class and say dot [5:35] from pre-trained with the model name and [5:38] the same for the tokenizer and this from [5:41] pre-trained method is a very important [5:43] method in hugging phase that you will [5:45] see a lot of times so just keep this one [5:48] in mind and now that we have this we can [5:51] for example copy and paste the same code [5:55] and now for the pipeline we can say [5:57] model equals model and [6:00] tokenizer equals the [6:03] tokenizer [6:05] and now since this is just the same [6:08] default model this should produce the [6:10] very same result so let's run this and [6:13] have a look at the output and the result [6:15] is the very same like i said so this [6:18] works so yeah this is what's going on [6:20] under the hood so there will be a [6:22] tokenizer and a model so now let's have [6:25] a look at the tokenizer and see what [6:27] this is doing so a tokenizer basically [6:30] puts a text in a mathematical [6:32] representation that the model [6:33] understands and in order to use this we [6:36] can call the tokenizer directly and give [6:39] it a text as input or we can also put in [6:42] multiple texts as ones as a list [6:45] and we can so here we do this and print [6:48] this and we can also do this separately [6:51] so we can call [6:53] tokenizer.tokenize this will give us [6:56] tokens back then we can call [6:58] tokenizer.convert [7:00] tokens to ids this will give us the ids [7:04] and we can do it the other way around so [7:06] we can call tokenizer.dcodeids [7:09] and this will give us the original [7:11] stringback so let's run this and have a [7:14] look at the different outputs all right [7:16] so here we see the output so if we apply [7:18] the tokenizer directly then here we get [7:21] this dictionary and the dictionary [7:24] contains the input ids that look like [7:27] this then we also have a attention mask [7:30] so for now we don't have to worry about [7:31] this a attention mask basically is a [7:34] list of zeros and ones and a zero means [7:37] that the attention layer should ignore [7:39] this token then if we do this separately [7:42] so if we call tokenizer.tokenize [7:46] then here we see the different tokens [7:48] then if we convert the tokens to ids [7:51] then each token has a unique [7:53] corresponding id so we see this here and [7:57] if we decode this then we get the [8:00] original string back but here please [8:02] note that we [8:04] basically removed the capitalization but [8:07] yeah and now if we compare um [8:11] this one with this one [8:13] then you see this should be the very [8:16] same ids but here we also have this id [8:20] and this id so this means beginning of [8:24] sentence and end of sentence [8:26] but basically yeah it's the same and [8:29] yeah and this is how a tokenizer works [8:33] now let's see how we can combine the [8:35] code with pythog or tensorflow so in [8:37] this example we use pytorch but the code [8:40] is very similar with tensorflow [8:42] so with tensorflow usually we have a tf [8:46] before all those classes [8:48] and yeah in the first case i simply [8:51] apply the pipeline like before and now [8:55] we use multiple sentences so usually we [8:58] just put in one sentence but we can use [9:01] a list of all those sentences so we call [9:03] this our x train data and yeah here we [9:07] feed it to the pipeline classifier and [9:09] print the result and now we do this [9:11] separately [9:13] so first we call the tokenizer with the [9:16] x train data and we call this our batch [9:20] and [9:21] then we can give it different arguments [9:23] like padding equals true truncation [9:25] equals true max length equals 512 [9:29] and return tensors equals pt so this [9:32] will be in pi torch format so you will [9:35] see how this looks in a moment because [9:37] we print the batch [9:39] so yeah usually we apply the tokenizer [9:42] directly instead of doing the [9:44] different functions separately [9:46] and then we do the inference in pytorch [9:50] so for this we say with torch dot no [9:52] grad then we call our model and here we [9:56] unpack this batch because this is a [9:59] dictionary and then we can apply [10:01] different functions like f dot soft max [10:03] to get the predictions or torch dot arc [10:06] max to get the labels and again these [10:10] predictions should be the same scores [10:12] that we get from our pipeline because it [10:15] essentially is the same step except that [10:17] now we do it for ourselves so let's run [10:20] this and have a look at the result so [10:22] yeah here we print the batch and you see [10:25] this is a dictionary with the input ids [10:29] and now we see this is a tensor and this [10:32] is because we specified in pi charge [10:35] tensor format so without this this would [10:37] just be a normal list and then we had to [10:40] take care of putting it in the correct [10:42] format ourselves but yeah this makes it [10:44] super handy to work with pytorch and [10:46] tensorflow and then here we print the [10:50] predictions and the labels and you see [10:52] if we compare this prediction score with [10:56] this one then it's the very same so yeah [10:59] this is how it works if we do it step by [11:01] step and this could be useful if we for [11:03] example want to fine tune our model with [11:05] a pytorch training loop [11:08] now to save a tokenizer and model we can [11:10] specify a safe directory and then we can [11:13] call [11:14] tokenizer.save pretrained and also [11:16] model.save pretrained and when we want [11:19] to load this again we can pick a class [11:21] like this one and then call [11:23] autotokenizer.frompretrained [11:26] and also for the model we say dot from [11:28] pre-trained and then we get the loaded [11:31] tokenizer and model and this should get [11:33] you the same results as before [11:36] now let's have a look at how we can use [11:38] different models from the model hub so [11:40] on the official home page we can click [11:42] on models and then you see there are [11:45] almost 35 000 models available created [11:48] from the community which is just awesome [11:51] so [11:52] here on the left side we can filter for [11:54] example we can filter for the different [11:56] pipeline tasks or we can filter for [11:59] libraries or data sets or languages [12:02] and we can also use the search bar for [12:06] example if i want a german model i can [12:08] simply search for this [12:10] so here let's filter for text [12:12] classification so this is the same as [12:15] the text analysis task and in this case [12:18] this is the default model and usually [12:22] the name says the name of the model so [12:24] in this case it's a distal bird base [12:27] uncased model and then it's fine-tuned [12:30] on the sst2 data set and it's in english [12:34] so yeah and then you can read through [12:36] this and find out more information [12:38] so let's for example clear this and [12:41] search for summarization and then click [12:43] on this one [12:45] and [12:46] yeah sometimes you even find code [12:49] examples and the way you can use this [12:51] now is either you grab the code example [12:54] or here in the top next to the model [12:57] name you can click on this copy icon [13:00] this will copy the whole name and now we [13:04] can jump back to the code and then for [13:07] example here again we want a pipeline [13:09] and in this case we know it's a [13:11] summarization pipeline and then as model [13:15] now here we paste in this model name and [13:18] then it's applying this one from the [13:20] model hub and this is how you can use [13:22] the model hub to use different models [13:25] now let's briefly go over how we can [13:27] fine-tune our own model so i'm not going [13:30] into detail here but they have excellent [13:32] documentation on the official pages so i [13:34] will put the link in the description and [13:36] by the way also you could switch here [13:38] between pytorch and tensorflow code and [13:41] then open a collab and have a look at [13:43] the exam code so this is super helpful [13:46] but [13:47] usually the way it works so of course [13:49] for fine tuning we use our own data set [13:51] so we prepare this [13:53] then we load a pre-trained tokenizer and [13:56] call it with this data set and get the [13:58] encodings [13:59] then in case of pi torch we prepare a pi [14:02] torch data set with the encodings then [14:05] we also load a pre-trained model and now [14:08] we can use this trainer class from the [14:10] transformers library and also the [14:12] trainer arguments and then we set this [14:15] up with the [14:16] model that we want to use and the data [14:18] sets that we prepared and then we simply [14:21] call [14:22] trainer.train and this is how we do this [14:24] we could also again do this with our [14:26] native pie charts training loop but this [14:29] just makes your life super simple and [14:31] this is how you fine-tune your own data [14:33] all right i hope you enjoyed this [14:34] tutorial if you have any questions let [14:36] us know in the comments also you might [14:38] enjoy this video about how to get [14:40] started with open ai and gpt3 so if you [14:43] haven't already then check this out and [14:45] then i hope to see you in the next video [14:47] bye