Keras 获取中间某一层输出

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

 1 #coding=utf-8
 2 import seaborn as sbn
 3 import pylab as plt
 4 import theano
 5 from keras.models import Sequential
 6 from keras.layers import Dense,Activation
 7 
 8 
 9 from keras.models import Model
10 
11 model = Sequential()
12 model.add(Dense(32, activation='relu', input_dim=100))
13 model.add(Dense(16, activation='relu',name="Dense_1"))
14 model.add(Dense(1, activation='sigmoid',name="Dense_2"))
15 model.compile(optimizer='rmsprop',
16 loss='binary_crossentropy',
17 metrics=['accuracy'])
18 
19 # Generate dummy data
20 import numpy as np
21 #假设训练和测试使用同一组数据
22 data = np.random.random((1000, 100))
23 labels = np.random.randint(2, size=(1000, 1))
24 
25 # Train the model, iterating on the data in batches of 32 samples
26 model.fit(data, labels, epochs=10, batch_size=32)
27 #已有的model在load权重过后
28 #取某一层的输出为输出新建为model,采用函数模型
29 dense1_layer_model = Model(inputs=model.input,
30 outputs=model.get_layer('Dense_1').output)
31 #以这个model的预测值作为输出
32 dense1_output = dense1_layer_model.predict(data)
33 
34 print dense1_output.shape
35 print dense1_output[0]
36 
37 
38 2.因为我的后端是使用的theano,所以还可以考虑使用theano的函数:
39 #这是一个theano的函数
40 dense1 = theano.function([model.layers[0].input],model.layers[1].output,allow_input_downcast=True)
41 dense1_output = dense1(data) #visualize these images's FC-layer feature
42 print dense1_output[0]

 

效果应该是一样的。

---------------------
作者:哈哈进步
来源:CSDN
原文:https://blog.csdn.net/hahajinbu/article/details/77982721
版权声明:本文为博主原创文章,转载请附上博文链接!

转载于:https://www.cnblogs.com/as3asddd/p/10129241.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/278661.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

在Windows 7中禁用或修改Aero Peek的“延迟时间”

Are you looking for an easy way to modify the “delay time” for Aero Peek in Windows 7 or perhaps want to disable the feature altogether? Then see how simple it is to do either with the Desktop Peek Tweak. 您是否正在寻找一种简便的方法来修改Windows 7中Aer…

php内核分析(六)-opcode

这里阅读的php版本为PHP-7.1.0 RC3,阅读代码的平台为linux 查看opcode php是先把源码解析成opcode,然后再把opcode传递给zend_vm进行执行的。 // 一个opcode的结构 struct _zend_op {const void *handler; // opcode对应的执行函数,每个opcod…

vue 监听路由变化

一.watch监听$route($router的对象) // 监听,当路由发生变化的时候执行 watch:{$route(to,from){console.log(to.path);} },// 监听,当路由发生变化的时候执行 watch: {$route: {handler: function(val, oldVal){console.log(val);},// 深度观察监听dee…

音频剪切_音频编辑入门指南:剪切,修剪和排列

音频剪切Audacity novices often start with lofty project ideas, but sometimes they lack the basics. Knowing how to cut and trim tracks is basic audio editing and is a fundamental starting point for making more elaborate arrangements. 大胆的新手通常从崇高的项…

Mybatis自定义SQL拦截器

本博客介绍的是继承Mybatis提供的Interface接口,自定义拦截器,然后将项目中的sql拦截一下,打印到控制台。 先自定义一个拦截器 package com.muses.taoshop.common.core.database.config;import org.apache.commons.lang3.StringUtils; import…

搭建spring boot环境并测试一个controller

Idea搭建spring boot环境一、新建项目二、起步依赖三、编写SpringBoot引导类四、编写Controller五、热部署一、新建项目 1.新建project 2.选择SpringInitializr,选择jdk,没有则需要下载并配置(若选择Maven工程则需要自己添加pom.xml所需依赖坐标和Java…

音频噪声抑制_音频编辑入门指南:基本噪声消除

音频噪声抑制Laying down some vocals? Starting your own podcast? Here’s how to remove noise from a messy audio track in Audacity quickly and easily. 放下人声? 开始自己的播客? 这是在Audacity中快速轻松地消除杂乱音轨中噪声的方法。 Th…

Dubbo集群容错

转自dubbo官网文档http://dubbo.apache.org/zh-cn/blog/dubbo-cluster-error-handling.html Design For failure 在分布式系统中,集群某个某些节点出现问题是大概率事件,因此在设计分布式RPC框架的过程中,必须要把失败作为设计的一等公民来对…

Linux基础(day53)

2019独角兽企业重金招聘Python工程师标准>>> 12.21 php-fpm的pool php-fpm的pool目录概要 vim /usr/local/php/etc/php-fpm.conf//在[global]部分增加include etc/php-fpm.d/*.confmkdir /usr/local/php/etc/php-fpm.d/cd /usr/local/php/etc/php-fpm.d/vim www.co…

Mysql+Navicat for Mysql

一、mysql 1.下载安装 Mysql官网下载地址 下载后解压 .zip (或安装.msi) 2.可加入全局变量mysqld (可选) 我的电脑->属性->高级->环境变量->Path(系统变量),添加mysql下的bin目录,如 D:\Pr…

公钥,私钥和数字签名

一、公钥加密 假设一下,我找了两个数字,一个是1,一个是2。我喜欢2这个数字,就保留起来,不告诉你们(私钥),然后我告诉大家,1是我的公钥。 我有一个文件,不能让别人看&…

MySQL中的日志类型(二)-General query log

简介 General query log记录客户端的连接和断开,以及从客户端发来的每一个SQL语句。 日志内容格式 General query log可以记录在文件中,也可以记录在表中,格式如下:在文件中会记录时间、线程ID、命令类型以及执行的语句示例如下&a…

android wi-fi_如何在Android手机上查找3G或Wi-Fi速度

android wi-fiAre you curious about what kind of connection speed you are getting with your Android phone? Today we’ll take a look at how to easily check your Wi-Fi or 3G speeds with Speedtest.net’s Speed Test app. 您是否对Android手机的连接速度感到好奇&a…

vue引入全局less实现全局变量的控制

vue引入全局less1.设置全局样式变量的好处:2.以less为例(sass等同原理)1.vue-cli2搭建的项目(1)2.vue-cli2搭建的项目(2)3.vue-cli3、vue-cli43.vue-cli2和vue-cli3的区别4.vue-cli3和vue-cli4的…

如何在eclipse中对项目进行重新编译

有时由于eclipse异常关闭,当我们重启Eclipse,在启动项目时,会报错,说:ClassNotFound类似的错误,引起这种问题的原因可能是由于,Eclipse异常关闭引起的。 解决:在一个项目中&#xff…

SQL 查询数据库中包含指定字符串的相关表和相关记录

declare str varchar(100)set str我要找的 --要搜索的字符串declare s varchar(8000)declare tb cursor local forselect if exists(select 1 from [b.name] where [a.name] like %str%)print [b.name].[a.name]from syscolumns a join sysobjects b on a.idb.idwhere b.xtype…

如何在Gmail的图片中插入超链接

Adding hyperlinks is an efficient way of getting your reader to the intended web page. Though it’s no secret that you can add hyperlinks to text, Gmail also lets you add hyperlinks to images in the body of the email. Here’s how to make it happen. 添加超链…

内联元素居中

父元素&#xff1a; height:100px; line-height:100px; // 与高相同 text-align:center; 子元素: display:inline; vertical-align: middle; 适用图片、文字 <div><div class"wrapper"><span>我是文字</span></div><div class&qu…

防止html标签转义

function htmlDecode ( str ) {var ele document.createElement(span);ele.innerHTML str;return ele.textContent;} 例如body下边所有的p标签都防止转义&#xff1a; $.each($("body").find(p),function(){this.innerHTML htmlDecode(this.innerHTML);}); 转载于…

新垣结衣自拍照_如何阻止自拍照出现在iPhone的自拍照专辑中

新垣结衣自拍照Khamosh PathakKhamosh PathakThe Photos app on your iPhone automatically populates all photos from the front-facing camera in the Selfies album. But what if you don’t want a photo to appear there? Here are a couple of solutions. iPhone上的“…