Tensorflow — LSTM, ТексТвекторизация (пользовательская стандартизация) и контрольная точка модели

#python #tensorflow #keras #tensorflow2.0

Вопрос:

Я довольно новичок в Tensorflow. Я следил за встраиванием слов, базовой классификацией текста и статьями RNN на веб-сайте TF.

У меня есть dataset_dir = 'aclImdb' набор данных IMDB, как в этих учебниках, и все так же, как описано там. Я добавил ModelCheckpoint с save_best_only=True , и я получаю две ошибки — первую при обучении, между эпохами, я думаю, когда сохраняется лучшая модель, и вторую, когда загружается лучшая сохраненная модель.

Предупреждение 1: WARNING:absl:Found untraced functions such as lstm_cell_1_layer_call_fn, lstm_cell_1_layer_call_and_return_conditional_losses, lstm_cell_2_layer_call_fn,lstm_cell_2_layer_call_and_return_conditional_losses, lstm_cell_4_layer_call_fn while saving (showing 5 of 20). These functions will not be directly callable after loading.

WARNING:absl:lt;keras.layers.recurrent.LSTMCell object at 0x000001E4DA8BC0A0gt; has the same name 'LSTMCell' as a built-in Keras object. Consider renaming lt;class 'keras.layers.recurrent.LSTMCell'gt; to avoid naming conflicts when loading with 'tf.keras.models.load_model'. If renaming is not possible, pass the object in the 'custom_objects' parameter of the load function.

Ошибка 2: RuntimeError: Unable to restore a layer of class TextVectorization. Layers of class TextVectorization require that the class be provided to the model loading code, either by registering the class using '@keras.utils.register_keras_serializable' on the class def and including that file in your program, or by passing the class in a 'keras.utils.CustomObjectScope' that wraps this load call.

Мои переменные:

 batch_size = 64 seed = 42 max_features = 10000 # vocabulary size sequence_length = 250 # sentence length of reviews or tweets, etc. output_dim = 16 # encoded size of a vector  

Вот мой обратный звонок:

 def create_model_checkpoint(model_name, save_path='model_experiments'):  return tf.keras.callbacks.ModelCheckpoint(filepath=os.path.join(save_path, model_name),  verbose=0, # output limited amount of info  save_best_only=True)  

Вот ТексТвекторизация (такая же, как в статьях):

 def custom_standardization(input_data):  lowercase = tf.strings.lower(input_data)  stripped_html = tf.strings.regex_replace(lowercase, 'lt;br/gt;', ' ')  return tf.strings.regex_replace(stripped_html,  '[%s]' % re.escape(string.punctuation), '')  vectorize_layer = tf.keras.layers.TextVectorization(  standardize=custom_standardization,  max_tokens=max_features,  output_mode='int',  output_sequence_length=sequence_length )  

Here’s the Model (I have added GlobalAveragePooling1D and callback compared to the article):

 model_2 = tf.keras.models.Sequential([  vectorize_layer,  tf.keras.layers.Embedding(len(vectorize_layer.get_vocabulary()), 64, mask_zero=False),  tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64, return_sequences=True)),  tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32, return_sequences=True)),  tf.keras.layers.GlobalAveragePooling1D(),  tf.keras.layers.Dense(64, activation='relu'),  tf.keras.layers.Dense(1) ], name='model_2_Bidirectional')  model_2.compile(loss=tf.keras.losses.BinaryCrossentropy(),  optimizer=tf.keras.optimizers.Adam(),  metrics=['accuracy'])  history_model_2 = model_2.fit(train_ds, epochs=3,  validation_data=valid_ds,  callbacks=[create_model_checkpoint(model_name=model_2.name)])  model_2_loaded = tf.keras.models.load_model(os.path.join('model_experiments', model_2.name)) model_2_loaded.evaluate(valid_ds)  

Questions: Regarding Warning — I have tried defining name=’lstm_custom_64′ and ‘lstm_custom_32’ for the layers but these warnings still kept on coming up. I couldn’t find any clear solution to what this warning means and how to fix it.

Что касается ошибки — у меня есть больше подсказок на этот счет, я думаю, мне нужно определить @keras.utils.register_keras_serializable и написать пользовательский класс слоя, наследуемый от TextVectorization него и включаемый custom_standardization в него. Должен ли я? Я был бы рад, если бы вы могли поделиться решением для этого, так как у меня нет опыта работы с TF.

P.S. А как насчет адаптации? насколько я понимаю, это создает вокабуляр. Это сохраняется, а затем загружается при выполнении сохранения/загрузки?

 text_ds = train_ds.map(lambda x, y: x) vectorize_layer.adapt(text_ds)