日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Keras 获取中间某一层输出

發(fā)布時間:2023/12/9 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Keras 获取中间某一层输出 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.使用函數(shù)模型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 #假設(shè)訓(xùn)練和測試使用同一組數(shù)據(jù) 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權(quán)重過后 28 #取某一層的輸出為輸出新建為model,采用函數(shù)模型 29 dense1_layer_model = Model(inputs=model.input, 30 outputs=model.get_layer('Dense_1').output) 31 #以這個model的預(yù)測值作為輸出 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的函數(shù): 39 #這是一個theano的函數(shù) 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]

?

效果應(yīng)該是一樣的。

---------------------
作者:哈哈進步
來源:CSDN
原文:https://blog.csdn.net/hahajinbu/article/details/77982721
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請附上博文鏈接!

轉(zhuǎn)載于:https://www.cnblogs.com/as3asddd/p/10129241.html

總結(jié)

以上是生活随笔為你收集整理的Keras 获取中间某一层输出的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。