Slide 1

Slide 1 text

¡Escuincla babosa!: Creating a telenovela script in three Python deep learning frameworks Lorena Mesa @loooorenanicole SLIDES @ http:/ /bit.ly/lmesa-pycon2019

Slide 2

Slide 2 text

HOLA! Soy Lorena Mesa. I am here because I am really interested in deep learning and wanted to start deep diving into the topic more. Also, I love telenovelas. Win win? 2 @loooorenanicole

Slide 3

Slide 3 text

3

Slide 4

Slide 4 text

How many of you saw this? Enter the creativity of deep learning. SLIDES @ http:/ /bit.ly/lmesa-pycon2019

Slide 5

Slide 5 text

5

Slide 6

Slide 6 text

6

Slide 7

Slide 7 text

Why telenovelas? Also … what is a telenovela?

Slide 8

Slide 8 text

8

Slide 9

Slide 9 text

“The social phenomenon of televised melodramas, called telenovelas in Spanish … are serial melodramas … [based] on the construction of a relationship of fidelity with the audience” - Jorge Gonzalez, 2003, Understanding Telenovelas as a Cultural Front 9

Slide 10

Slide 10 text

10 2 billion People watch telenovelas or approximately ⅓ of the global population (Telegraph - 2006)

Slide 11

Slide 11 text

The arc of a telenovela El drama es real

Slide 12

Slide 12 text

“Things have to be cleaned up so the audience has satisfaction. They won’t worry about Maria — did she find true love, her true mother or her true father?” - Dr. Diana Rios, Associate Professor of Communications, University of Connecticut 12

Slide 13

Slide 13 text

The arc of a telenovela Unlike English soap operas telenovelas are defined by: - A fixed melodramatic plot (e.g. love lost, mothers and daughters fighting, long-lost relatives, love found) - A finite beginning and end - A conclusion that ties up loose ends generally with a happy element (e.g. big wedding) 13

Slide 14

Slide 14 text

Examples of Telenovelas Spanish and English SLIDES @ http:/ /bit.ly/lmesa-pycon2019

Slide 15

Slide 15 text

15

Slide 16

Slide 16 text

16

Slide 17

Slide 17 text

Where does deep learning fit in? Text generation is a deep learning problem. SLIDES @ http:/ /bit.ly/lmesa-pycon2019

Slide 18

Slide 18 text

“A field of study that gives computers the ability to learn without being explicitly programmed” - Arthur Samuel, pioneer in Machine Learning (1959) 18

Slide 19

Slide 19 text

19 Landscape of Machine Learning Source: https:/ /www.packtpub.com/mapt/book/big_data_and_business_intelligence/9781784399689/1/ch01lvl1sec08/machine-learning

Slide 20

Slide 20 text

“A computer program is said to learn from experience (E) with respect to some task (T) and some performance measure (P), if its performance on T, as measured by P, improves with experience E.” - Tom Mitchell , Machine Learning (1997) 20

Slide 21

Slide 21 text

21 Source: https:/ /www.embedded-vision.com/industry-analysis/blog/artificial-intelligence-machine-learning-deep-learning-and-computer-visionwha

Slide 22

Slide 22 text

22 Source: http:/ /datasciencegyan.com/artificial-intelligence-vs-machine-learning-vs-de ep-learning/ Source: https:/ /www.iflscience.com/technology/artificial-intelligence-dreams/

Slide 23

Slide 23 text

Deep Learning: Classification Programming a flashlight to turn on with audible cue “dark” A deep learning implementation can learn: - Phrases with word “dark” - Other cues: “I can’t see” Learns via the “brain”, the activation method 23

Slide 24

Slide 24 text

Deep Learning: Text Generation Sequence processing has applications in several areas Two model approaches: - Character model - Word model Learns from the text corpus provided the flavor and feel of the text to generate its own 24

Slide 25

Slide 25 text

Neural networks vs deep learning: What’s the difference? “The difference between a neural network and deep learning is that deep learning is the act of using a subset of neural networks called deep neural networks” - What is a Neural Network and How are Businesses Using Them, Erika Morphy (May 2018) 25

Slide 26

Slide 26 text

26 Source: https:/ /hackernoon.com/a-hands-on-introduction-to-neural-networks-6a03afb468b1 What is a neuron?

Slide 27

Slide 27 text

Foundation of a neuron Neuron model: - inputs: Features represented as a weighted number represented as (x1 … xn) - Weights: Strength of the feature represented as (w1j .. wnj) - Outputs: Weighted sum of inputs passed (nj) through the activation function TLDR; a neuron is a mathematical function 27

Slide 28

Slide 28 text

The Activation Function Activation function models the “firing rate” of a biological neuron, converting weighted sum into a new number based on a formula. Training the neuron means: - The neuron takes an initial guess at classifying sample - Update the neuron’s feature weights with the activation function - Repeat 28

Slide 29

Slide 29 text

The many architectural types of Neural Networks Examples of types: - Feedforward - information flows only moves one way, from input layer to output layer - Radial basis function - Includes a distance criterion - Recurrent neural network - propagates data forward and backwards, from later processing stages back to earlier processing stages; it’s a directed graph! 29

Slide 30

Slide 30 text

30 Source: http:/ /neuralnetworksanddeeplearning.com/chap5.html

Slide 31

Slide 31 text

Making an RNN in Python There’s more than one way to do it! SLIDES @ http:/ /bit.ly/lmesa-pycon2019

Slide 32

Slide 32 text

Python and Deep Learning Frameworks Python is an ideal language to use for scientific programming! Don’t believe me? See Jake Vanderplas “The Unexpected Success of Python in Science” PyCon 2017 keynote! Keras TensorFlow PyTorch 32

Slide 33

Slide 33 text

Which framework to choose? 33 What’s important for your use case? Consider these three guiding questions: 1. How much technical expertise is needed to start using the framework? 2. What are your requirements (e.g. speed, dataset size)? 3. How easy is the framework to start working with?

Slide 34

Slide 34 text

Which framework to choose? 34 How much technical expertise is needed to start using the framework? + Beginner with deep learning + Something that allows us to build a small toy project quickly + Has good developer community and demos

Slide 35

Slide 35 text

Which framework to choose? 35 What are your requirements (e.g. speed, dataset size)? + Working with < 100 megabytes of data (only 3 seasons Queen of the South, 4 seasons of Ugly Betty, 5 seasons Jane the Virgin) + Working with text data + Need something that has debugging functionality

Slide 36

Slide 36 text

Which framework to choose? 36 How easy is the framework to start working with? + Want something that doesn’t require a lot of setup + Has sensical debugging capacity + Has built in bootstrapping tools + Doesn’t require a lot of deep mathematical knowledge

Slide 37

Slide 37 text

Which framework to choose? 37 Keras - Can run on top of TensorFlow - High-level API - Slower vs TensorFlow & PyTorch - Prioritizes readability - Better suited for smaller datasets TensorFlow - Both high and low-level APIs - About same as PyTorch - Can use Keras thereby making architecture simpler - Good fit for larger datasets PyTorch - Low-level API - Works directly on array expressions - Good traction in academia - Same speed as TensorFlow - More complex architecture - Less readable - Good fit for larger datasets

Slide 38

Slide 38 text

Which framework to choose? 38 Keras - Can run on top of TensorFlow - High-level API - Slower vs TensorFlow & PyTorch - Prioritizes readability - Better suited for smaller datasets TensorFlow - Both high and low-level APIs - About same as PyTorch - Can use Keras thereby making architecture simpler - Good fit for larger datasets PyTorch - Low-level API - Works directly on array expressions - Good traction in academia - Same speed as TensorFlow - More complex architecture - Less readable - Good fit for larger datasets

Slide 39

Slide 39 text

Which framework to choose? 39 Keras - Can run on top of TensorFlow - High-level API - Slower vs TensorFlow & PyTorch - Prioritizes readability - Better suited for smaller datasets

Slide 40

Slide 40 text

Making an RNN in Python Framework #1: Keras SLIDES @ http:/ /bit.ly/lmesa-pycon2019

Slide 41

Slide 41 text

41 Hot Encoding Allows Model to Predict the Probability of Each Character in the Vocabulary Jupyter Notebook here! Steps to creating an RNN Fit model during training epochs Generate character hot encodings Transform data to input sequences and rescale [0,1]

Slide 42

Slide 42 text

42 Hot Encoding Allows Model to Predict the Probability of Each Character in the Vocabulary Character One-Hot Vector Encoding Corpus: [‘a’, ‘b’, ‘c’, ‘d, ‘e’, ‘f’, g’, ‘h’, ‘i’] — len(Corpus) = 9 Word: [‘bad’] → [‘b’, ‘a’, ‘d’] b → [0, 1, 0, 0, 0, 0, 0, 0, 0], a → [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], d → [0, 0, 0, 1, 0, 0, 0, 0, 0]

Slide 43

Slide 43 text

43 Building an RNN Long Short Term Memory (LSTM) Model with Keras from keras.models import Sequential from keras.layers import Dense, Dropout, LSTM from keras.callbacks import ModelCheckpoint from keras.utils import np_utils model = Sequential() model.add(LSTM(256, input_shape=(X.shape[1], X.shape[2]))) model.add(Dropout(0.2)) model.add(Dense(y.shape[1], activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam') model.fit(X, y, epochs=20, batch_size=128, callbacks=callbacks_list)

Slide 44

Slide 44 text

44 Optimizing Weights with Keras # pick a random seed pattern = dataX[numpy.random.randint(0, len(dataX)-1)] # generate characters for i in range(1000): x = numpy.reshape(pattern, (1, len(pattern), 1)) x = x / float(n_vocab) prediction = model.predict(x, verbose=0) index = numpy.argmax(prediction) result = int_to_char[index] seq_in = [int_to_char[value] for value in pattern] sys.stdout.write(result) pattern.append(index) pattern = pattern[1:len(pattern)]

Slide 45

Slide 45 text

Making an RNN in Python Framework #2: TensorFlow SLIDES @ http:/ /bit.ly/lmesa-pycon2019

Slide 46

Slide 46 text

46 Building an RNN Long Short Term Memory (LSTM) Model with TensorFlow import tensorflow as tf from tensorflow.contrib import rnn def RNN(x, weights, biases): x = tf.reshape(x, [-1, n_input]) x = tf.split(x,n_input,1) # Generate n_input sequence of inputs rnn_cell = rnn.BasicLSTMCell(n_hidden) # Make prediction outputs, states = rnn.static_rnn(rnn_cell, x, dtype=tf.float32) # We only want the last output of the n outputs return tf.matmul(outputs[-1], weights['out']) + biases['out']

Slide 47

Slide 47 text

47 Optimizing Weights with TensorFlow with tf.Session() as session: session.run([init_op]) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) saver = tf.train.Saver() for epoch in range(num_epochs): for step in range(training_input.epoch_size): . . . logic for optimizing weights to use for text generation saver.save(session, data_path + '\\' + model_save_name, global_step=epoch) saver.save(session, data_path + '\\' + model_save_name + '-final') coord.request_stop() # close threads coord.join(threads)

Slide 48

Slide 48 text

Making an RNN in Python Framework #3: PyTorch SLIDES @ http:/ /bit.ly/lmesa-pycon2019

Slide 49

Slide 49 text

49 Building an RNN Long Short Term Memory (LSTM) Model with PyTorch import torch from torch import nn class CharRNN(nn.Module): def __init__(self, input_size, hidden_size, output_size): self.lstm = nn.LSTM(len(self.chars), n_hidden, n_layers, dropout=drop_prob, batch_first=True) # Set attrs related to hidden, dropout, connected layers, etc. def forward(self, category, input, hidden): r_output, hidden = self.lstm(x, hidden) # Get outputs from hidden output = self.dropout(r_output) # Run through dropout layer out = out.contiguous().view(-1, self.n_hid) # Add LSTM outputs up out = self.fc(out) # Run through connect layer return output, hidden

Slide 50

Slide 50 text

50 Optimizing Weights with PyTorch net.train() opt = torch.optim.Adam(net.parameters(), lr=lr) criterion = nn.CrossEntropyLoss() for e in range(epochs): h = net.init_hidden(batch_size) # initialize hidden state inputs, targets = torch.from_numpy(x), torch.from_numpy(y)# make torches h = tuple([each.data for each in h]) # New h to prevent backpropagation output, h = net(inputs, h) # Get output # Calculate loss, keep the output with optimal performance

Slide 51

Slide 51 text

Generating a telenovela “script” with an RNN What’s possible? SLIDES @ http:/ /bit.ly/lmesa-pycon2019

Slide 52

Slide 52 text

Sample Text Generated: “do not threaten me. do you understand me? do you understand me? whoa, whoa, all right? hey, look, i” 52

Slide 53

Slide 53 text

Approaches for creating telenovela deep learning scripts Improvements: - Make a LSTM model per character you want in your script - Find similar enough “sources of inspiration” per character - Weave all these models together? - Generate LSTM models for telenovelas of each type of melodrama (e.g. lost love) 53

Slide 54

Slide 54 text

Text generation is hard. Generating a “learned” plot may be possible, but without coherent narratives and emotionally compelling characters we miss the mark. 54

Slide 55

Slide 55 text

How can we do better? Telenovelas may have a format, but text generation is complex. SLIDES @ http:/ /bit.ly/lmesa-pycon2019

Slide 56

Slide 56 text

Varying Data Quality Queen of the South (2016) s01e01 Episode Script 1 (helicopter blades whirring) (upbeat music) TERESA: My name is Teresa Mendoza. I am from México. I was born poor, not that that's bad. But take it from me, I've been poor. And I've been rich. Rich is better. Believe me. 01x01 - Chapter One (Pilot) 01x01 - Chapter One (Pilot) Latin lover narrator: Our story begins 13 and a half years ago, when Jane Gloriana Villanueva was a mere ten years old. It should be noted that at a mere ten years old, Jane's passions include... in no particular order... her family, God, and grilled cheese sandwiches. This is Jane's grandmother, Alba Gloriana Villanueva. Her passions include God and Jane, in that particular order. Woman: Really, Mom? Shh. But this is so lame. 56

Slide 57

Slide 57 text

Let’s generate more data! Want to contribute to my telenovela sets? Please do! Add at http:/ /bit.ly/telenovelas-for-all! 57

Slide 58

Slide 58 text

58 Source: https:/ /medium.freecodecamp.org/chihuahua-or-muffin-my-search-for-the-best-computer-vision-api-cbda4d6b425d

Slide 59

Slide 59 text

59 Thwarting deep learning with CVZazzle and One Pixel Attacks.

Slide 60

Slide 60 text

Want to keep learning? Great! SLIDES @ http:/ /bit.ly/lmesa-pycon2019

Slide 61

Slide 61 text

Resources 61 Blogs + Articles - Develop your first NN in Python with Keras Step-by-Step A NN in 11 lines of Python - How to build a 3 layer neural network from scratch - What is the best alternative to Keras? Videos + MOOCs - 10.4 NN: Multilayer Perceptron Part 1: The Nature of Code - Exploring Deep Learning Framework in PyTorch - Stephanie Kim PyCon USA 2018 - Mathematics for Machine Learning: Linear Algebra - Fast.ai

Slide 62

Slide 62 text

¡Gracias! You can find me at: @loooorenanicole (social) [email protected] (email) 62 Also, if you own Maria la del Barrio, please find me!