C3W3: exercise 4 Classify

Hi,
I don’t know how to define this variables, I define this:
pred = model.predict(test_gen)
but:
_, n_feat = None
v1 = None
v2 = None
Not idea what to do, and what is n_feat, I try to use predict with only test_Q1 for v1 but don’t work.

Hello there,

This is similar to def TripletLoss(labels, out, margin=0.25) function in exercise 2. If you can understand that part you will be able to do this part easily :slight_smile:

Hi I was looking the train model exercise I pass all the test but give me this error
UnimplementedError: Graph execution error:
(0) UNIMPLEMENTED: Cast string to float is not supported
[[{{node SiameseModel/Cast}}]]
(1) CANCELLED: Function was cancelled before it was started

model.fit(  
            train_dataset,
            epochs = train_steps,
            validation_data = val_dataset,
         )

it seems in this part there is a problem with the train_dataset and val_dataset but not sure why it should be forward and simple, so when I run the next exercise I get this error.

I am also struggling with this exercise.

So far, I understood that we have 10240 question pairs. We are concatenating them and are creating a dataset using test_Q1 and test_Q2:

test_gen = tf.data.Dataset.from_tensor_slices(((test_Q1, test_Q2),None)).batch(batch_size=batch_size)

The output of model.predict(test_gen) is of shape (10240, 256). This means that each rows represents one question pair with a embedding dimension of 256.

How am I supposed to calculate q1 and q2 based on this?
Thanks a lot in advance!

Hi @user3141

This is a good question.

You are correct and note that 256 is because the model concatenates outputs of both branches, as per code hints for Siamese implementation:

# 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

In other words, there are 10240 rows (question pairs) with v1 ad v2 which represent the questions - each 128 dimensional, and they are concatenated into 256 vector. Now, your question is how to separate them (out of the v1v2 variable).

For hints, you can look at the TripletLoss function above which is already provided for you:

    v1 = out[:,:int(embedding_size/2)] # Extract v1 from out
    v2 = out[:,int(embedding_size/2):] # Extract v2 from out

The difference is that they used embedding_size variable there (which is a bit inaccurate) and we use the n_feat variable name (which is a better choice for the name) but in essence they are the same - the dimension of the concatenated vectors (of the averaged and normalized LSTM outputs).

So in summary, you can do what was previously done in the TripletLoss function but with the right variable names.

Cheers

2 Likes

Hi, I think is something wrong with my notebook, I get the error I explain above I don’t know if something related with a change, I understand your explanation thank you, someone can help me with the error please.

Hi @Juan_David_Vargas_Al

The mistake you’re getting might be caused due to the way you defined input1 and input2 in Siamese(..) function. Note, that both inputs in our case are of dtype tf.string.

3 Likes

Thank you so much, have a great day :slight_smile:

1 Like

Thank you arvy, this comment helped me to resolve a longstanding issue :slight_smile:

Regards
DP

1 Like