在keras中实现查看其训练loss值
作者:感到鸭力 发布时间:2021-03-05 05:29:44
标签:keras,训练,loss
想要查看每次训练模型后的 loss 值变化需要如下操作
loss_value= [ ]
self.history = model.fit(state,target_f,epochs=1, batch_size =32)
b = abs(float(self.history.history[‘loss'][0]))
loss_value.append(b)
print(loss_value)
loss_value = np.array( loss_value)
x = np.array(range(len( loss_value)))
plt.plot(x, loss_value, c = ‘g')
pt.svefit('c地址‘, dpi= 100)
plt.show()
scipy.sparse 稀疏矩阵 函数集合
pandas 用于在各种文件中提取,并处理分析数据; 有DataFrame数据结构,类似表格。
x=np.linspace(-10, 10, 100) 生成100个在-10到10之间的数组
补充知识:对keras训练过程中loss,val_loss,以及accuracy,val_accuracy的可视化
我就废话不多说了,大家还是直接看代码吧!
hist = model.fit_generator(generator=data_generator_reg(X=x_train, Y=[y_train_a,y_train_g], batch_size=batch_size),
steps_per_epoch=train_num // batch_size,
validation_data=(x_test, [y_test_a,y_test_g]),
epochs=nb_epochs, verbose=1,
workers=8, use_multiprocessing=True,
callbacks=callbacks)
logging.debug("Saving weights...")
model.save_weights(os.path.join(db_name+"_models/"+save_name, save_name+'.h5'), overwrite=True)
pd.DataFrame(hist.history).to_hdf(os.path.join(db_name+"_models/"+save_name, 'history_'+save_name+'.h5'), "history")
在训练时,会输出如下打印:
640/640 [==============================] - 35s 55ms/step - loss: 4.0216 - mean_absolute_error: 4.6525 - val_loss: 3.2888 - val_mean_absolute_error: 3.9109
有训练loss,训练预测准确度,以及测试loss,以及测试准确度,将文件保存后,使用下面的代码可以对训练以及评估进行可视化,下面有对应的参数名称:
loss,mean_absolute_error,val_loss,val_mean_absolute_error
import pandas as pd
import matplotlib.pyplot as plt
import argparse
import os
import numpy as np
def get_args():
parser = argparse.ArgumentParser(description="This script shows training graph from history file.")
parser.add_argument("--input", "-i", type=str, required=True,
help="path to input history h5 file")
args = parser.parse_args()
return args
def main():
args = get_args()
input_path = args.input
df = pd.read_hdf(input_path, "history")
print(np.min(df['val_mean_absolute_error']))
input_dir = os.path.dirname(input_path)
plt.plot(df["loss"], '-o', label="loss (age)", linewidth=2.0)
plt.plot(df["val_loss"], '-o', label="val_loss (age)", linewidth=2.0)
plt.xlabel("Number of epochs", fontsize=20)
plt.ylabel("Loss", fontsize=20)
plt.legend()
plt.grid()
plt.savefig(os.path.join(input_dir, "loss.pdf"), bbox_inches='tight', pad_inches=0)
plt.cla()
plt.plot(df["mean_absolute_error"], '-o', label="training", linewidth=2.0)
plt.plot(df["val_mean_absolute_error"], '-o', label="validation", linewidth=2.0)
ax = plt.gca()
ax.set_ylim([2,13])
ax.set_aspect(0.6/ax.get_data_ratio())
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.xlabel("Number of epochs", fontsize=20)
plt.ylabel("Mean absolute error", fontsize=20)
plt.legend(fontsize=20)
plt.grid()
plt.savefig(os.path.join(input_dir, "performance.pdf"), bbox_inches='tight', pad_inches=0)
if __name__ == '__main__':
main()
来源:https://blog.csdn.net/qq_37640545/article/details/88979428
0
投稿
猜你喜欢
- 在上篇博客中,提到了对一个脚本进行的多次优化。当时以为已经优化得差不多了,但是当测试人员测试时,我才发现,踩到了Python的一个大坑。在上
- 一直以来,每次调用Ajax方法都需要创建一次 Microsoft.XMLHTTP 对象,今天在使用Ajax技术做一个类似聊天室的
- 本人自己写过三个图片轮播,一个是简单的原生JS实现的,没有什么动画效果的,一个是结合JQuery实现的,淡入淡出切换的。现在想做一个酷一点的
- 1 简介今天我要给大家介绍的这个Python库prettymaps非常的有趣,基于它,我们只需要简单的代码就可以对地球上给定坐标和范围的任意
- 在今天网络开发方面,JavaScript起了很关键的作用;像jQuery, MooTools, Prototype等等JavaScript框
- 在Python语言中,Uincode字符串处理一直是一个容易让人迷惑的问题。许多Python爱好者经常因为搞不清Unicode、UTF-8还
- 今天我们说一下Redis中最后一个数据类型 “有序集合类型”,回首之前学过的几个数据结构,不知道你会不会由衷感叹,开源的世界真好,写这些代码
- 在JAVA WEB应用中,如何获取servlet请求中的参数,并传递给跳转的JSP页面?例如访问http://localhost:8088/
- 该代码用的是paramiko模块,python版本是python2.7下面上源码# -*- coding: utf-8 -*-import
- 本文实例讲述了JavaScript获取一个范围内日期的方法。分享给大家供大家参考。具体分析如下:指定开始和结束时间,范围该范围内的所有日期放
- 下面这个截图,就是使用 schedule 定时执行 Notebook 的例子import scheduleimport timeimport
- Python可以使用 xml.etree.ElementTree 模块从简单的XML文档中提取数据。 为了演示,假设你想解析Planet P
- 1、服务器就是一系列硬件或软件,为一个或多个客户端(服务的用户)提供所需的“服务”。它存在唯一目的就是等待客户端的请求,并响应它们(提供服务
- 今天有一位同学给了我一个excel文件,要求读取某些行,某些列,然后我试着做了一个demo,这里分享出来,希望能帮到大家:首先安装xlrd:
- 统计在线人数是实时的吗?实现起来也比较简单,见下列代码:global.asa<SCRIPT LANGUAGE="V
- 今天在慕课网上学习了有关于python操作MySQL的相关知识,在此做些总结。python操作数据库还是相对比较简单的,由于python统一
- 1 Neural Networks 神经网络1.1 Visualizing the data 可视化数据这部分我们随机选取100个样本并可视
- __getitem__ 来看个简单的例子就明白:def __getitem__(self, key): return self.data[k
- 1.爬虫是什么所谓爬虫,就是按照一定的规则,自动的从网络中抓取信息的程序或者脚本。万维网就像一个巨大的蜘蛛网,我们的爬虫就是上面的一个蜘蛛,
- 这篇文章主要介绍了python有序查找算法 二分法实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要