source code
in the decode_sequence function
if i try to call that function in a loop it would leak my memory
transformer([tokenized_input_sentence, tokenized_target_sentence])
gc.collect() not work
tf.keras.backend.clear_session() not work
transformer.predict() not work with gc.collect and tf.keras.backend.clear_session()
transformer(training=False) not work
def decode_sequence(input_sentence): tokenized_input_sentence = input_vectorization([input_sentence]) decoded_sentence = START_TOKEN for i in tf.range(max_decoded_sentence_length): tokenized_target_sentence = output_vectorization([decoded_sentence])#[:, :-1] predictions = transformer([tokenized_input_sentence, tokenized_target_sentence]) sampled_token_index = np.argmax(predictions[0, i, :]) sampled_token = output_index_lookup[sampled_token_index] decoded_sentence += sampled_token if sampled_token == END_TOKEN: break gc.collect() return decoded_sentence from tqdm import tqdm def overall_accuracy(pairs): corrects = 0 inputs = pairs[2739:] iter = tqdm(inputs) for i, pair in enumerate(iter): input_text = pair[0] target = pair[1] predicted = decode_sequence(input_text) #guess = '✓' if predicted == target else '✗' #print('Sample Number : ', i, 'Predicted : ', predicted, 'Real : ', target, guess) if predicted == target: corrects += 1 iter.set_postfix(corrects=corrects, accuracy=corrects / (i + 1)) return corrects / len(inputs) print("Overall Acurracy : ", overall_accuracy(test_pairs))```