Natural Language Processing
Wednesday, June 16
11:15 am–1:00 pm
Connection Details: Lesson Room Link
— Go to Room 1 (NLP)
Heads-up
This session is conceptual rather than hands-on.
I will explain what machine learning, neural networks, deep learning, and NLP are and I will demo an example of using NLP for sentiment analysis, but you will not get to play with Python code.
Resources
- Stanza is a Python NLP package developed by the Stanford NLP Group
- Since many of you have interests in Russian literature, here is a set of interesting resources
- Speech and Language Processing is a book by Dan Jurafsky and James Martin with a free online version
- The CS224n: Natural Language Processing with Deep Learning Stanford University course has many resources available on its site
Concepts
Example
Let's go through an example of NLP using fastai. I am using here one of their demo examples.
Below is the code that I will run. Because we only have one GPU and because it would take an extremely long time to run this code on CPUs, unfortunately, you will have to watch and you won't be able to run this on your JupyterLab.
# First, we load the necessary functions from fastai
# Note that import * is appropriate with fastai
# but not a very good practice with other packages
from fastai.text.all import *
# Then we build our data loaders
# This is an object of the fastai class DataLoaders
# It contains a validation data loader and a training data loader
dls = TextDataLoaders.from_folder(
'/project/def-sponsor00/dhsi/.fastai/data/imdb',
valid='test',
bs=16
)
# Let's have a look at the data
dls.show_batch()
# Now, we define our learner
# AWD_LSTM is the architecture (https://arxiv.org/abs/1708.02182)
learn = text_classifier_learner(dls, AWD_LSTM, drop_mult=0.5, metrics=accuracy)
# Finally, we fine-tune the pretrained model with our data
# over 2 epochs with a learning rate of 1e-2
learn.fine_tune(2, 1e-2)
# Then we can use our model to predict whether sentences are positive or negative
learn.predict("This is amazing.")
learn.predict("I love it.")
learn.predict("This is terrible.")
I don't understand what neural networks really are!
If you want to really understand what neural networks are and how they work, I really recommend you to watch the 4 videos below from 3Blue1Brown by Grant Sanderson . They are easy to watch, fun, and do an excellent job at introducing the functioning of a simple neural network.
What neural networks really are
For those of you who will develop NLP models, if you find that your mathematical background is shaky, 3blue1brown also has an excellent series of videos on linear algebra and an equally great one on calculus.
How neural networks learn
More technical: how backpropagation works
There is one minor terminological error in this video: they call the use of mini-batches stochastic gradient descent. In fact, this is called mini-batch gradient descent. Stochastic gradient descent uses a single example at each iteration.
Even more nerdy: the calculus of backpropagation