提问者:小点点

序列对象没有属性output_names


我对下面一行的代码有一些问题new_model=load_model('124446. model',custom_objects=无,编译=真)这是代码:

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)

错误是:

Traceback(最近一次调用最后一次): File"C:/User/TanveerISIS/PycharmProjects/DeepLearning实践/1.py",第32行,在new_model=load_model('124446.model',custom_objects=无,编译=True)File"C:\User\TanveerIsam\PycharmProjects\DeepLearning实践\venv\lib\site-Packes\Tensorflow\Python\keras\engine\saving.py",第262行,在load_model=sample_weight_mode)File"C:\用户\TanveerIsam\PycharmProjects\DeepLearning实践\venv\lib\site-Packds\Tensorflow\python\训练\checkpoint able\base.py",第426行,在_method_wrapper方法(self,*args,**kwargs)File"C:\用户\TanveerIsam\PycharmProjects\DeepLearning实践\venv\lib\sitorflow\python\keras\engine\training.py",第

序列对象没有属性output_names

所以任何人都可以给我蚂蚁的解决方案。

注意:我使用pyCharm作为IDE。


共3个答案

匿名用户

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

from tensorflow.keras.models import save_model, load_model
save_model(model,'124446.model')

然后再次加载模型:

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

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

我的设置在“Windows 10”上,使用python:“3.6.7”、ensorflow:“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')

选项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')

备选方案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')

方案1要求在使用前编译new_model。

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

使用的代码来自:
1.保存

匿名用户

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

匿名用户

import tensorflow as tf    
tf.keras.models.save_model(
    model,
    "epic_num_reader.model",
    overwrite=True,
    include_optimizer=True
) 

new_model = tf.keras.models.load_model('epic_num_reader.model', custom_objects=None, compile=False)

predictions = new_model.predict(x_test)
print(predictions)

import numpy as np

print(np.argmax(predictions[0]))
plt.imshow(x_test[0],cmap=plt.cm.binary)
plt.show()