Answered step by step
Verified Expert Solution
Link Copied!

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 01
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.
tf.keras.models.Sequential: groups a linear stack of layers into a tf.keras.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: Sequential(Embeddings(...), AveragePooling1D(...), Dense(...), Softmax(...)) or
model = Sequential() model.add(Embeddings(...)) model.add(AveragePooling1D(...)) model.add(Dense(...)) model.add(Softmax(...))
tf.keras.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 d_featurein the model) is the number of elements in the word embedding.
Embedding(input_dim, output_dim).
input_dim is the number of unique words in the given vocabulary.
output_dim is the number of elements in the word embedding (some choices for a word embedding size range from 150 to 300, for example).
tf.keras.layers.LSTM : The LSTM layer. The number of units should be specified and should match the number of elements in the word embedding.
LSTM(units) Builds an LSTM layer of n_units.
tf.keras.layers.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.
GlobalAveragePooling1D() takes the mean.
tf.keras.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.l2_normalize(x)
tf.keras.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.
Input(input_shape,dtype=None,...)
input_shape: Shape tuple (not including the batch axis)
dtype: (optional) data type of the input
tf.keras.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 Siamese(text_vectorizer, vocab_size=36224, d_feature=128):
"""Returns a Siamese model.
Args:
text_vectorizer (TextVectorization): TextVectorization instance, already adapted to your training data.
vocab_size (int, optional): Length of the vocabulary. Defaults to 56400.
d_model (int, optional): Depth of the model. Defaults to 128.
Returns:
tf.model.Model: A Siamese model.
"""
### START CODE HERE ###
branch = tf.keras.models.Sequential(name='sequential')
# Add the text_vectorizer layer. This is the text_vectorizer you instantiated and trained before
branch.add(None)
# Add the Embedding layer. Remember to call it 'embedding' using the parameter `name`
branch.add(None)
# Add the LSTM layer, recall from W2 that you want to the LSTM layer to return sequences, ot just one value.
# Remember to call it 'LSTM' using the parameter `name`
branch.add(None)
# Add the GlobalAveragePooling1D layer. Remember to call it 'mean' using the parameter `name`
branch.add(None)
# Add the normalizing layer using the Lambda function. Remember to call it 'out' using the parameter `name`
branch.add(None)
# Define both inputs. Remember to call then 'input_1' and 'input_2' using the `name` parameter.
# Be mindful of the data type and size
input1= tf.keras.layers.Input(None, dtype=None, name='input_1')
input2= tf.keras.layers.Input(None, dtype=None, name='input_2')
# Define the output of each branch of your Siamese network. Remember that both branches have the same coefficients,
# but they each receive different inputs.
branch1= None
branch2= None
# Define the Concatenate layer. You should concatenate columns, you can fix this using the `axis`parameter.
# This layer is applied over the outputs of each branch of the Siamese network
conc = tf.keras.layers.Concatenate(axis=None, name='conc_1_2')([None, None])
### END CODE HERE ###
return tf.keras.models.Model(inputs=[input1, input2], outputs=conc, name="SiameseModel")

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Excel 2024 In 7 Days

Authors: Alan Dinkins

1st Edition

B0CJ3X98XK, 979-8861224000

Students also viewed these Databases questions

Question

Evaluate the impact of unions on nurses and physicians.

Answered: 1 week ago

Question

Describe the impact of strikes on patient care.

Answered: 1 week ago

Question

Evaluate long-term care insurance.

Answered: 1 week ago