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.pswp.cn/news/278661.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278661.shtml
英文地址,請注明出處:http://en.pswp.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上的“…