在使用keras搭建神经网络时,有时需要查看一下预测值和真是值的具体数值,然后可以进行一些其他的操作。这几天查阅了很多资料。好像没办法直接access到训练时的数据。所以我们可以通过回调函数,传入新的数据,然后查看预测值和真是值。参考这篇解决:

https://stackoverflow.com/questions/47079111/create-keras-callback-to-save-model-predictions-and-targets-for-each-batch-durin

我的解决方法是这样的:

from keras.callbacks import Callback
import tensorflow as tf
import numpy as np
class my_callback(Callback):
def __init__(self,dataGen,showTestDetail=True):
self.dataGen=dataGen
self.showTestDetail=showTestDetail
self.predhis = []
self.targets = []
def mape(self,y,predict):
diff = np.abs(np.array(y) - np.array(predict))
return np.mean(diff / y)
def on_epoch_end(self, epoch, logs=None):
x_test,y_test=next(self.dataGen)
prediction = self.model.predict(x_test)
self.predhis.append(prediction)
#print("Prediction shape: {}".format(prediction.shape))
#print("Targets shape: {}".format(y_test.shape))
if self.showTestDetail:
for index,item in enumerate(prediction):
print(item,"=====",y_test[index],"====",y_test[index]-item)
testLoss=self.mape(y_test,prediction)
print("test loss is :{}".format(testLoss))

画一下知识点,我们在继承的callback中实现 on_epoch_end方法:

x_test,y_test=next(self.dataGen)

这个数据生成方法是这样的

import numpy as np
def shuffleDatas(x,y): shuffleIndex=np.arange(len(x))
np.random.shuffle(shuffleIndex)
x=x[shuffleIndex]
y=y[shuffleIndex]
return x,y
def dataGen(x,y,batchsize=8,shuffle=True):
assert len(x) == len(y)
while True:
if shuffle:
x,y=shuffleDatas(x,y)
index=0
while index+batchsize<len(x):
yield (x[index:index+batchsize],y[index:index+batchsize])
index=index+batchsize

使用yield可以减少内存的使用,而且显得很高级。

keras输出预测值和真实值的更多相关文章

  1. django序列化时使用外键的真实值

    展示: 普通情况下序列化得到的外键的内容仅仅是id: ... { fields: { uat_date: "2015-07-25", statu: "CG", ...

  2. ComboBox的真实值和显示值

    一.类型 /// <summary> /// 下拉框值类型 /// </summary> public class TextAndValue { /// <summary ...

  3. keras输出中间层结果,某一层的权重、偏置

    转载:https://blog.csdn.net/hahajinbu/article/details/77982721 from keras.models import Sequential,Mode ...

  4. Keras输出每一层网络大小

    示例代码: model = Model(inputs=self.inpt, outputs=self.net) model.compile(loss='categorical_crossentropy ...

  5. kettle查询出来的真实值被识别为null

    问题描述: 通过关联表查询出来的applyId(申请编号),在数据流里也是能看到的,但是在写入到数据表中的时候,由于设置了这个字段不能为空,所以一直报错. 问题实质: 数据流内存在的数据却不能保存,原 ...

  6. TF之AE:AE实现TF自带数据集数字真实值对比AE先encoder后decoder预测数字的精确对比—Jason niu

    import tensorflow as tf import numpy as np import matplotlib.pyplot as plt #Import MNIST data from t ...

  7. Keras 入门

    “sample”“batch”“epoch” Sample:样本,比如:一张图像是一个样本,一段音频也是一个样本. Batch:批,含有N个样本的集合.每一个batch的样本都是独立的并行处理.在训练 ...

  8. keras 自定义 custom 函数

    转自: https://kexue.fm/archives/4493/,感谢分享! Keras是一个搭积木式的深度学习框架,用它可以很方便且直观地搭建一些常见的深度学习模型.在tensorflow出来 ...

  9. 自我学习与理解:keras框架下的深度学习(三)回归问题

    本文主要是使用keras对其有的波士顿房价数据集做一个回归预测,其代码架构与之前一样(都只是使用多层感知机):数据的预处理.搭建网络框架.编译.循环训练以及测试训练的网络模型.其中除了数据预处理与之前 ...

随机推荐

  1. TLS1.3对CIP的影响(对密码套件的解释)

    1.术语定义的即使(算法)Definition of terms (optional) Cipher Suite  :通信数据保护规范,对TLS指定对端身份验证,关键技术机制,后续数据加密和数据验证机 ...

  2. GoogLeNet网络的Pytorch实现

    1.文章原文地址 Going deeper with convolutions 2.文章摘要 我们提出了一种代号为Inception的深度卷积神经网络,它在ILSVRC2014的分类和检测任务上都取得 ...

  3. html知识补充

    1.点击超链接跳转到新窗口 <a href="http://www.baidu.com" target="_blank">百度一下</a> ...

  4. 1128 聚合查询 orm字段及属性

    目录 一 聚合查询 1. 级联 级联删除 级联更新 2. 聚合函数 使用 aggregate 使用场景 3. 分组查询 语法 使用 annotate 代码 4. F与Q查询 F查询 Q查询 二 ORM ...

  5. 普通的java Ftp客户端的文件上传

    关于ftp上传文件其实并不难,但有时候面对现实的环境还是很蛋疼的,今天我就分享一下,普通的上传文件文件至FTP的方式,它满足大部分FTP,但也有特别的,下篇博客会提及到. 下面我用一个FtpUtil, ...

  6. MongoDB 查看chunk块大小

    使用mongo shell连到mongos执行命令:AllChunkInfo("dbname.cellname",true) 点击(此处)折叠或打开 AllChunkInfo = ...

  7. 002_Python3 基础语法

    1.注释 实例1: #!/usr/bin/python3 # 第一个注释 print("Hello, Python!")  # 第二个注释   ****************** ...

  8. Bootstap学习的实用网站

    基本CSS样式 http://v2.bootcss.com/base-css.html 93 Twitter Bootstrap HTML Templates https://shapebootstr ...

  9. [NOI2010]超级钢琴 倍增

    [NOI2010]超级钢琴 倍增 题面 暴力:枚举区间丢入堆\(O(n^2logn)\) 正解:考虑每次枚举和弦起点\(s\),那么以\(s\)为起点的和弦为\(sum[t]-sum[s](s+L-1 ...

  10. Python的模块,模块的使用、安装,别名,模块作用域

    模块和包 所谓的模块就是将不同功能的函数分别放到不同的文件中,这样不仅有利于函数的维护,也方便了函数的调用.在Python中,一个.py文件就是一个模块(Module). 在模块的上层有一个叫做包(P ...