1.ValueError: Unknown XXX: YYY
解決辦法是在tf.keras.models.load_model()括號裏面加上
custom_objects = {“YYY”:XXX對應的函數或者對象}
例如教程裏是把網絡模型定義成一個class,class名字爲MyModel(它是繼承了Sequential),這個解決辦法是在模型導入的時候加上 custom_objects = {“MyModel”:Sequential},例如以下代碼:
new_model=tf.keras.models.load_model("chapter10/models/model.h5",
custom_objects = {"MyModel":Sequential})
有時候還會再報其他的錯,長這樣的這種類型的錯誤缺什麼加什麼一般就管用。
例如報錯 ValueError: Unknown activation function:leaky_relu 就加上
new_model=tf.keras.models.load_model("chapter10/models/model.h5",
custom_objects = {"MyModel":Sequential,
"leaky_relu":tf.nn.leaky_relu})
2.ValueError: Error when checking input:
如果訓練模型的時候,第一層沒有表明輸入張量的維度,導入模型時可能會報錯:
ValueError: You are trying to load a weight file containing 6 layers into a model with 0 layers.
如果把上述維度添加上再訓練,寫成 input_shape=(None,784),導入模型時可能會報錯:
ValueError: Error when checking input: expected dense_input to have 3 dimensions, but got array with shape (5139, 784)
一種避免出錯的做法是訓練模型時,用input_dim=784來表明輸入數據的維度,例如:
Dense(100,kernel_initializer=he,activation=elu,input_dim=784)