基于Theano的深度学习框架keras及配合SVM训练模型
https://blog.csdn.net/a819825294/article/details/51334397
1.介绍
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)
---------------------
作者:雪伦_
来源:CSDN
原文:https://blog.csdn.net/a819825294/article/details/51334397
版权声明:本文为博主原创文章,转载请附上博文链接!
基于Theano的深度学习框架keras及配合SVM训练模型的更多相关文章
- 基于Windows,Python,Theano的深度学习框架Keras的配置
1.安装Anaconda 面向科学计算的Python IDE--Anaconda 2.打开Anaconda Prompt 3.安装gcc环境 (1)conda update conda (2)cond ...
- (转) 基于Theano的深度学习(Deep Learning)框架Keras学习随笔-01-FAQ
特别棒的一篇文章,仍不住转一下,留着以后需要时阅读 基于Theano的深度学习(Deep Learning)框架Keras学习随笔-01-FAQ
- 如何评价深度学习框架Keras?
python机器学习-乳腺癌细胞挖掘(博主亲自录制视频)https://study.163.com/course/introduction.htm?courseId=1005269003&ut ...
- 深度学习框架Keras与Pytorch对比
对于许多科学家.工程师和开发人员来说,TensorFlow是他们的第一个深度学习框架.TensorFlow 1.0于2017年2月发布,可以说,它对用户不太友好. 在过去的几年里,两个主要的深度学习库 ...
- 深度学习框架Keras介绍及实战
Keras 是一个用 Python 编写的高级神经网络 API,它能够以 TensorFlow, CNTK, 或者 Theano 作为后端运行.Keras 的开发重点是支持快速的实验.能够以最小的时延 ...
- 常用深度学习框架(keras,pytorch.cntk,theano)conda 安装--未整理
版本查询 cpu tensorflow conda env list source activate tensorflow python import tensorflow as tf 和 tf.__ ...
- 深度学习框架Keras安装
环境:Windows 10 64位 版本!版本!版本!不要下载最新版本的! 一点要按照这个来!安装顺序也最好不要错! 首先安装DirectX SDK工具包 ,这是链接:https://www.micr ...
- 从Theano到Lasagne:基于Python的深度学习的框架和库
从Theano到Lasagne:基于Python的深度学习的框架和库 摘要:最近,深度神经网络以“Deep Dreams”形式在网站中如雨后春笋般出现,或是像谷歌研究原创论文中描述的那样:Incept ...
- 深度学习框架比较TensorFlow、Theano、Caffe、SciKit-learn、Keras
TheanoTheano在深度学习框架中是祖师级的存在.Theano基于Python语言开发的,是一个擅长处理多维数组的库,这一点和numpy很像.当与其他深度学习库结合起来,它十分适合数据探索.它为 ...
随机推荐
- centos mysql安装 完全版
在linux中安装数据库首选MySQL,Mysql数据库的第一个版本就是发行在Linux系统上,其他选择还可以有postgreSQL,oracle等 在Linux上安装mysql数据库,我们可以去其官 ...
- abap function module中的异常处理
1: 定义一个有异常抛出的function module. (zfm_moudle6), 该函数中有符合exceptions中的异常,将会自动将exceptions中的异常抛出. FUNCTION ...
- Python复习基础篇
4.4 使用列表的一部分 Python切片(切片就是取值的呗) print(players[0:3]) 中括号,冒号隔开,顾头不顾尾. print([:4]) 从开始取还是会顾尾的 pri ...
- sjw-风评评测-定位页面元素
一.手工标准化操作流程: 1.登录系统 2.登录后的页面点击:账户设置 3.点击“重新评测”,进入到风险评测页面 4.答完8道题 5.勾选条件checkbox 6.点击“提交” 提交后的页面 二.自动 ...
- 《全栈性能Jmeter》-2性能测试初体验
- C++编译变更stlport到使用g++的stl经验总结
. 花了几天时间,需要把经验给记下来. 1. 需要支持C++11的编译器,最方便的方式就是 yum -y install devtoolset-6 在/usr/local/bin中增加gcc6.sh ...
- QT linux
一. 如果你是服务器版的CentOS,我建议你安装一下图形界面 1. 首先安装X window: yum groupinstall 'X WindowSystem'2. 然后安装GNOME: yum ...
- opencv安装终结版
---恢复内容开始--- 1.安装Qt5.9.1 Qt官网址:https://download.qt.io/official_releases/qt/ 根据自己的需要选择版本,这里我选择的是qt-op ...
- teragen/terasort_简化版
1, 关闭Hadoop安全模式 进入hdfs用户 su – hdfs Cd /opt/cloudera/parcels/CDH-5.12.1-1.cdh5.12.1.p0.3/bin hdfs dfs ...
- 封装及调用fetch
一.封装fetch 创建fetch/index.js import 'whatwg-fetch' import 'es6-promise' export function get(url) { let ...