嘗試使用已保存的 Keras 模型時遇到此錯誤。
此版本的代碼有效。它是用于紙牌游戲的對手手牌預測模型,每個批次大小為 64,每個時間步長為 25 個,其中每個步長是一個dim 211 的張量,表示前一個時間步中的游戲資訊。它是從這個官方教程修改而來的。
# Main model
class HandPredictionModel(tf.keras.Model):
def __init__(self):
super().__init__(self)
self.lstm1 = tf.keras.layers.LSTM(512, return_sequences=True)
self.dropout1 = tf.keras.layers.Dropout(0.2)
self.lstm2 = tf.keras.layers.LSTM(512, return_sequences=True, return_state=True)
self.dense = tf.keras.layers.Dense(156, activation="sigmoid")
@tf.function
def call(self, x, states=None, return_state=False, training=False):
if states is None:
states = self.lstm1.get_initial_state(x)
x = self.lstm1(x, states)
x = self.dropout1(x)
x, final_memory_state, final_carry_state = self.lstm2(x)
x = self.dense(x)
if return_state:
return x, final_memory_state, final_carry_state
return x
handPredictionmodel = HandPredictionModel()
handPredictionModel.compile(...) # loss function, optimizer
dataset = (dataset.shuffle(1000, reshuffle_each_iteration=True).batch(64, drop_remainder=True))
# <BatchDataset shapes: ((64, 25, 211), (64, 25, 156)), types: (tf.float32, tf.float32)>
history = handPredictionModel.fit(dataset, epochs=100)
# One-step model
class OneStep(tf.keras.Model):
def __init__(self, model):
super().__init__()
self.model = model
@tf.function
def predict(self, inputs, states=None):
inputs = tf.expand_dims(tf.expand_dims(inputs, axis=0), axis=0) # add 'fake' dims for batch and timestep
predicted_logits, memory_state, carry_state= self.model(x=inputs, states=states, return_state=True, training=False)
predicted_logits = predicted_logits[:, -1, :]
return predicted_logits, [memory_state, carry_state]
# Testing
oneStepModel = OneStep(handPredictionModel)
states = None
for i in range(10):
t = tf.zeros([211])
pred, states = oneStepModel.predict(t, states)
print(pred)
正如預期的那樣,這提供了 10 個 shape(1, 156) 張量的輸出,但是當我保存時HandPredictionModel,將其重新加載,并使用它來初始化OneStepModel我收到有關輸入維度的錯誤。
tf.saved_model.save(model, 'handPredictionModel')
loadedModel = tf.saved_model.load('handPredictionModel')
oneStepModel = OneStep(loadedModel)
states = None
for i in range(10):
t = tf.zeros([211])
pred, states = oneStepModel.predict(t, states)
print(pred)
ValueError: Could not find matching function to call loaded from the SavedModel. Got:
Positional arguments (4 total):
* Tensor("x:0", shape=(1, 1, 211), dtype=float32)
* None
* True
* False
Keyword arguments: {}
Expected these arguments to match one of the following 4 option(s):
Option 1:
Positional arguments (4 total):
* TensorSpec(shape=(None, 25, 211), dtype=tf.float32, name='input_1')
* None
* False
* False
Keyword arguments: {}
Option 2:
Positional arguments (4 total):
* TensorSpec(shape=(None, 25, 211), dtype=tf.float32, name='x')
* None
* False
* False
Keyword arguments: {}
Option 3:
Positional arguments (4 total):
* TensorSpec(shape=(None, 25, 211), dtype=tf.float32, name='x')
* None
* False
* True
Keyword arguments: {}
Option 4:
Positional arguments (4 total):
* TensorSpec(shape=(None, 25, 211), dtype=tf.float32, name='input_1')
* None
* False
* True
Keyword arguments: {}
什么可能導致這種情況?這里唯一的區別是保存和加載模型的額外步驟。這是一個問題,因為對于我的資料集的大小,我必須以HandPredictionModel增量方式訓練,但是任何時候我必須保存和加載它,這意味著我OneStepModel將無法作業。
uj5u.com熱心網友回復:
問題在于,當您保存模型時,用于訓練 LSTM 模型的批量大小 64 也必須用于預測。可以在這篇文章中找到這樣做的原因以及有關此主題的更多資訊。在您的情況下,我通常只保存模型的權重,然后創建一個新模型并在進行預測時加載權重:
class OneStep(tf.keras.Model):
def __init__(self, model):
super().__init__()
self.model = model
@tf.function
def predict(self, inputs, states=None):
inputs = tf.expand_dims(tf.expand_dims(inputs, axis=0), axis=0) # add 'fake' dims for batch and timestep
predicted_logits, memory_state, carry_state= self.model.call(x=inputs, states=states, return_state=True, training=False)
predicted_logits = predicted_logits[:, -1, :]
return predicted_logits, [memory_state, carry_state]
handPredictionModel.save_weights('model_weights')
loadedModel = HandPredictionModel()
loadedModel.load_weights('model_weights')
oneStepModel = OneStep(loadedModel)
states = None
for i in range(10):
t = tf.zeros([211])
pred, states = oneStepModel.predict(t, states)
print(pred)
如果您不打算再次訓練模型,則此選項特別有用。有關此問題的更多替代解決方案,請參閱給定鏈接。如果你需要在訓練迭代模型,正如你所說的話,我會建議您繼續使用save和load,當你完成訓練,你可以簡單地節省重量,并加載它們的預測。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/334342.html
上一篇:我增加資料后CNN停止運行
