加载 Keras 模型引发异常 - 使用 TextVectorization 层部署模型
Loading a Keras model raises exception - Deploying a model with a TextVectorization layer
我在 google colab 上用 tensorflow 2.5.0 训练了一个模型,结构如下:
encoder = tf.keras.layers.experimental.preprocessing.TextVectorization(
max_tokens=VOCAB_SIZE,
output_mode='int',
output_sequence_length=MAX_SEQUENCE_LEN
)
encoder.adapt(train_dataset.map(lambda text, label: text))
model = tf.keras.Sequential([
encoder,
tf.keras.layers.Embedding(len(encoder.get_vocabulary()), 64, mask_zero=True),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64, return_sequences=True)),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32)),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(1)
])
当训练成功完成后,我像这样保存了模型
!mkdir -p saved_model
model.save('saved_model/my_model')
在我的本地机器上使用 tf.keras.models.load_model('saved_model/my_model')
下载并加载模型后(没有来自 colab 的代码的不同项目),该函数抛出一个以以下内容结尾的异常:
TypeError: ('Keyword argument not understood:', 'vocabulary_size')
如何在 python 环境中成功 运行 我的模型?
尝试将模型另存为 .h5 文件时发生以下错误:
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-23-f4fa74fc37a7> in <module>()
----> 1 model.save('my_model.h5')
4 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/saving/hdf5_format.py in _legacy_weights(layer)
896 'Save or restore weights that is not an instance of `tf.Variable` is '
897 'not supported in h5, use `save_format=\'tf\'` instead. Got a model '
--> 898 'or layer {} with weights {}'.format(layer.__class__.__name__, weights))
899 return weights
NotImplementedError: Save or restore weights that is not an instance of `tf.Variable` is not supported in h5, use `save_format='tf'` instead.
Got a model or layer TextVectorization with weights
[<tensorflow.python.keras.engine.base_layer_utils.TrackableWeightHandler object at 0x7f7415b59d10>]
最终使用 saved_model 格式和安装了 Tensorflow nightly v2.6.0.dev20210602 的生产环境成功了
我在 google colab 上用 tensorflow 2.5.0 训练了一个模型,结构如下:
encoder = tf.keras.layers.experimental.preprocessing.TextVectorization(
max_tokens=VOCAB_SIZE,
output_mode='int',
output_sequence_length=MAX_SEQUENCE_LEN
)
encoder.adapt(train_dataset.map(lambda text, label: text))
model = tf.keras.Sequential([
encoder,
tf.keras.layers.Embedding(len(encoder.get_vocabulary()), 64, mask_zero=True),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64, return_sequences=True)),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32)),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(1)
])
当训练成功完成后,我像这样保存了模型
!mkdir -p saved_model
model.save('saved_model/my_model')
在我的本地机器上使用 tf.keras.models.load_model('saved_model/my_model')
下载并加载模型后(没有来自 colab 的代码的不同项目),该函数抛出一个以以下内容结尾的异常:
TypeError: ('Keyword argument not understood:', 'vocabulary_size')
如何在 python 环境中成功 运行 我的模型?
尝试将模型另存为 .h5 文件时发生以下错误:
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-23-f4fa74fc37a7> in <module>()
----> 1 model.save('my_model.h5')
4 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/saving/hdf5_format.py in _legacy_weights(layer)
896 'Save or restore weights that is not an instance of `tf.Variable` is '
897 'not supported in h5, use `save_format=\'tf\'` instead. Got a model '
--> 898 'or layer {} with weights {}'.format(layer.__class__.__name__, weights))
899 return weights
NotImplementedError: Save or restore weights that is not an instance of `tf.Variable` is not supported in h5, use `save_format='tf'` instead.
Got a model or layer TextVectorization with weights
[<tensorflow.python.keras.engine.base_layer_utils.TrackableWeightHandler object at 0x7f7415b59d10>]
最终使用 saved_model 格式和安装了 Tensorflow nightly v2.6.0.dev20210602 的生产环境成功了