初窥keras

代码来源于如何用卷积神经网络CNN识别手写数字集?
加了一点可视化的输出,可视化需要安装pydot 和 graphviz ,可以命令行输入
pip install pydot-ng & brew install graphviz
如果失败,在ubuntu环境下可以输入 sudo apt install graphviz

上代码:

# Larger CNN for the MNIST Dataset
import numpy
from keras.datasets import mnist
from keras.models import Sequential, save_model
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Convolution2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
import matplotlib.pyplot as plt
from keras.constraints import maxnorm
from keras.optimizers import SGD
from keras.models import load_model
from statsmodels.sandbox.regression.kernridgeregress_class import plt_closeall
from bokeh.plotting.figure import figure

# del X_train
# del X_test 
# del y_test 
# del y_train
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# reshape to be [samples][pixels][width][height]
X_train = X_train.reshape(X_train.shape[0], 28, 28,1).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 28, 28,1).astype('float32')
# normalize inputs from 0-255 to 0-1
X_train = X_train / 255
X_test = X_test / 255
# one hot encode outputs
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
num_classes = y_test.shape[1]
###raw
# define the larger model

def larger_model():
    # create model
    model = Sequential()
    model.add(Convolution2D(30, 5, 5, border_mode='valid', input_shape=(28, 28,1), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.4))
    model.add(Convolution2D(15, 3, 3, activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.4))
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dropout(0.4))
    model.add(Dense(50, activation='relu'))
    model.add(Dropout(0.4))
    model.add(Dense(num_classes, activation='softmax'))
    # Compile model
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

# build the model
model = larger_model()
# Fit the model
model.fit(X_train, y_train, validation_data=(X_test, y_test), nb_epoch=3, batch_size=32,verbose=2)

#存储模型
model.save('my_model.h5')
del model

#载入模型
model=load_model('my_model.h5')
#可视化
from keras.utils.visualize_util import plot
# import keras.utils.visualize_util.plot as plot
plot(model, to_file='model.png')
#获取中间层
from keras.models import Model
intermediate_layer_model = Model(input=model.input,
                                 output=model.layers[3].output)
intermediate_output = intermediate_layer_model.predict(X_test[4:5])
plt.figure()
plt.imshow(X_test[4,:,:,0],cmap='gray')
print(intermediate_output.shape)
I=intermediate_output#.reshape(30,24,24)
sp=I.shape
#显示
plt.figure()
for i in range(0,sp[3]):
    plt.subplot(5,6,i+1)
    plt.imshow(I[0,:,:,i],cmap ='gray')
plt.show()

scores = model.evaluate(X_test, y_test, verbose=0)
print("%.2f%%" % (scores[0]*100),"%.2f%%" % (scores[1]*100))
# print(scores)

网络结构:
这里写图片描述
第一个卷积层输出
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

第二个卷积层的输出
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值