AttributeError:“顺序”对象没有属性“output_names”

Tan*_*lam 7 python machine-learning deep-learning keras tensorflow

我对以下行的以下代码有一些问题 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)
Run Code Online (Sandbox Code Playgroud)

错误是:

回溯(最近一次调用最后):文件“C:/Users/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”,第 262 行,在 load_model Sample_weight_mode=sample_weight_mode 中) 文件“C:\Users\TanveerIslam\ PycharmProjects\DeepLearningPractice\venv\lib\site-packages\tensorflow\python\training\checkpointable\base.py”,第 426 行,在 _method_wrapper 方法(self,*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。

Vai*_*ngh 5

正如@Shinva所说,将load_model函数的“compile”属性设置为“False”。然后加载模型后,单独编译。

from tensorflow.keras.models import save_model, load_model
save_model(model,'124446.model')
Run Code Online (Sandbox Code Playgroud)

然后再次加载模型:

saved_model = load_model('124446.model', compile=False)
saved_model.compile(optimizer='adam',
          loss='sparse_categorical_crossentropy',
          metrics=['accuracy'])
saved_model.predict([x_test])
Run Code Online (Sandbox Code Playgroud)

更新:由于一些未知的原因,我开始遇到与问题所述相同的错误。在尝试找到不同的解决方案后,似乎直接使用“keras”库而不是“tensorflow.keras”可以正常工作。

我的设置是在“Windows 10”上使用 python:'3.6.7'、tensorflow:'1.11.0' 和 keras:'2.2.4'

据我所知,可以通过三种不同的方式保存和恢复模型;前提是您直接使用 keras 来制作模型。

选项1:

import json
from keras.models import model_from_json, load_model

# Save Weights + Architecture
model.save_weights('model_weights.h5')
with open('model_architecture.json', 'w') as f:
    f.write(model.to_json())

# Load Weights + Architecture
with open('model_architecture.json', 'r') as f:
    new_model = model_from_json(f.read())
new_model.load_weights('model_weights.h5')
Run Code Online (Sandbox Code Playgroud)

选项2:

from keras.models import save_model, load_model

# Creates a HDF5 file 'my_model.h5' 
save_model(model, 'my_model.h5') # model, [path + "/"] name of model

# Deletes the existing model
del model  

# Returns a compiled model identical to the previous one
new_model = load_model('my_model.h5')
Run Code Online (Sandbox Code Playgroud)

选项3

# using model's methods
model.save("my_model.h5")

# deletes the existing model
del model

# load the saved model back
new_model = load_model('my_model.h5')
Run Code Online (Sandbox Code Playgroud)

选项 1 需要在使用前编译new_model 。

选项 2 和选项 3 在语法上几乎相似。

使用的代码来自:
1.保存和加载 Keras 模型
2. https://keras.io/getting-started/faq/#how-can-i-save-a-keras-model


小智 0

如果这是在 Windows 上运行,那么问题是 Windows 目前不支持 toco - https://github.com/tensorflow/tensorflow/issues/20975