Caffeinated Logistic Regression of HDF5 Data

xiaoxiao2021-02-28  139

Caffeinated Logistic Regression of HDF5 Data

该例子是利用Caffe完成一个浅层模型的训练,这里使用手工生成的数据训练一个logistic regression for classification。手工生成的数据将保存到HDF5中,然后以向量的形式输入到Caffe中。例子中分别对比了sklearn-learn库和两个Caffe模型进行训练和预测,对比了训练时间和精度,可以看到Caffe在较少的时间内可以获得较高的训练精度。

整个过程包括:定义模型、实验和部署。l

本文为了运行于本机,只做了细微修改和中文说明,原始文件来源于Caffe官网对应的Notebook Examples。http://nbviewer.ipython.org/github/BVLC/caffe/blob/master/examples/hdf5_classification.ipynb

---Last update 2015年6月7日

Setup

import numpy as np import pandas as pd import matplotlib.pyplot as plt %matplotlib inline # 切换工作目录到 caffe-master %cd '/home/ouxinyu/caffe-master' # Make sure that caffe is on the python path: caffe_root = './' # this file is expected to be in {caffe_root}/examples import sys sys.path.insert(0, caffe_root + 'python') import caffe import os import h5py import shutil import tempfile # You may need to 'pip install scikit-learn' import sklearn import sklearn.datasets import sklearn.linear_model /home/ouxinyu/caffe-master

生成训练数据

生成10000个4-vectors的向量用于binary分类,包含2种informative feature和2种noise features。

X, y = sklearn.datasets.make_classification( n_samples=10000, n_features=4, n_redundant=0, n_informative=2, n_clusters_per_class=2, hypercube=False, random_state=0 ) # Split into train and test X, Xt, y, yt = sklearn.cross_validation.train_test_split(X, y) # Visualize sample of the data ind = np.random.permutation(X.shape[0])[:1000] df = pd.DataFrame(X[ind]) _ = pd.scatter_matrix(df, figsize=(9, 9), diagonal='kde', marker='o', s=40, alpha=.4, c=y[ind])
转载请注明原文地址: https://www.6miu.com/read-30022.html

最新回复(0)