[00:00]
hi everyone today i show you how to get
[00:01]
started with hacking face and the
[00:03]
transformers library the hacking face
[00:05]
transformers library is the most popular
[00:08]
nlp library in python with over 60 000
[00:11]
stars on github it provides state of the
[00:13]
art natural language processing models
[00:15]
and a very clean api that makes it super
[00:18]
simple to build powerful nlp pipelines
[00:20]
even for beginners so today i show you
[00:22]
how to get started with it i show you
[00:24]
how to use the pipeline how to use model
[00:27]
and tokenizer how to combine it with
[00:29]
pytorch or tensorflow how to save and
[00:32]
load models how to use models from the
[00:34]
official model hub and also how to fine
[00:36]
tune your own models so let's get
[00:38]
started
[00:40]
so first of all how do we install the
[00:42]
transformers library so the transformers
[00:45]
library should be combined with your
[00:46]
favorite deep learning library so this
[00:48]
could be pytorch or tensorflow or even
[00:51]
flex
[00:52]
so go ahead and install these first and
[00:55]
then you can install the transformers
[00:57]
library by saying pip install
[00:59]
transformers and that's all you need to
[01:01]
do
[01:03]
first let's have a look at the pipeline
[01:06]
so a pipeline makes it super simple to
[01:08]
apply an nlp task because it abstracts a
[01:11]
lot of things away for us and the way it
[01:14]
works is that we say from transformers
[01:16]
import pipeline then we create a
[01:19]
pipeline object so we say classifier
[01:21]
equals pipeline and here we put in a
[01:24]
task so in this case we want to do
[01:26]
sentiment analysis there are a lot of
[01:29]
more tasks available and we will have a
[01:31]
look at them in a moment but for now
[01:33]
let's do the sentiment analysis so we
[01:36]
create our object and then we apply this
[01:39]
classifier and here we put in the data
[01:41]
that we want to test so in this case we
[01:44]
only put in one string and the string is
[01:46]
i've been waiting for a hugging phase
[01:48]
course my whole life and then we print
[01:50]
the results so now let's run this and
[01:52]
see how the result looks like
[01:54]
all right and here's the result so we
[01:56]
see the label which is positive and we
[01:58]
also get a score so almost 96 percent
[02:02]
so yeah this is super cool and the way
[02:04]
this pipeline works is that it will do
[02:07]
three things for us so the first one is
[02:10]
the pre-processing so it's
[02:12]
pre-processing the text so in this case
[02:15]
it's applying a tokenizer then it feeds
[02:18]
the pre-processed text to the model then
[02:20]
it applies the model and then it also
[02:22]
does the post-processing so
[02:24]
post-processing means it will show us
[02:27]
the result how we would expect it so in
[02:29]
this case of a sentiment analysis
[02:31]
pipeline it for example shows us the
[02:34]
label positive or negative but it can
[02:36]
also look different for different tasks
[02:39]
so yeah that's how it works and now
[02:40]
let's look at a few other examples of
[02:42]
pipelines for example we can also use a
[02:45]
text generation pipeline and we can also
[02:48]
give it a specific model so in the first
[02:51]
example we just used the default model
[02:54]
which you can also see here in the
[02:56]
output but you can give it a specific
[02:59]
model either one that you have saved
[03:01]
locally or one from the model hub so we
[03:04]
will also have a look at this in a
[03:06]
moment so yeah let's apply this example
[03:09]
to generate some text and you can also
[03:12]
see there are different available
[03:14]
arguments so for this i just recommend
[03:16]
to check out the documentation so yeah
[03:19]
here's the result so we wanted to have
[03:21]
two possible
[03:23]
return sequences so the first generated
[03:26]
text is this one in this course we will
[03:28]
teach you how to play chess
[03:30]
or here's a second one in this course we
[03:33]
will teach you how to use a combination
[03:35]
of a traditional and simple blah blah
[03:37]
blah so yeah this also works and now
[03:40]
let's have a look at a third example for
[03:42]
example we can do zero shot
[03:44]
classification this means we can give it
[03:46]
a text without knowing the corresponding
[03:49]
label and then we put different
[03:51]
candidate labels for example this text
[03:54]
can be education politics or business
[03:57]
and then let's run this and see the
[03:59]
result and here we get the results so
[04:01]
all the different labels and the
[04:03]
different scores and the highest scores
[04:05]
with over 96
[04:07]
is the education which is correct so
[04:10]
let's have a look at the different other
[04:11]
available pipelines so for this i
[04:13]
recommend to go to the official
[04:15]
documentation and here you see all the
[04:18]
available tasks for example we can do
[04:20]
audio classification we can do automatic
[04:22]
speech recognition we can do image
[04:25]
classification question answering
[04:28]
and translation summarization so yeah
[04:31]
this is super cool and yeah i just
[04:32]
recommend to play around with different
[04:34]
ones and see how it looks like
[04:37]
now let's have a look behind the
[04:39]
pipeline and understand the different
[04:41]
steps a little bit better so for this we
[04:43]
have a look at a tokenizer and the model
[04:45]
class
[04:46]
so we can say
[04:48]
from transformers import auto tokenizer
[04:51]
and auto model for sequence
[04:53]
classification so this one here is a
[04:56]
very generic class and this is also a
[04:59]
generic class but a little bit more
[05:00]
specified for the sequence
[05:02]
classification task so for this i just
[05:04]
recommend to have a look at the official
[05:06]
documentation but for example if you
[05:08]
know you want a specific one there's for
[05:11]
example also a bird tokenizer class and
[05:14]
a bird model class
[05:17]
so
[05:18]
yeah so we import those classes and then
[05:20]
we create instances of this
[05:23]
so for this we specify a model name so
[05:27]
in this case this is just the default
[05:29]
model that is used for this pipeline and
[05:32]
then we call the model class and say dot
[05:35]
from pre-trained with the model name and
[05:38]
the same for the tokenizer and this from
[05:41]
pre-trained method is a very important
[05:43]
method in hugging phase that you will
[05:45]
see a lot of times so just keep this one
[05:48]
in mind and now that we have this we can
[05:51]
for example copy and paste the same code
[05:55]
and now for the pipeline we can say
[05:57]
model equals model and
[06:00]
tokenizer equals the
[06:03]
tokenizer
[06:05]
and now since this is just the same
[06:08]
default model this should produce the
[06:10]
very same result so let's run this and
[06:13]
have a look at the output and the result
[06:15]
is the very same like i said so this
[06:18]
works so yeah this is what's going on
[06:20]
under the hood so there will be a
[06:22]
tokenizer and a model so now let's have
[06:25]
a look at the tokenizer and see what
[06:27]
this is doing so a tokenizer basically
[06:30]
puts a text in a mathematical
[06:32]
representation that the model
[06:33]
understands and in order to use this we
[06:36]
can call the tokenizer directly and give
[06:39]
it a text as input or we can also put in
[06:42]
multiple texts as ones as a list
[06:45]
and we can so here we do this and print
[06:48]
this and we can also do this separately
[06:51]
so we can call
[06:53]
tokenizer.tokenize this will give us
[06:56]
tokens back then we can call
[06:58]
tokenizer.convert
[07:00]
tokens to ids this will give us the ids
[07:04]
and we can do it the other way around so
[07:06]
we can call tokenizer.dcodeids
[07:09]
and this will give us the original
[07:11]
stringback so let's run this and have a
[07:14]
look at the different outputs all right
[07:16]
so here we see the output so if we apply
[07:18]
the tokenizer directly then here we get
[07:21]
this dictionary and the dictionary
[07:24]
contains the input ids that look like
[07:27]
this then we also have a attention mask
[07:30]
so for now we don't have to worry about
[07:31]
this a attention mask basically is a
[07:34]
list of zeros and ones and a zero means
[07:37]
that the attention layer should ignore
[07:39]
this token then if we do this separately
[07:42]
so if we call tokenizer.tokenize
[07:46]
then here we see the different tokens
[07:48]
then if we convert the tokens to ids
[07:51]
then each token has a unique
[07:53]
corresponding id so we see this here and
[07:57]
if we decode this then we get the
[08:00]
original string back but here please
[08:02]
note that we
[08:04]
basically removed the capitalization but
[08:07]
yeah and now if we compare um
[08:11]
this one with this one
[08:13]
then you see this should be the very
[08:16]
same ids but here we also have this id
[08:20]
and this id so this means beginning of
[08:24]
sentence and end of sentence
[08:26]
but basically yeah it's the same and
[08:29]
yeah and this is how a tokenizer works
[08:33]
now let's see how we can combine the
[08:35]
code with pythog or tensorflow so in
[08:37]
this example we use pytorch but the code
[08:40]
is very similar with tensorflow
[08:42]
so with tensorflow usually we have a tf
[08:46]
before all those classes
[08:48]
and yeah in the first case i simply
[08:51]
apply the pipeline like before and now
[08:55]
we use multiple sentences so usually we
[08:58]
just put in one sentence but we can use
[09:01]
a list of all those sentences so we call
[09:03]
this our x train data and yeah here we
[09:07]
feed it to the pipeline classifier and
[09:09]
print the result and now we do this
[09:11]
separately
[09:13]
so first we call the tokenizer with the
[09:16]
x train data and we call this our batch
[09:20]
and
[09:21]
then we can give it different arguments
[09:23]
like padding equals true truncation
[09:25]
equals true max length equals 512
[09:29]
and return tensors equals pt so this
[09:32]
will be in pi torch format so you will
[09:35]
see how this looks in a moment because
[09:37]
we print the batch
[09:39]
so yeah usually we apply the tokenizer
[09:42]
directly instead of doing the
[09:44]
different functions separately
[09:46]
and then we do the inference in pytorch
[09:50]
so for this we say with torch dot no
[09:52]
grad then we call our model and here we
[09:56]
unpack this batch because this is a
[09: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