在我试图保存和加载包含LSTM层的模型时,使用load命令会出现值错误:无法找到与从SavedModel中加载的函数相匹配的函数。
class RegNet(Model):
def __init__(self,
intermediate_dim=50,
state_dim=9,
name='RegNet',
**kwargs):
super(RegNet, self).__init__()
self.d1 = Dense(intermediate_dim, activation='relu')
self.d2 = Dense(state_dim, activation='relu')
self.h = LSTM(state_dim, activation='sigmoid', return_sequences=True)
self.o = Dense(state_dim, activation='softmax')
def call(self, x):
x = self.d1(x)
x = self.d2(x)
x = self.h(x)
y = self.o(x)
return y
regNet = RegNet()
...
# Export the model to a SavedModel
regNet.save(regNet_ckpt_dir, save_format='tf')
# Recreate the exact same model
tf.keras.models.load_model(regNet_ckpt_dir)
错误报告:
> ValueError: Could not find matching function to call loaded from the SavedModel. Got:
Positional arguments (2 total):
* Tensor("x:0", shape=(None, 1, 20), dtype=float32)
* Tensor("training:0", shape=(), dtype=bool)
Keyword arguments: {}
Expected these arguments to match one of the following 4 option(s):
Option 1:
Positional arguments (2 total):
* TensorSpec(shape=(None, 1, 20), dtype=tf.float32, name='input_1')
* False
Keyword arguments: {}
Option 2:
Positional arguments (2 total):
* TensorSpec(shape=(None, 1, 20), dtype=tf.float32, name='x')
* False
Keyword arguments: {}
Option 3:
Positional arguments (2 total):
* TensorSpec(shape=(None, 1, 20), dtype=tf.float32, name='x')
* True
Keyword arguments: {}
Option 4:
Positional arguments (2 total):
* TensorSpec(shape=(None, 1, 20), dtype=tf.float32, name='input_1')
* True
Keyword arguments: {}
当我注释掉LSTM层时,load命令会成功。问题出在哪里?我们无法在TensorFlow 2.0中保存和加载带有LSTM层的模型吗?