Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 属性错误:';顺序';对象没有属性';输出名称';_Python_Tensorflow_Machine Learning_Keras_Deep Learning - Fatal编程技术网

Python 属性错误:';顺序';对象没有属性';输出名称';

Python 属性错误:';顺序';对象没有属性';输出名称';,python,tensorflow,machine-learning,keras,deep-learning,Python,Tensorflow,Machine Learning,Keras,Deep Learning,我对下面的代码有一些问题 下一行 new\u model=load\u model('124446.model',custom\u 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.loa

我对下面的代码有一些问题 下一行 new\u model=load\u model('124446.model',custom\u 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:/Users/TanveerIslam/pycharm项目/DeepLearningPractice/1.py”, 第32行,输入 新建_模型=加载_模型('124446.model',自定义_对象=无,编译=真)文件 “C:\Users\TanveerIslam\pycharm项目\DeepLearningPractice\venv\lib\site packages\tensorflow\python\keras\engine\saving.py”, 第262行,在load_模型中 sample\u weight\u mode=sample\u weight\u mode)文件“C:\Users\TanveerIslam\PycharmProjects\DeepLearningPractice\venv\lib\site packages\tensorflow\python\training\checkpointable\base.py”, 第426行,在方法包装中 方法(self,*args,**kwargs)文件“C:\Users\TanveerIslam\PycharmProjects\DeepLearningPractice\venv\lib\site packages\tensorflow\python\keras\engine\training.py”, 第525行,在编译中 指标、自我输出(单位名称)

AttributeError:“Sequential”对象没有属性“output\u names”

所以谁能给我一个蚂蚁解决方案


注意:我使用pycharm作为IDE。

如果这是在Windows上运行的,那么问题是当前Windows上不支持toco-

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

,正如@Shinva所说,将加载模型函数的“compile”属性设置为“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”库而不是“tensorflow.keras”可以正常工作

我的设置在“Windows10”上,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')
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')
选项2:

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')
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要求在使用前编译新的_模型

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

使用的代码来自:
1.

2.

代码运行正常。我不知道是什么问题。可能尝试指定保存/加载文件的特定位置。感谢您的回复。但我也使用了文件(模型)的实际路径。但是显示相同的错误“AttributeError:‘Sequential’object没有属性‘output_names’”,我使用了pycharm IDE,虽然这段代码可能会解决这个问题,但它如何以及为什么解决这个问题会真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。