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] `
plt打印图片无法显示问题。
import matplotlib.pyplotas plt
plt.imshow(img)
#控制台打印出图像对象的信息,而图像没有显示
解决方法:
#引入pylab解决
import matplotlib.pyplotas plt
import pylab
plt.imshow(img)
pylab.show()
python matplotlib.pyplot 显示中文title等参数
# -*- coding: utf-8 -*
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
plt.figure(1)
plt.plot(x, y)
plt.xlabel(u'我是横坐标')
plt.ylabel(u'我是纵坐标')
plt.show()
参考文献
https://blog.csdn.net/hahajinbu/article/details/77982721
https://blog.csdn.net/alickr/article/details/72804258
https://blog.csdn.net/renjunsong0/article/details/55057173