3.1


Embedded Layers and NLP Models

  • NLP = Natural language processing model designed to understand and process human language

    • analyzing what was said

    • make new conversations

  • Tokenizing = series of steps to prepare text for NLP model

    • 1. Tokenize words by converting words to numerical values

    • 2. Creating numerical sequences from input sentences

    • 3. Adjust sequences to be the same length

  • Examples

    • Sentiment Model

      • analysis

      • ex: store review and determine if it is positive or negative

    • Supervised model

      • labels are used with training data to establish relationship between input and output

        • ex; Label is positive and text is “it was great”

        • model can now determine what words are used in a positive vs negative review

  • Text can be represented as a point in a graph

  • Input data for NLP is generally low-frequency and high dimensions→ not a lot of common words and little organization to the words

  • NLP supervised model uses embedded layer to group similar words together to form cluster → fewer dimensions and higher frequency in each dimension

  • ex: cluster of good words are on one side, negative are on other sides and ambiguous words are in middle

  • mapping→ model will take review and predict it to be + or - based on mapping

  • Basic NLP supervised Model

model = tf.keras.Sequential ( [ 
    tf.keras.layers.Embedding ( //layer that looks for similarities in words
        vocab_size, 
        embedding_dimensions, //integer that determines # dimension to make the model 
        input_length=max_length
        ), 
    tf.keras.layers.Flatten(), 
    tf.keras.layers.Dense(6, activation = 'relu'), 
    tf.keras.layers.Dense(1, activation='sigmoid')  //final layer with one node
)]
  • the more dimensions you have, the more complex the model = similar to nodes

  • Final output is decimal between 0 to 1 , how positive or negative

model.fit(padded, 
            training_labels_final, 
            epochs=num_epochs, 
            validation_data= 
            (testing_padded, 
            testing_labels_final))
  • first two parameters are input data represented by list of padded reviews and sentiment value for each of the reviews

  • validation data has list for review texts and list for review labels


  • review that you want to predict still has to go through tokenizer and pad sequences.

  • use model predict command for model to be scored ( 0 is neg and 1 is pos )

test_reviews = [ 'I love this phone'] 
test_sequences = tokenizer.texts_to_sequences(test_reviews_ 

test_padded = pad_sequences(test_sequences, padding = padding_type, maxlen=max_length ) 

classes = model.predict(test_padded) 
print( classes [ 0])



3.2 Recurrent Neural Network

  • = a form of neural network that uses a feedback loop to help memorize previous inputs

  • loses long term and short term memory components to memorize par t of the input to be used later down

  • bidirectional layer with RNN model, to use feedback from earlier words to later words, but also from later words to earlier words

  • Used to help associate words together

  • ex: double negatives are associated with a positive

  • nearly identical to embedding models

  • remove flattening layer and replace with a bidirectional LSTM ( long short term memory ) layer

model = tf.keras.Sequential ( [ 
    tf.keras.layers.Embedding ( //layer that looks for similarities in words
        vocab_size, 
        embedding_dimensions, //integer that determines # dimension to make the model 
        input_length=max_length
        ), 
    tf.keras.layers.Bidirectional( tf.keras.layers.LSTM(embedding_dim)), 
    tf.keras.layers.Dense(6, activation = 'relu'), 
    tf.keras.layers.Dense(1, activation='sigmoid')  //final layer with one node
)]
  • LSTM layer is parameter for bidirectional layers

  • often there will be two bidirectional layers in the model, → first layer needs a parameter to specify return_sequences as true so that model can pass output values from each step to the next layer , without this only the last value is passed down

  • Bidirectional RNN models are good for analyzing unsupervised text generating models

    • model learns patterns of words with no particular sentiment or category associated with the text

  • training data is unsupervised data, because model learns patterns of words without labels

  • bidirectional RNN model can predict next words in a sentence


  • N gram =a group of words that appear together in a sentence or a text

    • Example : 2 gram for “ The cat sat on the mat “ would be “the cat” “cat sat “ “sat on “ “on the “ “ the mat “

    • a 3 gram would be “ The cat sat “ “ cat sat on “ “ sat on the “ “on the mat”