#coding:utf8
import cPickle
import gzip
import numpy as np
from sklearn.svm import libsvm class SVM(object):
def __init__(self, kernel='rbf', degree=3, gamma='auto',
coef0=0.0, tol=1e-3, C=1.0,nu=0., epsilon=0.,shrinking=True, probability=False,
cache_size=200, class_weight=None, max_iter=-1):
self.kernel = kernel
self.degree = degree
self.gamma = gamma
self.coef0 = coef0
self.tol = tol
self.C = C
self.nu = nu
self.epsilon = epsilon
self.shrinking = shrinking
self.probability = probability
self.cache_size = cache_size
self.class_weight = class_weight
self.max_iter = max_iter def fit(self, X, y):
X= np.array(X, dtype=np.float64, order='C')
cls, y = np.unique(y, return_inverse=True)
weight = np.ones(cls.shape[0], dtype=np.float64, order='C')
self.class_weight_=weight
self.classes_ = cls
y= np.asarray(y, dtype=np.float64, order='C')
sample_weight = np.asarray([])
solver_type =0
self._gamma = 1.0 / X.shape[1]
kernel = self.kernel
seed = np.random.randint(np.iinfo('i').max)
self.support_, self.support_vectors_, self.n_support_, \
self.dual_coef_, self.intercept_, self.probA_, \
self.probB_, self.fit_status_ = libsvm.fit(
X, y,
svm_type=solver_type, sample_weight=sample_weight,
class_weight=self.class_weight_, kernel=kernel, C=self.C,
nu=self.nu, probability=self.probability, degree=self.degree,
shrinking=self.shrinking, tol=self.tol,
cache_size=self.cache_size, coef0=self.coef0,
gamma=self._gamma, epsilon=self.epsilon,
max_iter=self.max_iter, random_seed=seed)
self.shape_fit_ = X.shape
self._intercept_ = self.intercept_.copy()
self._dual_coef_ = self.dual_coef_
self.intercept_ *= -1
self.dual_coef_ = -self.dual_coef_
return self def predict(self, X):
X= np.array(X,dtype=np.float64, order='C')
svm_type = 0
return libsvm.predict(
X, self.support_, self.support_vectors_, self.n_support_,
self._dual_coef_, self._intercept_,
self.probA_, self.probB_, svm_type=svm_type, kernel=self.kernel,
degree=self.degree, coef0=self.coef0, gamma=self._gamma,
cache_size=self.cache_size) def load_data():
f = gzip.open('../data/mnist.pkl.gz', 'rb')
training_data, validation_data, test_data = cPickle.load(f)
f.close()
return (training_data, validation_data, test_data) def svm_test():
training_data, validation_data, test_data = load_data()
clf = SVM(kernel='linear') # 'linear', 'poly', 'rbf', 'sigmoid', 'precomputed'
clf.fit(training_data[0][:10000], training_data[1][:10000])
predictions = [int(a) for a in clf.predict(test_data[0][:10000])]
num_correct = sum(int(a == y) for a, y in zip(predictions, test_data[1][:10000]))
print "Baseline classifier using an SVM."
print "%s of %s values correct." % (num_correct, len(test_data[1][:10000])) # 0.9172 'rbf'=0.9214 if __name__ == "__main__":
svm_test()

采用libsvm进行mnist训练的更多相关文章

  1. 使用libsvm对MNIST数据集进行实验

    使用libsvm对MNIST数据集进行实验 在学SVM中的实验环节,老师介绍了libsvm的使用.当时看完之后感觉简单的说不出话来. 1. libsvm介绍 虽然原理要求很高的数学知识等,但是libs ...

  2. 从一到二:利用mnist训练集生成的caffemodel对mnist测试集与自己手写的数字进行测试

    通过从零到一的教程,我们已经得到了通过mnist训练集生成的caffemodel,主要包含下面四个文件: 接下来就可以利用模型进行测试了.关于测试方法按照上篇教程还是选择bat文件,当然python. ...

  3. Ubuntu16.04安装TensorFlow及Mnist训练

    版权声明:本文为博主原创文章,欢迎转载,并请注明出处.联系方式:460356155@qq.com TensorFlow是Google开发的开源的深度学习框架,也是当前使用最广泛的深度学习框架. 一.安 ...

  4. 利用mnist训练集生成的caffemodel对mnist测试集与自己手写的数字进行测试

    从一到二:利用mnist训练集生成的caffemodel对mnist测试集与自己手写的数字进行测试 通过从零到一的教程,我们已经得到了通过mnist训练集生成的caffemodel,主要包含下面四个文 ...

  5. 使用libsvm对MNIST数据集进行实验---浅显易懂!

    原文:http://blog.csdn.net/arthur503/article/details/19974057 在学SVM中的实验环节,老师介绍了libsvm的使用.当时看完之后感觉简单的说不出 ...

  6. windows环境Caffe安装配置步骤(无GPU)及mnist训练

    在硕士第二年,义无反顾地投身到了深度学习的浪潮中.从之前的惯性导航转到这个方向,一切从头开始,在此,仅以此文记录自己的打怪之路. 最初的想法是动手熟悉Caffe,考虑到直接上手Ubuntu会有些难度, ...

  7. TensorFlow下利用MNIST训练模型识别手写数字

    本文将参考TensorFlow中文社区官方文档使用mnist数据集训练一个多层卷积神经网络(LeNet5网络),并利用所训练的模型识别自己手写数字. 训练MNIST数据集,并保存训练模型 # Pyth ...

  8. 使用caffemodel模型(由mnist训练)测试单张手写数字样本

    caffe中训练和测试mnist数据集都是批处理,可以反馈识别率,但是看不到单张样本的识别效果,这里使用windows自带的画图工具手写制作0~9的测试数字,然后使用caffemodel模型识别. 1 ...

  9. 【caffe】mnist训练日志

    @tags caffe 前面根据train_lenet.sh改写了train_lenet.py后,在根目录下执行它,得到一系列输出,内容如下: I1013 10:05:16.721294 1684 c ...

随机推荐

  1. php的查询数据

    php中 连接数据库,通过表格形式输出,查询数据.全选时,下面的分选项都选中;子选项取消一个时,全选按钮也取消选中. <!DOCTYPE html PUBLIC "-//W3C//DT ...

  2. 主线程中一定不能放耗时操作,必须要开子线程,比如下载文件,不然会不让你拿到输入流--报错显示android.os.NetworkOnMainThreadException

    1.必须要开子线程来操作耗时操作,android.os.NetworkOnMainThreadException new Thread(new Runnable() { @Override publi ...

  3. 关于resolve非泛型方法不能与类型实参一起使用

    今天mvc新建三层时,写到bll层中一直报下面的错误,检查了几遍赶脚并没有什么错.最后发现缺少一些引用. 如下面的图,少添加了下面的两个引用.Unity是微软模式与实践团队开发的一个轻量级.可扩展的依 ...

  4. spring mvc + freemarker优雅的实现邮件定时发送

    1. spring mvc工程中引入相关freemarker\mail的包 如:pom.xml中加入类似 <dependency> <groupId>javax.mail< ...

  5. IIS6的session丢失问题

    解决办法:      a IIS6中相比IIS5增加了一个应用程序池,默认是使用DefaultAppPool.      b   先为站点建立一个应用程序池,打开IIS管理器,右键点击应用程序池-新建 ...

  6. 一个经典例子让你彻彻底底理解java回调机制

    转帖请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/17483273),请尊重他人的辛勤劳动成果,谢谢 所谓回调: ...

  7. 谷歌、火狐浏览器gift图片缓存后不显示动态效果

    <script> $(function(){ $('.center img').prop("src","images/service/01.gif" ...

  8. CODEVS1380 没有上司的舞会 (树形DP)

    f[i,0] 表示 第i个人不参加舞会 f[i,1] 表示 第i个人参加舞会 f[i,1]=sigma(f[j,0])+v[i]   j 为 i 的孩子 f[i,1]=sigma(max(f[j,0] ...

  9. NSNumber 、 NSValue 、 日期处理 、 集合类 、 NSArray(一)

    1 基本数据类型的封装 1.1 问题 我们所学的所有基本数据类型,如int.float.double.char等,都不是对象,不能向它们发送消息.然而,在Foundation中的许多类,如NSArra ...

  10. 尽量多的以 const/enum/inline 替代 #define

    前言 在面向过程语言,如 C 语言中,#define 非常常见,也确实好用,值得提倡.但在如今面向对象的语言,如 C++ 语言中,#define 就要尽量少用了. 为何在 C++ 中就要少用了呢? 这 ...