Keras & Theano get output of an intermediate layer

1.使用函数模型API,新建一个model,将输入和输出定义为原来的model的输入和想要的那一层的输出,然后重新进行predict.

import seaborn as sbn
import pylab as plt
import theano
from keras.models import Sequential
from keras.layers import Dense,Activation from keras.models import Model model = Sequential()
model.add(Dense(32, activation='relu', input_dim=100))
model.add(Dense(16, activation='relu',name="Dense_1"))
model.add(Dense(1, activation='sigmoid',name="Dense_2"))
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy']) # Generate dummy data
import numpy as np
#假设训练和测试使用同一组数据
data = np.random.random((1000, 100))
labels = np.random.randint(2, size=(1000, 1)) # Train the model, iterating on the data in batches of 32 samples
model.fit(data, labels, epochs=10, batch_size=32)
#已有的model在load权重过后
#取某一层的输出为输出新建为model,采用函数模型
dense1_layer_model = Model(inputs=model.input,
outputs=model.get_layer('Dense_1').output)
#以这个model的预测值作为输出
dense1_output = dense1_layer_model.predict(data) print(dense1_output.shape)
print(dense1_output[0])
2.因为我的后端是使用的theano,所以还可以考虑使用theano的函数:

#这是一个theano的函数
dense1 = theano.function([model.layers[0].input],model.layers[1].output,allow_input_downcast=True)
dense1_output = dense1(data) #visualize these images's FC-layer feature
print(dense1_output[0])

效果应该是一样的。

 

来源:https://blog.csdn.net/hahajinbu/article/details/77982721

 
 
 
 
 
 
 

Keras & Theano 输出中间层结果的更多相关文章

  1. Hi3559AV100 NNIE开发(5)mobilefacenet.wk仿真成功量化及与CNN_convert_bin_and_print_featuremap.py输出中间层数据对比过程

    前面随笔给出了NNIE开发的基本知识,下面几篇随笔将着重于Mobilefacenet NNIE开发,实现mobilefacenet.wk的chip版本,并在Hi3559AV100上实现mobilefa ...

  2. Hi3559AV100 NNIE开发(7) Ruyistudio 输出mobileface_func.wk与板载运行mobileface_chip.wk输出中间层数据对比

    前面随笔讲了关于NNIE的整个开发流程,并给出了Hi3559AV100 NNIE开发(5)mobilefacenet.wk仿真成功量化及与CNN_convert_bin_and_print_featu ...

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

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

  4. keras+theano+tensorflow+darknet

    keras 安装: 最好在anaconda的虚拟环境下搭建: conda create -n 环境名 python=3.6 进入环境: source activate 环境名 安装keras: pip ...

  5. keras 多输出问题

    转自:https://github.com/Xls1994/DeepLearningCode/blob/master/Keras/HedgeScope/multiOutputLSTM.py

  6. 开源项目(4-2)手势识别-Keras/Theano/OpenCV实现的CNN手势识别

    https://github.com/asingh33/CNNGestureRecognizer 我提供了两种捕获模式: 二进制模式:在这里我首先将图像转换为灰度,然后应用高斯模糊效果和自适应阈值滤波 ...

  7. 关于深度学习框架 TensorFlow、Theano 和 Keras

    [TensorFlow] ——( https://morvanzhou.github.io/tutorials/machine-learning/tensorflow/) 1.TensorFlow是啥 ...

  8. Keras + Ubuntu环境搭建

    安装Theano (环境参数:Ubuntu 16.04.2  Python 2.7) 安装 numpy 和 scipy 1.sudo apt-get install python-numpy pyth ...

  9. Windows7配置GPU和Theano编程环境

    可参考Windows安装Theano官方教程: http://deeplearning.net/software/theano/install_windows.html#install-windows ...

随机推荐

  1. jQuery过滤选择器:first和:first-child的区别,CSS伪类:first-child

    最近项目中遇到需求:只在第一列不能删除,不显示小叉号:点击可添加一列,后面的列右上角显示小叉号,可以点击删除. 我是使用以下方法解决这个小需求 :CSS伪类选择器:first-child设置所有小叉号 ...

  2. 安装Hive-0.10.0-CDH4.5.0所遇异常

    Note: 虚拟机访问Win7中mysql(root用户+密码) hive出现异常1: FAILED: Error in metadata: java.lang.RuntimeException: U ...

  3. 机器大数据也离不开Hadoop

    转自:http://gtstorageworld.blog.51cto.com/908359/1286758 根据数据来源划分,大数据主要包括三类:商业运作产生的数据.人类行为产生的数据和机器数据.目 ...

  4. Spring Security构建Rest服务-0702-个性化用户认证流程2

    登录成功后的处理AuthenticationSuccessHandler: 认证成功后,默认情况下spring security会继续访问之前访问的url,如果想自定义处理逻辑,用默认的就不行了.此时 ...

  5. Android Kotlin开发之使用Butterknife注意要点

    使用kotlin-kapt插件 依赖由java的annotationProcessor改为kapt 在使用控件绑定使用时,网上搜使用方法,不知道被哪个家伙带坑里了. //错误用法 @BindView( ...

  6. android学习-LocationManager(一)-

    Location Provider是不同位置信息来源的抽象 Location封装了从位置提供者提供给应用的位置数据(经纬度) Criteria提供了查询获取包含特定特征的位置提供者(accuracy精 ...

  7. mongodb二进制安装与yum安装

    一.什么是mongodb MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种.MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当 ...

  8. 业务ID 生成规则

    在实际业务中,是否碰到过这种场景: 我们需要一个单号,要在业务系统里面保证唯一,保证增长? 在运营过程,需要知道业务单发生的时间,最好不用经过系统查找就知道发生的时间? 在排障过程中,不用再次查找就知 ...

  9. Linux的用户(组),权限,文件精妙的三角关系,和强大的帮助系统

    在linux中一切都是文件(文件夹和硬件外设是特殊的文件),如果有可能尽量使用文本文件.文本文件是人和机器能理解的文件,也成为人和机器进行 交流的最好途径.由于所有的配置文件都是文本,所以你只需要一个 ...

  10. 浅谈JS中的!=、== 、!==、===的用法和区别

    var num = 1;     var str = '1';     var test = 1;     test == num  //true 相同类型 相同值     test === num ...