Keras Theano 输出中间层结果
生活随笔
收集整理的這篇文章主要介紹了
Keras Theano 输出中间层结果
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Keras & Theano?get output of an intermediate layer
1.使用函數(shù)模型API,新建一個model,將輸入和輸出定義為原來的model的輸入和想要的那一層的輸出,然后重新進行predict.
import seaborn as sbn import pylab as plt import theano from keras.models import Sequential from keras.layers import Dense,Activationfrom keras.models import Modelmodel = 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 #假設訓練和測試使用同一組數(shù)據(jù) 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權(quán)重過后 #取某一層的輸出為輸出新建為model,采用函數(shù)模型 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的函數(shù):#這是一個theano的函數(shù) 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轉(zhuǎn)載于:https://www.cnblogs.com/jins-note/p/9767403.html
總結(jié)
以上是生活随笔為你收集整理的Keras Theano 输出中间层结果的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mongodb模糊查询包含特殊字符
- 下一篇: ceres入门学习