Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Exercise 0 1 Instructions: Implement the Siamese function below. You should be using all the functions explained below. To implement this model, you will be
Exercise
Instructions: Implement the Siamese function below. You should be using all the functions explained below.
To implement this model, you will be using TensorFlow. Concretely, you will be using the following functions.
tfkeras.models.Sequential: groups a linear stack of layers into a tfkeras.Model.
You can pass in the layers as arguments to Serial, separated by commas, or simply instantiate the Sequentialmodel and use the add method to add layers.
For example: SequentialEmbeddings AveragePoolingD Dense Softmax or
model Sequential model.addEmbeddings model.addAveragePoolingD model.addDense model.addSoftmax
tfkeras.layers.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 dfeaturein the model is the number of elements in the word embedding.
Embeddinginputdim, outputdim
inputdim is the number of unique words in the given vocabulary.
outputdim is the number of elements in the word embedding some choices for a word embedding size range from to for example
tfkeras.layers.LSTM : The LSTM layer. The number of units should be specified and should match the number of elements in the word embedding.
LSTMunits Builds an LSTM layer of nunits.
tfkeras.layers.GlobalAveragePoolingD : Computes global average pooling, which essentially takes the mean across a desired axis. GlobalAveragePoolingD uses one tensor axis to form groups of values and replaces each group with the mean value of that group.
GlobalAveragePoolingD takes the mean.
tfkeras.layers.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.lnormalizex
tfkeras.layers.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. For this, keep in mind that each question is a single string.
Inputinputshape,dtypeNone,...
inputshape: Shape tuple not including the batch axis
dtype: optional data type of the input
tfkeras.layers.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.
Concatenate
# GRADED FUNCTION: Siamese
def Siamesetextvectorizer, vocabsize dfeature:
Returns a Siamese model.
Args:
textvectorizer TextVectorization: TextVectorization instance, already adapted to your training data.
vocabsize int optional: Length of the vocabulary. Defaults to
dmodel int optional: Depth of the model. Defaults to
Returns:
tfmodel.Model: A Siamese model.
### START CODE HERE ###
branch tfkeras.models.Sequentialname'sequential'
# Add the textvectorizer layer. This is the textvectorizer you instantiated and trained before
branch.addNone
# Add the Embedding layer. Remember to call it 'embedding' using the parameter name
branch.addNone
# Add the LSTM layer, recall from W that you want to the LSTM layer to return sequences, ot just one value.
# Remember to call it LSTM using the parameter name
branch.addNone
# Add the GlobalAveragePoolingD layer. Remember to call it 'mean' using the parameter name
branch.addNone
# Add the normalizing layer using the Lambda function. Remember to call it 'out' using the parameter name
branch.addNone
# Define both inputs. Remember to call then 'input and 'input using the name parameter.
# Be mindful of the data type and size
input tfkeras.layers.InputNone dtypeNone, name'input
input tfkeras.layers.InputNone dtypeNone, name'input
# Define the output of each branch of your Siamese network. Remember that both branches have the same coefficients,
# but they each receive different inputs.
branch None
branch None
# Define the Concatenate layer. You should concatenate columns, you can fix this using the axisparameter
# This layer is applied over the outputs of each branch of the Siamese network
conc tfkeras.layers.ConcatenateaxisNone, name'concNone None
### END CODE HERE ###
return tfkeras.models.Modelinputsinput input outputsconc, name"SiameseModel"
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started