我的以下代码有问题
下一行
new_model = load_model('124446.model',custom_objects = None,compile = True)
这是代码:

import tensorflow as tf
from tensorflow.keras.models import load_model

mnist = tf.keras.datasets.mnist

(x_train,y_train), (x_test,y_test) = mnist.load_data()

x_train = tf.keras.utils.normalize(x_train,axis=1)
x_test = tf.keras.utils.normalize(x_test,axis=1)

model = tf.keras.models.Sequential()

model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(128,activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(128,activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(10,activation=tf.nn.softmax))

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
model.fit(x_train,y_train,epochs=3)


tf.keras.models.save_model(model,'124446.model')


val_loss, val_acc = model.evaluate(x_test,y_test)
print(val_loss, val_acc)


new_model = load_model('124446.model', custom_objects=None, compile=True)


prediction = new_model.predict([x_test])
print(prediction)


错误是:


  追溯(最近一次通话):文件
  “ C:/用户/TanveerIslam/PycharmProjects/DeepLearningPractice/1.py”,
  第32行,在
      new_model = load_model('124446.model',custom_objects = None,compile = True)文件
  “ C:\ Users \ TanveerIslam \ PycharmProjects \ DeepLearningPractice \ venv \ lib \ site-packages \ tensorflow \ python \ keras \ engine \ saving.py”,
  在load_model中的第262行
      sample_weight_mode = sample_weight_mode)文件“ C:\ Users \ TanveerIslam \ PycharmProjects \ DeepLearningPractice \ venv \ lib \ site-packages \ tensorflow \ python \ training \ checkpointable \ base.py”,
  _method_wrapper中的第426行
      方法(自身,* args,** kwargs)文件“ C:\ Users \ TanveerIslam \ PycharmProjects \ DeepLearningPractice \ venv \ lib \ site-packages \ tensorflow \ python \ keras \ engine \ training.py”,
  525行,正在编译
      指标,self.output_names)
  
  AttributeError:“顺序”对象没有属性“ output_names”


因此,任何人都可以给我提供解决方案。

注意:我使用pycharm作为IDE。

最佳答案

我可以通过在load_model()中设置compile = False来加载模型

10-07 12:35