Python 3.x 如何从先前保存的Keras模型中绘制精度和损失图?

Python 3.x 如何从先前保存的Keras模型中绘制精度和损失图?,python-3.x,keras,Python 3.x,Keras,有没有办法从先前保存的CNN模型中绘制准确度和损失图? 或者我们只能在训练和评估模型时绘制图表 model = Sequential() model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape)) model.add(Conv2D(64, (3, 3), activation='relu')) model.add(Ma

有没有办法从先前保存的CNN模型中绘制准确度和损失图? 或者我们只能在训练和评估模型时绘制图表

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(_NUM_CLASSES, activation='softmax'))
model.compile(optimizer='rmsprop', loss='categorical_crossentropy',metrics= 
              ["accuracy"])
model.fit(x_train, y_train,
          batch_size=_BATCH_SIZE,
          epochs=_EPOCHS,
          verbose=1,
          validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
model.save('model.h5')

这取决于您保存模型的方式

通常有两种情况,第一种是保存和加载整个模型(包括架构和权重):

第二个是仅保存权重:

def create_model():
   model = Sequential() 
   # ... creating the model exactly as it was defined in the training time

# You must create the model since loading only the weights
model = create_model()
model.load_weights('my_model_weights.h5')

有关详细信息,请阅读

,这取决于您保存模型的方式

通常有两种情况,第一种是保存和加载整个模型(包括架构和权重):

第二个是仅保存权重:

def create_model():
   model = Sequential() 
   # ... creating the model exactly as it was defined in the training time

# You must create the model since loading only the weights
model = create_model()
model.load_weights('my_model_weights.h5')

有关更多详细信息,请阅读

在Keras中保存模型的可用选项中没有一个包含培训历史记录,这正是您在此处要求的。为了保持此历史记录可用,您必须对培训代码进行一些琐碎的修改,以便单独保存它;以下是一个基于Keras和仅3个培训阶段的可复制示例:

hist = model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=3,
          verbose=1,
          validation_data=(x_test, y_test))
hist
是一个Keras回调,它包括一个
history
字典,其中包含您要查找的度量:

hist.history
# result:
{'acc': [0.9234666666348775, 0.9744000000317892, 0.9805999999682109],
 'loss': [0.249011807457606, 0.08651042315363884, 0.06568188704450925],
 'val_acc': [0.9799, 0.9843, 0.9876],
 'val_loss': [0.06219216037504375, 0.04431889447008725, 0.03649089169385843]}
i、 e.每个培训阶段(此处为3)的培训和验证指标(此处为损失和准确性)

现在,使用保存此词典并根据需要进行恢复非常简单:

import pickle

# save:
f = open('history.pckl', 'wb')
pickle.dump(hist.history, f)
f.close()

# retrieve:    
f = open('history.pckl', 'rb')
history = pickle.load(f)
f.close()
此处的简单检查确认原始变量和检索到的变量确实相同:

hist.history == history
# True

在Keras中保存模型的可用选项中没有一个包含培训历史记录,这正是您在这里要求的。为了保持此历史记录可用,您必须对培训代码进行一些琐碎的修改,以便单独保存它;以下是一个基于Keras和仅3个培训阶段的可复制示例:

hist = model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=3,
          verbose=1,
          validation_data=(x_test, y_test))
hist
是一个Keras回调,它包括一个
history
字典,其中包含您要查找的度量:

hist.history
# result:
{'acc': [0.9234666666348775, 0.9744000000317892, 0.9805999999682109],
 'loss': [0.249011807457606, 0.08651042315363884, 0.06568188704450925],
 'val_acc': [0.9799, 0.9843, 0.9876],
 'val_loss': [0.06219216037504375, 0.04431889447008725, 0.03649089169385843]}
i、 e.每个培训阶段(此处为3)的培训和验证指标(此处为损失和准确性)

现在,使用保存此词典并根据需要进行恢复非常简单:

import pickle

# save:
f = open('history.pckl', 'wb')
pickle.dump(hist.history, f)
f.close()

# retrieve:    
f = open('history.pckl', 'rb')
history = pickle.load(f)
f.close()
此处的简单检查确认原始变量和检索到的变量确实相同:

hist.history == history
# True

我使用model.save('model.h5')来保存整个模型。现在,如何从保存的模型生成精度和损耗曲线?加载模型后,使用
model.evaluate()
。您将得到精度和损失值,而不是曲线。您应该考虑将最后的注释添加到答案中,因为这实际上解释了如何获得精度/丢失值。我已经使用了Simult.Save'(“Mult.H5”)来保存整个模型。现在,如何从保存的模型生成精度和损耗曲线?加载模型后,使用
model.evaluate()
。你会得到精度和损失值,而不是曲线。你应该考虑把你最后的评论添加到你的答案中,因为这实际上解释了如何获得精度/损失值。