基于Theano的深度学习框架keras及配合SVM训练模型
2016-05-06 20:35
633 查看
1.介绍
Keras是基于Theano的一个深度学习框架,它的设计参考了Torch,用Python语言编写,是一个高度模块化的神经网络库,支持GPU和CPU。keras官方文档地址 地址
2.流程
先使用CNN进行训练,利用Theano函数将CNN全连接层的值取出来,给SVM进行训练
3.结果示例
因为这里只是一个演示keras&SVM的demo,未对参数进行过多的尝试,结果一般
4.代码
由于keras文档、代码更新,目前网上很多代码都不能使用,下面贴上我的代码,可以直接运行
Keras是基于Theano的一个深度学习框架,它的设计参考了Torch,用Python语言编写,是一个高度模块化的神经网络库,支持GPU和CPU。keras官方文档地址 地址
2.流程
先使用CNN进行训练,利用Theano函数将CNN全连接层的值取出来,给SVM进行训练
3.结果示例
因为这里只是一个演示keras&SVM的demo,未对参数进行过多的尝试,结果一般
4.代码
由于keras文档、代码更新,目前网上很多代码都不能使用,下面贴上我的代码,可以直接运行
from keras.models import Sequential from keras.layers.core import Dense, Dropout, Activation,Flatten from keras.layers.convolutional import Convolution2D, MaxPooling2D from keras.optimizers import SGD from keras.datasets import mnist from keras.layers import BatchNormalization from sklearn.svm import SVC import theano from keras.utils import np_utils def svc(traindata,trainlabel,testdata,testlabel): print("Start training SVM...") svcClf = SVC(C=1.0,kernel="rbf",cache_size=3000) svcClf.fit(traindata,trainlabel) pred_testlabel = svcClf.predict(testdata) num = len(pred_testlabel) accuracy = len([1 for i in range(num) if testlabel[i]==pred_testlabel[i]])/float(num) print("cnn-svm Accuracy:",accuracy) #each add as one layer model = Sequential() #1 .use convolution,pooling,full connection model.add(Convolution2D(5, 3, 3,border_mode='valid',input_shape=(1, 28, 28),activation='tanh')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Convolution2D(10, 3, 3,activation='tanh')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(100,activation='tanh')) #Full connection model.add(Dense(10,activation='softmax')) #2 .just only user full connection # model.add(Dense(100,input_dim = 784, init='uniform',activation='tanh')) # model.add(Dense(100,init='uniform',activation='tanh')) # model.add(Dense(10,init='uniform',activation='softmax')) # sgd = SGD(lr=0.2, decay=1e-6, momentum=0.9, nesterov=True) model.compile(optimizer='sgd', loss='categorical_crossentropy') (X_train, y_train), (X_test, y_test) = mnist.load_data() #change data type,keras category need ont hot #2 reshape #X_train = X_train.reshape(X_train.shape[0],X_train.shape[1]*X_train.shape[2]) #X_train.shape[0] 60000 X_train.shape[1] 28 X_train.shape[2] 28 #1 reshape X_train = X_train.reshape(X_train.shape[0],1,X_train.shape[1],X_train.shape[2]) Y_train = np_utils.to_categorical(y_train, 10) #new label for svm y_train_new = y_train[0:42000] y_test_new = y_train[42000:] #new train and test data X_train_new = X_train[0:42000] X_test = X_train[42000:] Y_train_new = Y_train[0:42000] Y_test = Y_train[42000:] model.fit(X_train_new, Y_train_new, batch_size=200, nb_epoch=100,shuffle=True, verbose=1, show_accuracy=True, validation_split=0.2) print("Validation...") val_loss,val_accuracy = model.evaluate(X_test, Y_test, batch_size=1,show_accuracy=True) print "val_loss: %f" %val_loss print "val_accuracy: %f" %val_accuracy #define theano funtion to get output of FC layer get_feature = theano.function([model.layers[0].input],model.layers[5].get_output(train=False),allow_input_downcast=False) FC_train_feature = get_feature(X_train_new) FC_test_feature = get_feature(X_test) svc(FC_train_feature,y_train_new,FC_test_feature,y_test_new)
相关文章推荐
- HDU 1227 dp距离和最小,中位数的应用
- HDU 2069 & UVA 674 Coin Change(换硬币 dp 入门经典水题,背包问题)
- 22.复杂链表的复制
- 结构体字节对齐
- Hadoop安装及开发
- mock私有变量的时候出现的问题
- POJ3177 Redundant Paths
- C经典 结构图的三种定义方式
- cocos-js,时间相关的处理
- Hadoop HDFS Tools
- 手工MAVEN建立WEBAPP项目并打包部署
- lightoj 1074 spfa判断负环
- 日记帐导入-来源为“采购”日记帐导入失败问题
- 文件传输基础—Java IO流 FileInputStream 的运用
- deep learning 练习1 线性回归练习
- 虚拟化_KVM
- 代理模式:控制对对象的访问
- 前端-js-this
- CentOS允许/禁止ping的方法
- IE7-span折行问题