DL之RBM:(sklearn自带数据集为1797个样本*64个特征+5倍数据集)深度学习之BRBM模型学习+LR进行分类实现手写数字图识别

简介: DL之RBM:(sklearn自带数据集为1797个样本*64个特征+5倍数据集)深度学习之BRBM模型学习+LR进行分类实现手写数字图识别

输出结果

image.png



image.png





实现代码


from __future__ import print_function

print(__doc__)

import numpy as np              

import matplotlib.pyplot as plt  

from scipy.ndimage import convolve

from sklearn import linear_model, datasets, metrics  

from sklearn.cross_validation import train_test_split

from sklearn.neural_network import BernoulliRBM  

from sklearn.pipeline import Pipeline            

def nudge_dataset(X, Y):  

direction_vectors = [

   [[0, 1, 0],[0, 0, 0],[0, 0, 0]],

   [[0, 0, 0],[1, 0, 0],[0, 0, 0]],

   [[0, 0, 0],[0, 0, 1],[0, 0, 0]],

   [[0, 0, 0],[0, 0, 0],[0, 1, 0]]

   ]

shift = lambda x, w: convolve(x.reshape((8, 8)), mode='constant',weights=w).ravel()

X = np.concatenate([X] +

[np.apply_along_axis(shift, 1, X, vector)

for vector in direction_vectors])

Y = np.concatenate([Y for _ in range(5)], axis=0)

return X, Y

digits = datasets.load_digits()

X = np.asarray(digits.data, 'float32')

X, Y = nudge_dataset(X, digits.target)  

X = (X - np.min(X, 0)) / (np.max(X, 0) + 0.0001)

X_train, X_test, Y_train, Y_test = train_test_split(X, Y,test_size=0.2,random_state=0)

logistic = linear_model.LogisticRegression()

rbm = BernoulliRBM(random_state=0, verbose=True)

classifier = Pipeline(steps=[('rbm', rbm), ('logistic', logistic)])

rbm.learning_rate = 0.06  

rbm.n_iter = 20

# More components tend to give better prediction performance, but larger fitting time

rbm.n_components = 100

logistic.C = 6000.0

classifier.fit(X_train, Y_train)  

logistic_classifier = linear_model.LogisticRegression(C=100.0)

logistic_classifier.fit(X_train, Y_train)

print()

print("Logistic regression using RBM features:\n%s\n" % (

   metrics.classification_report(

       Y_test,classifier.predict(X_test)  

       )

   ))

print("Logistic regression using raw pixel features:\n%s\n" % (

metrics.classification_report(

Y_test,

logistic_classifier.predict(X_test))))

plt.figure(figsize=(4.2, 4))

for i, comp in enumerate(rbm.components_):

plt.subplot(10, 10, i + 1)

plt.imshow(comp.reshape((8, 8)), cmap=plt.cm.gray_r,

interpolation='nearest')

plt.xticks(())

plt.yticks(())

plt.suptitle('100 components extracted by RBM', fontsize=16)

plt.subplots_adjust(0.08, 0.02, 0.92, 0.85, 0.08, 0.23)

plt.show()


相关文章
|
3天前
|
机器学习/深度学习 自然语言处理 前端开发
深度学习-[源码+数据集]基于LSTM神经网络黄金价格预测实战
深度学习-[源码+数据集]基于LSTM神经网络黄金价格预测实战
|
3天前
|
机器学习/深度学习 自然语言处理 前端开发
深度学习-[数据集+完整代码]基于卷积神经网络的缺陷检测
深度学习-[数据集+完整代码]基于卷积神经网络的缺陷检测
|
3天前
|
机器学习/深度学习 人工智能 自然语言处理
探索深度学习的未来:从模型架构到应用场景
在信息技术飞速发展的时代,深度学习作为人工智能的核心领域正不断推动科技前沿。本文将探讨深度学习的最新发展趋势,包括模型架构的创新和实际应用场景的拓展。同时,我们将分析当前面临的挑战以及未来可能的发展方向,旨在为读者提供一个全面的视角,了解这一充满潜力的技术领域。
7 0
|
4天前
|
机器学习/深度学习 存储 算法
基于YOLOv8与ByteTrack的车辆行人多目标检测与追踪系统【python源码+Pyqt5界面+数据集+训练代码】深度学习实战、目标追踪、运动物体追踪
基于YOLOv8与ByteTrack的车辆行人多目标检测与追踪系统【python源码+Pyqt5界面+数据集+训练代码】深度学习实战、目标追踪、运动物体追踪
|
4天前
|
机器学习/深度学习 存储 计算机视觉
基于YOLOv8深度学习的PCB板缺陷检测系统【python源码+Pyqt5界面+数据集+训练代码】目标检测
基于YOLOv8深度学习的PCB板缺陷检测系统【python源码+Pyqt5界面+数据集+训练代码】目标检测
|
4天前
|
机器学习/深度学习 存储 安全
基于YOLOv8深度学习的行人跌倒检测系统【python源码+Pyqt5界面+数据集+训练代码】目标检测
基于YOLOv8深度学习的行人跌倒检测系统【python源码+Pyqt5界面+数据集+训练代码】目标检测
|
4天前
|
机器学习/深度学习 资源调度 PyTorch
【从零开始学习深度学习】15. Pytorch实战Kaggle比赛:房价预测案例【含数据集与源码】
【从零开始学习深度学习】15. Pytorch实战Kaggle比赛:房价预测案例【含数据集与源码】
|
4天前
|
机器学习/深度学习 存储 监控
基于YOLOv8深度学习的无人机视角高精度太阳能电池板检测与分析系统【python源码+Pyqt5界面+数据集+训练代码】深度学习实战、目标分割
基于YOLOv8深度学习的无人机视角高精度太阳能电池板检测与分析系统【python源码+Pyqt5界面+数据集+训练代码】深度学习实战、目标分割
|
4天前
|
机器学习/深度学习 存储 监控
基于YOLOv8深度学习的高压输电线绝缘子缺陷智能检测系统【python源码+Pyqt5界面+数据集+训练代码】深度学习实战、目标检测
基于YOLOv8深度学习的高压输电线绝缘子缺陷智能检测系统【python源码+Pyqt5界面+数据集+训练代码】深度学习实战、目标检测
|
4天前
|
机器学习/深度学习 算法 安全
基于YOLOv8深度学习的危险区域人员闯入检测与报警系统【python源码+Pyqt5界面+数据集+训练代码】YOLOv8、ByteTrack、目标追踪、区域闯入
基于YOLOv8深度学习的危险区域人员闯入检测与报警系统【python源码+Pyqt5界面+数据集+训练代码】YOLOv8、ByteTrack、目标追踪、区域闯入