Keras 获取中间某一层输出
1.使用函数模型API,新建一个model,将输入和输出定义为原来的model的输入和想要的那一层的输出,然后重新进行predict.
#coding=utf-8
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]
效果应该是一样的。
---------------------
作者:哈哈进步
来源:CSDN
原文:https://blog.csdn.net/hahajinbu/article/details/77982721
版权声明:本文为博主原创文章,转载请附上博文链接!
Keras 获取中间某一层输出的更多相关文章
- Freemarker-2.3.22 Demo - No01_获取模板并直接输出
package No01_获取模板并直接输出; import java.io.File; import java.io.FileOutputStream; import java.io.OutputS ...
- js Date()获取时间,格式化输出,时间比较大小
1.获取时间并且格式化输出 new Date().toLocaleString('cn',{hour12:false}) //2018/12/6 17:57:15 new Date().toLocal ...
- HttpRequest获取文件流,HttpResponse输出文件流
HttpResponse输出文件: Response.Clear(); Response.ContentType = "application/octet-stream"; //通 ...
- keras计算指定层的输出
import keras model = keras.models.Sequential([ keras.layers.Dense(4, activation='relu', input_dim=1, ...
- 【C#】【MySQL】C#获取存储过程的Output输出参数值
创建存储过程 Create PROCEDURE MYSQL @a int, @b int, @c int output AS Set @c = @a + @b GO 通过以下方法可以获得储存过程的输出 ...
- 异步获取CMD命令行输出内容
当控制台命令使用process.Start(); 后可以直接显示输出内容,当然它是异步显示的不用等程序结束.代码如下: using System;using System.Collections.Ge ...
- C#获取堆栈信息,输出文件名、行号、函数名、列号等
命名空间:System.Diagnostics 得到相关信息: StackTrace st = new StackTrace(new StackFrame(true));StackFrame sf = ...
- 用 get 同步/异步 方式获取网络数据并输出
//同步请求 //创建NSString用来存储请求的网址 NSString* str=@"http://v.juhe.cn/weather/index?format=2&cityna ...
- python 获取中文文件名的输出
#coding:utf8 if __name__ == '__main__': srcfile = u"D:/测试路径/测试文件.txt" f = open(srcfile.enc ...
随机推荐
- centOS7下Spark安装配置
环境说明: 操作系统: centos7 64位 3台 centos7-1 192.168.190.130 master centos7-2 192.168.190.129 slave1 centos7 ...
- Windows下建立ArcGIS Server集群
原创文章,转载须标明出处自: http://www.cnblogs.com/gisspace/p/8269525.html -------------------------------------- ...
- cvc-elt.1: Cannot find the declaration of element 'beans'Failed to read schema document 'http://www.springframework.org/schema/beans/spring- beans-3.0.xsd'
Multiple annotations found at this line: - cvc-elt.1: Cannot find the declaration of element 'beans' ...
- Linux(三)——Unix&Linux 的基础命令
Linux(三)--Unix&Linux 的基础命令 快捷键 Ctl-A 光标移动到行首 Ctl-C 终止命令 Ctl-D 注销登录 Ctl-E 光标移动到行尾 Ctl-U 删除光标到行首的所 ...
- phpdocumentor 安装以及使用说明
一 缘由 最近改版公司网站和app端的api,发现很多函数和方法都没写注释,搞得每次调用之前还需要看底层实现,有的方法名和功能还类似,区分不出使用哪个最优!为了避免给后人挖坑,除了将代码写得规范外, ...
- sys系统模块
import sys # print(0)## sys.exit(0)## print(123) # print(sys.version)# print(sys.maxsize)# print(sys ...
- 逆向-攻防世界-logmein
iDA载入程序,shift+F12查看关键字符串,找到双击来到所在地址,进入函数 然后进入主函数, 经过分析,可以得出:输入的字符要等于 经过处理的v7和v8的异或.v8很明显,但是v7是怎么回事呢 ...
- flask wtforms组件详解
一.简介 在flask内部并没有提供全面的表单验证,所以当我们不借助第三方插件来处理时候代码会显得混乱,而官方推荐的一个表单验证插件就是wtforms.wtfroms是一个支持多种web框架的form ...
- 13 在 Django REST framework 善用 SerializerMethodField方法
01-使用SerializerMethodField 来优化不必要的查询 class RepairQueueSerializer(serializers.ModelSerializer): # rq_ ...
- zcu102 hdmi example(一)
1,概述 有一个计划是打算做一个摄像头的驱动与显示. 但是实际上手上只有一个zcu102开发板,没有摄像头,也没有上位机,自己也不会写.所以就将方案阉割成将录制好的视频放在SD卡里面,然后从SD卡里面 ...