NLP/NLU Specialization Notes
  • Natural Language Processing/Understanding Notes, Resources
  • Natural Language Processing With Attention Models
    • NLP With Attention Models
  • Natural Language Processing With Attention Models Notebooks
    • Assignment 1: Neural Machine Translation
    • Basic Attention Operation: Ungraded Lab
    • Calculating the Bilingual Evaluation Understudy (BLEU) score: Ungraded Lab
    • Scaled Dot-Product Attention: Ungraded Lab
    • Assignment 2: Transformer Summarizer
    • The Three Ways of Attention and Dot Product Attention: Ungraded Lab Notebook
    • Masking
    • Positional Encoding
    • Assignment 3: Question Answering
    • Assignment 3: Question Answering
    • Question Answering with BERT and HuggingFace
    • Question Answering with BERT and HuggingFace 🤗 (Fine-tuning)
    • SentencePiece and BPE
  • Natural Language Processing With Classification And Vector Spaces
    • NLP With Classification and Vector Spaces
  • Natural Language Processing With Sequence Models
    • NLP With Sequence Models
  • Natural Language Processing with Classification and Vector Spaces Notebooks
    • Assignment 1: Logistic Regression
    • Preprocessing
    • Building and Visualizing word frequencies
    • Visualizing tweets and the Logistic Regression model
    • Assignment 2: Naive Bayes
    • Assignment 3: Hello Vectors
    • Linear algebra in Python with NumPy
    • Manipulating word embeddings
    • Another explanation about PCA
    • Assignment 4 - Naive Machine Translation and LSH
    • Vector manipulation in Python
    • Hash functions and multiplanes
  • Natural Language Processing with Probabilistic Models
    • NLP With Probabilistic Models
  • Natural Language Processing with Probabilistic Models Notebooks
    • Assignment 1: Autocorrect
    • NLP Course 2 Week 1 Lesson : Building The Model - Lecture Exercise 01
    • NLP Course 2 Week 1 Lesson : Building The Model - Lecture Exercise 02
    • Assignment 2: Parts-of-Speech Tagging (POS)
    • Parts-of-Speech Tagging - First Steps: Working with text files, Creating a Vocabulary and Handling Unknown Words
    • Parts-of-Speech Tagging - Working with tags and Numpy
    • Assignment 3: Language Models: Auto-Complete
    • N-grams Corpus preprocessing
    • Building the language model
    • Out of vocabulary words (OOV)
    • Assignment 4: Word Embeddings
    • Word Embeddings First Steps: Data Preparation
    • Word Embeddings: Intro to CBOW model, activation functions and working with Numpy
    • Word Embeddings: Training the CBOW model
    • Word Embeddings: Hands On
    • Word Embeddings: Ungraded Practice Notebook
  • Natural Language Processing with Sequence Models Notebooks
    • Assignment 1: Deep N-grams
    • Hidden State Activation : Ungraded Lecture Notebook
    • Assignment 1: Sentiment with Deep Neural Networks
    • Vanilla RNNs and GRUs
    • Lab 1: TensorFlow Tutorial and Some Useful Functions
    • Calculating perplexity using numpy: Ungraded Lecture Notebook
    • Assignment 2 - Named Entity Recognition (NER)
    • Vanishing Gradients and Exploding Gradients in RNNs : Ungraded Lecture Notebook
    • Evaluate a Siamese model: Ungraded Lecture Notebook
    • Assignment 3: Question duplicates
    • Modified Triplet Loss : Ungraded Lecture Notebook
    • Creating a Siamese model: Ungraded Lecture Notebook
  • Stanford
    • Stanford CS 224U,224N
  • Udacity
    • NLP Nanodegree
  • Previous
  • Next
  • Creating a Siamese model: Ungraded Lecture Notebook
    • Siamese Model

Creating a Siamese model: Ungraded Lecture Notebook¶

In this notebook you will learn how to create a siamese model in TensorFlow.

In [2]:
Copied!
import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras.models import Model, Sequential
from tensorflow import math
import numpy

# Setting random seeds
numpy.random.seed(10)
import tensorflow as tf from tensorflow.keras import layers from tensorflow.keras.models import Model, Sequential from tensorflow import math import numpy # Setting random seeds numpy.random.seed(10)

Siamese Model¶

To create a Siamese model you will first need to create a LSTM model. For this you can stack layers using theSequential model. To retrieve the output of both branches of the Siamese model, you can concatenate results using the Concatenate layer. You should be familiar with the following layers (notice each layer can be clicked to go to the docs):

  • Sequential groups a linear stack of layers into a tf.keras.Model

  • Embedding Maps positive integers into vectors of fixed size. It will have shape (vocabulary length X dimension of output vectors). The dimension of output vectors (called model_dimensionin the code) is the number of elements in the word embedding.

  • LSTM The Long Short-Term Memory (LSTM) layer. The number of units should be specified and should match the number of elements in the word embedding.

  • GlobalAveragePooling1D Computes global average pooling, which essentially takes the mean across a desired axis. GlobalAveragePooling1D uses one tensor axis to form groups of values and replaces each group with the mean value of that group.

  • Lambda Layer with no weights that applies the function f, which should be specified using a lambda syntax. You will use this layer to apply normalization with the function

    • tfmath.l2_normalize(x)
  • Concatenate Layer that concatenates a list of inputs. This layer will concatenate the normalized outputs of each LSTM into a single output for the model.

  • Input: it is used to instantiate a Keras tensor.. Remember to set correctly the dimension and type of the input, which are batches of questions.

Putting everything together the Siamese model will look like this:

In [3]:
Copied!
vocab_size = 500
model_dimension = 128

# Define the LSTM model
LSTM = Sequential()
LSTM.add(layers.Embedding(input_dim=vocab_size, output_dim=model_dimension))
LSTM.add(layers.LSTM(units=model_dimension, return_sequences = True))
LSTM.add(layers.AveragePooling1D())
LSTM.add(layers.Lambda(lambda x: math.l2_normalize(x)))

input1 = layers.Input((None,))
input2 = layers.Input((None,))

# Concatenate two LSTMs together
conc = layers.Concatenate(axis=1)((LSTM(input1), LSTM(input2)))
    

# Use the Parallel combinator to create a Siamese model out of the LSTM 
Siamese = Model(inputs=(input1, input2), outputs=conc)

# Print the summary of the model
Siamese.summary()
vocab_size = 500 model_dimension = 128 # Define the LSTM model LSTM = Sequential() LSTM.add(layers.Embedding(input_dim=vocab_size, output_dim=model_dimension)) LSTM.add(layers.LSTM(units=model_dimension, return_sequences = True)) LSTM.add(layers.AveragePooling1D()) LSTM.add(layers.Lambda(lambda x: math.l2_normalize(x))) input1 = layers.Input((None,)) input2 = layers.Input((None,)) # Concatenate two LSTMs together conc = layers.Concatenate(axis=1)((LSTM(input1), LSTM(input2))) # Use the Parallel combinator to create a Siamese model out of the LSTM Siamese = Model(inputs=(input1, input2), outputs=conc) # Print the summary of the model Siamese.summary()
Model: "model"
__________________________________________________________________________________________________
 Layer (type)                Output Shape                 Param #   Connected to                  
==================================================================================================
 input_1 (InputLayer)        [(None, None)]               0         []                            
                                                                                                  
 input_2 (InputLayer)        [(None, None)]               0         []                            
                                                                                                  
 sequential (Sequential)     (None, None, 128)            195584    ['input_1[0][0]',             
                                                                     'input_2[0][0]']             
                                                                                                  
 concatenate (Concatenate)   (None, None, 128)            0         ['sequential[0][0]',          
                                                                     'sequential[1][0]']          
                                                                                                  
==================================================================================================
Total params: 195584 (764.00 KB)
Trainable params: 195584 (764.00 KB)
Non-trainable params: 0 (0.00 Byte)
__________________________________________________________________________________________________

Next is a helper function that prints information for every layer:

In [4]:
Copied!
def show_layers(model, layer_prefix):
    print(f"Total layers: {len(model.layers)}\n")
    for i in range(len(model.layers)):
        print('========')
        print(f'{layer_prefix}_{i}: {model.layers[i]}\n')

print('Siamese model:\n')
show_layers(Siamese, 'Parallel.sublayers')

print('Detail of LSTM models:\n')
show_layers(LSTM, 'Serial.sublayers')
def show_layers(model, layer_prefix): print(f"Total layers: {len(model.layers)}\n") for i in range(len(model.layers)): print('========') print(f'{layer_prefix}_{i}: {model.layers[i]}\n') print('Siamese model:\n') show_layers(Siamese, 'Parallel.sublayers') print('Detail of LSTM models:\n') show_layers(LSTM, 'Serial.sublayers')
Siamese model:

Total layers: 4

========
Parallel.sublayers_0: <keras.src.engine.input_layer.InputLayer object at 0x7fa6d87eb8e0>

========
Parallel.sublayers_1: <keras.src.engine.input_layer.InputLayer object at 0x7fa6d87eab60>

========
Parallel.sublayers_2: <keras.src.engine.sequential.Sequential object at 0x7fa6f3913730>

========
Parallel.sublayers_3: <keras.src.layers.merging.concatenate.Concatenate object at 0x7fa6d87eb160>

Detail of LSTM models:

Total layers: 4

========
Serial.sublayers_0: <keras.src.layers.core.embedding.Embedding object at 0x7fa6f3913760>

========
Serial.sublayers_1: <keras.src.layers.rnn.lstm.LSTM object at 0x7fa6f3912410>

========
Serial.sublayers_2: <keras.src.layers.pooling.average_pooling1d.AveragePooling1D object at 0x7fa6d87eb880>

========
Serial.sublayers_3: <keras.src.layers.core.lambda_layer.Lambda object at 0x7fa6d87eb0d0>

Try changing the parameters defined before the Siamese model and see how it changes!

You will actually train this model in this week's assignment. For now you should be more familiarized with creating Siamese models using TensorFlow. Keep it up!

In [ ]:
Copied!


Documentation built with MkDocs.

Keyboard Shortcuts

Keys Action
? Open this help
n Next page
p Previous page
s Search