采用libsvm进行mnist训练
#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训练的更多相关文章
- 使用libsvm对MNIST数据集进行实验
使用libsvm对MNIST数据集进行实验 在学SVM中的实验环节,老师介绍了libsvm的使用.当时看完之后感觉简单的说不出话来. 1. libsvm介绍 虽然原理要求很高的数学知识等,但是libs ...
- 从一到二:利用mnist训练集生成的caffemodel对mnist测试集与自己手写的数字进行测试
通过从零到一的教程,我们已经得到了通过mnist训练集生成的caffemodel,主要包含下面四个文件: 接下来就可以利用模型进行测试了.关于测试方法按照上篇教程还是选择bat文件,当然python. ...
- Ubuntu16.04安装TensorFlow及Mnist训练
版权声明:本文为博主原创文章,欢迎转载,并请注明出处.联系方式:460356155@qq.com TensorFlow是Google开发的开源的深度学习框架,也是当前使用最广泛的深度学习框架. 一.安 ...
- 利用mnist训练集生成的caffemodel对mnist测试集与自己手写的数字进行测试
从一到二:利用mnist训练集生成的caffemodel对mnist测试集与自己手写的数字进行测试 通过从零到一的教程,我们已经得到了通过mnist训练集生成的caffemodel,主要包含下面四个文 ...
- 使用libsvm对MNIST数据集进行实验---浅显易懂!
原文:http://blog.csdn.net/arthur503/article/details/19974057 在学SVM中的实验环节,老师介绍了libsvm的使用.当时看完之后感觉简单的说不出 ...
- windows环境Caffe安装配置步骤(无GPU)及mnist训练
在硕士第二年,义无反顾地投身到了深度学习的浪潮中.从之前的惯性导航转到这个方向,一切从头开始,在此,仅以此文记录自己的打怪之路. 最初的想法是动手熟悉Caffe,考虑到直接上手Ubuntu会有些难度, ...
- TensorFlow下利用MNIST训练模型识别手写数字
本文将参考TensorFlow中文社区官方文档使用mnist数据集训练一个多层卷积神经网络(LeNet5网络),并利用所训练的模型识别自己手写数字. 训练MNIST数据集,并保存训练模型 # Pyth ...
- 使用caffemodel模型(由mnist训练)测试单张手写数字样本
caffe中训练和测试mnist数据集都是批处理,可以反馈识别率,但是看不到单张样本的识别效果,这里使用windows自带的画图工具手写制作0~9的测试数字,然后使用caffemodel模型识别. 1 ...
- 【caffe】mnist训练日志
@tags caffe 前面根据train_lenet.sh改写了train_lenet.py后,在根目录下执行它,得到一系列输出,内容如下: I1013 10:05:16.721294 1684 c ...
随机推荐
- WPF 中动态创建、删除控件,注册控件名字,根据名字查找控件
动态创建控件 1.容器控件.RegisterName("Name",要注册的控件) //注册控件 2.容器控件.FindName("Name") as 控 ...
- (转)phoneGap-Android开发环境搭建
(原)http://www.cnblogs.com/shawn-xie/archive/2012/08/15/2638480.html phoneGap-Android开发环境搭建 一.安装 在安 ...
- Alice and Bob(不断补充)
我之前做过一些博弈的题目,以为博弈都是DP,结果被坑了很多次,其实博弈有很多种,在此,把我见过的类型都搬上来. 1,HDU3951(找规律) 题意:把n枚硬币围成一个圆,让Alice和Bob两个人分别 ...
- C#_控件——CheckBox,TextBox,RequiredFieldValidator
1. <asp:CheckBox ID="CheckBox2" runat="server" Text="你大爷" AutoPostB ...
- mysql的ERROR:1042
在虚拟机上测试数据库备份功能,需要连接外部机器上的mysql,pdo总是报超时错误! 起初认为是用的mysql账号的域不匹配!后来发现不是因为这个! 在终端中用mysql命令尝试连接,发现返回的错误是 ...
- 【转】Web应用的组件化开发(一)
原文转自:http://blog.jobbole.com/56161/ 基本思路 1. 为什么要做组件化? 无论前端也好,后端也好,都是整个软件体系的一部分.软件产品也是产品,它的研发过程也必然是有其 ...
- 第一个Sprint冲刺第四天
讨论成员:邵家文.李新.朱浩龙.陈俊金 讨论问题:掌握计时技术的知识 讨论地点:qq网络 进展:即将开始对计时技术代码的编写
- 《JavaScript Ninja》之函数是根基
函数是根基 理解函数为什么如此重要 JavaScript 是一门 函数式语言 . 函数为什么是第一型对象 在 JavaScript 中,函数可以共处,可以将其视为其他任意类型的 JavaScript ...
- 如何将character_set_database latin1 改为 gbk(转)
第一篇文章: 原地址: Linux服务器下文件名为 my.cnf Windows 下文件名为 my.ini 问题:通过sql语言向数据库中添加中文的数据的时候,查询是显示的是乱码. 原因:当初安装数据 ...
- ML2 – Address Population
Why do we need it, whatever it is? VM unicast, multicast and broadcast traffic flow is detailed in m ...