在使用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. 系统API是原子操作吗?

    系统API里面也会有简单的指令,类似于a++这种,我们认为的简单指令对应到汇编可能很多条.执行在其中某一条汇编的时候可能就切换进程了.切换进程可能发生在用户态(虚拟内存的用户空间),也可能发生在内核态 ...

  2. OF框架使用指导系列索引

    一.项目搭建指导 [OF框架]搭建标准工作环境 [OF框架]使用OF框架创建应用项目 [OF框架]在Visual Studio中启用Docker支持,编译生成,并在容器运行项目 [OF框架]在Visu ...

  3. LRU(最近最少使用)(python实现)

    """ python3 only LRU cache """ from collections import OrderedDict fro ...

  4. python数据类型:dict(字典)

    一.字典的简单介绍 字典(dict)是python中唯一的一个映射类型.他是以{}括起来的键值对组成. 语法: {key1:value1,key2:value2......} 注意:key必须是不可变 ...

  5. cookie和session基础知识学习

    一.session的简单使用 session是服务器端技术,服务器在运行时可以为每一个用户的浏览器创建一个独享的session对象.session的使用步骤: 获取session对象使用session ...

  6. Android-jacoco代码覆盖率:单元测试覆盖率+功能测试覆盖率

    参考:https://docs.gradle.org/current/dsl/org.gradle.testing.jacoco.tasks.JacocoCoverageVerification.ht ...

  7. 01-Spring概述

    1.spring是什么 Spring是分层的Java SE/EE应用 full-stack轻量级开源框架,以IoC(Inverse Of Control:反转控制)和AOP(Aspect Orient ...

  8. machine learning(12) -- classification: One-vs-all classfication

    Multiclass classification例子: 邮箱的邮件的分类: 工作邮件,私人邮件,朋友的邮件,兴趣爱好的邮件 医学诊断: 没有生病,患有流感,患有普通感冒 天气: 晴天,兩,多云等 O ...

  9. js+css 动效+1的效果

    点击数值 +1 的动效 vue data:{ timer: null,plus:''// 次数 } method:{ animate(plus) { var _this = this; clearIn ...

  10. Spring MVC 学习笔记(一)

    • 1.SpringMVC概述 • 2.SpringMVC的HelloWorld • 3.使用@RequestMapping映射请求 • 4.映射请求参数&请求头 • 5.处理模型数据 • 6 ...