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. Python “ValueError: incomplete format” print(“a%” % ”)

    解决python打印%号和%format输出问题 >>> print('%s' %'1') 1 >>> print('%s%' %'1') Traceback (m ...

  2. POJ 1270

    #include<iostream> #include<algorithm> #define MAXN 26 #define MAX 300 using namespace s ...

  3. POJ 1243

    #include <iostream> #include <algorithm> #include <cmath> #define MAXN 50 using na ...

  4. python 查找字符串中字母的个数

    2017.6.17 更新:好像知道错在哪里了.以第一个为例,输入应该是“AHHaaBBa”,因为直接输入AHHaaBBa时,系统不知到这是一个变量还是字符串,所以必须输入的时候申明定义.既然这样的话, ...

  5. Kali Linux信息收集工具全集

    001:0trace.tcptraceroute.traceroute 描述:进行路径枚举时,传统基于ICMP协议的探测工具经常会受到屏蔽,造成探测结果不够全面的问题.与此相对基于TCP协议的探测,则 ...

  6. matplotlib基本使用(矩形图、饼图、热力图、3D图)

    使用matplotlib画简单的图形: #-*- coding:utf-8 -*- from numpy.random import randn import matplotlib.pyplot as ...

  7. 关于符号Symbol第一篇

    Symbol类的一个实例代表一个符号.对于语法树来说,并不是每个节点都有一个符号实例.下面列举了哪些语法树节点具有符号的引用,如下表格: 其中JCNewClass.JCAssignOp.JCUnary ...

  8. js with 语句的用法

    with 语句 为语句设定默认对象. with (object)    statements 参数 object 新的默认对象. statements 一个或多个语句,object 是该语句的默认对象 ...

  9. java8 学习之路之lambda

    前言 目前我们知道java的版本已经发布到12了,之前的项目用的是JDK1.7,听说JDK1.8的改动相对来说大一些,因此抽空学学JDK1.8的一些新特性.本人也是通过阅读Java8实战这本书做一些小 ...

  10. Redis笔记(二):Redis数据类型

    Redis 数据类型 Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合). String(字符串) st ...