08 Natural Language Processing in Tensorflow
08 Natural Language Processing in Tensorflow
with
Where can you get help?
“If in doubt, run the code”
Machine learning
Representation learning
Arti cial intelligence
Input
Input
Input
Source: https://www.coindesk.com/price/bitcoin
• Using a model we’ve built to make predictions on text from the wild
👩🍳 👩🔬
(w e’ ll be co ok ing u p lots of co d e! )
How:
NLP inputs and outputs
Diaster 🌪
Not Diaster 👌
“Is this Tweet for a disaster or not?”
Actual output
🌪 👌
[[0.22, 0.98, 0.02…],
[[0.97, 0.03],
[0.09, 0.55, 0.87…],
[0.81, 0.19],
[0.53, 0.81, 0.79…],
…,
…,
👌 🌪
[0.99, 0.01]
i o n p r ob ab i l i t i e s )
(predict
1. Turn all data into numbers (neural networks can’t handle text/natural language)
2. Make sure all of your tensors are the right shape (pad sequences which don’t t)
fi
“What is a recurrent neural
network (RNN)?”
(typical)*
Architecture of an RNN
Not Diaster 👌
*Note: there are almost an unlimited amount of ways you could stack together a recurrent neural network, this slide demonstrates only one.
Let’s code!
Tokenization vs Embedding
I=0
I love TensorFlow 0 1 2 love = 1
TensorFlow = 2
Tokenization — straight [[1, 0, 0],
mapping from token to
number (can be modelled but
[0, 1, 0], One-hot
Encoding
quickly gets too big) [0, 0, 1],
…,
Embedding — richer
representation of [[0.492, 0.005, 0.019],
relationships between tokens [0.060, 0.233, 0.899], Embedding
(can limit size + can be [0.741, 0.983, 0.567],
learned) …,
Experiments we’re running
Experiment Number Model
0 Naive Bayes with TF-IDF encoder (baseline)
2 LSTM (RNN)
3 GRU (RNN)
4 Bidirectional-LSTM (RNN)
Standard RNN
Types of RNN cells
Name When to use Learn more Code
LSTM (long short-term Default RNN layer for sequence Understanding LSTM Networks
tf.keras.layers.LSTM
memory) problems. by Chris Olah
Encoder Decoder
(Encodes sequences into (Decodes sequences into
numerical representation) desired output)
Ideal speed/performance trade off
Smaller model
For example, a student who studies the course materials too hard and then isn’t able to perform
well on the nal exam. Or tries to put their knowledge into practice at the workplace and nds
what they learned has nothing to do with the real world.
Not all data samples are created equally. Removing poor samples
Better data from or adding better samples to your dataset can improve your
model’s performance.
Training
Predictions
The machine learning practitioner’s
motto
👩🍳 👩🔬
(try lots of things an
d see what
tastes good)