I have a problem for C3W3 Exercise 3 train Samias model part. When I train the model and tested I got errors as screen show:
It can be trained but stop in epoch 1 in total 2 epochs.
Epoch 1/2
TypeError Traceback (most recent call last)
Cell In[24], line 9
3 train_generator = train_dataset.shuffle(len(train_Q1),
4 seed=7,
5 reshuffle_each_iteration=True).batch(batch_size=batch_size)
6 val_generator = val_dataset.shuffle(len(val_Q1),
7 seed=7,
8 reshuffle_each_iteration=True).batch(batch_size=batch_size)
----> 9 model = train_model(Siamese, TripletLoss,text_vectorization,
10 train_generator,
11 val_generator,
12 train_steps=train_steps,)
Cell In[23], line 39, in train_model(Siamese, TripletLoss, text_vectorizer, train_dataset, val_dataset, d_feature, lr, train_steps)
26 model.compile(loss=TripletLossFn,
27 optimizer = tf.keras.optimizers.Adam(lr)
28 )
30 # Train the model
31 # model.fit(train_dataset = train_dataset,
32 # val_dataset = val_dataset
(β¦)
36 # images, labels = tuple(zip(*dataset))
37 # x, y = tuple(zip(*train_dataset))
β> 39 model.fit(train_dataset,
40 epochs = train_steps,
41 validation_data = val_dataset,
42 )
45 ### END CODE HERE ###
47 return model
File /usr/local/lib/python3.8/dist-packages/keras/src/utils/traceback_utils.py:70, in filter_traceback..error_handler(*args, **kwargs)
67 filtered_tb = _process_traceback_frames(e.traceback)
68 # To get the full stack trace, call:
69 # tf.debugging.disable_traceback_filtering()
β> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb
File /tmp/autograph_generated_file0ez1bfo5.py:15, in outer_factory..inner_factory..tf__train_function(iterator)
13 try:
14 do_return = True
β> 15 retval = ag_.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
16 except:
17 do_return = False
File /tmp/autograph_generated_file_ih_9j6f.py:25, in outer_factory..inner_factory..tf__TripletLossFn(v1, v2, margin)
23 raise
24 return fscope_1.ret(retval__1, do_return_1)
β> 25 sim = ag.converted_call(ag__.ld(tf).linalg.matmul, (ag__.converted_call(ag__.ld(norm), (ag__.ld(v2),), None, fscope), ag__.converted_call(ag__.ld(norm), (ag__.ld(v1),), None, fscope)), dict(transpose_b=True), fscope)
26 batch_size = ag__.ld(v1).shape[0]
27 sim_ap = ag__.converted_call(ag__.ld(tf).linalg.diag_part, (ag__.ld(sim),), None, fscope)
File /tmp/autograph_generated_file_ih_9j6f.py:20, in outer_factory..inner_factory..tf__TripletLossFn..norm(x)
18 try:
19 do_return_1 = True
β> 20 retval__1 = ag.converted_call(ag__.ld(tf).math.l2_normalize, (ag__.ld(x),), dict(axis=1), fscope_1)
21 except:
22 do_return_1 = False
TypeError: in user code:
File "/usr/local/lib/python3.8/dist-packages/keras/src/engine/training.py", line 1338, in train_function *
return step_function(self, iterator)
File "/tmp/ipykernel_14/3543266221.py", line 36, in norm *
return tf.math.l2_normalize(x, axis=1) # use tensorflow built in normalization
TypeError: Expected int32 passed to parameter 'y' of op 'Maximum', got 1e-12 of type 'float' instead. Error: Expected int32, but got 1e-12 of type 'float'.
Last time I really suspect it caused by triplet loss function:
I calcuated similarity value from v1 and v2 based on previous lab hints as:
def norm(x):
return tf.math.l2_normalize(x, axis=1) # use tensorflow built in normalization
# v1 = tf.cast(v1, tf.float32)
# v2 = tf.cast(v1, tf.float32)
sim = tf.linalg.matmul(norm(v2), norm(v1), transpose_b=True)
Any hints for this errors? I need train the model correctly to make following code work.