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


樱桃笨笨
樱桃笨笨 2022-09-20 11:18:39 52199
分类专栏: 资讯

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

 

 

目录

输出结果

实现代码


 

 

输出结果

 

实现代码

  1. from __future__ import print_function
  2. print(__doc__)
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. from scipy.ndimage import convolve
  6. from sklearn import linear_model, datasets, metrics
  7. from sklearn.cross_validation import train_test_split
  8. from sklearn.neural_network import BernoulliRBM
  9. from sklearn.pipeline import Pipeline
  10. def nudge_dataset(X, Y):
  11. direction_vectors = [
  12. [[0, 1, 0],[0, 0, 0],[0, 0, 0]],
  13. [[0, 0, 0],[1, 0, 0],[0, 0, 0]],
  14. [[0, 0, 0],[0, 0, 1],[0, 0, 0]],
  15. [[0, 0, 0],[0, 0, 0],[0, 1, 0]]
  16. ]
  17. shift = lambda x, w: convolve(x.reshape((8, 8)), mode='constant',weights=w).ravel()
  18. X = np.concatenate([X] +
  19. [np.apply_along_axis(shift, 1, X, vector)
  20. for vector in direction_vectors])
  21. Y = np.concatenate([Y for _ in range(5)], axis=0)
  22. return X, Y
  23. digits = datasets.load_digits()
  24. X = np.asarray(digits.data, 'float32')
  25. X, Y = nudge_dataset(X, digits.target)
  26. X = (X - np.min(X, 0)) / (np.max(X, 0) + 0.0001)
  27. X_train, X_test, Y_train, Y_test = train_test_split(X, Y,test_size=0.2,random_state=0)
  28. logistic = linear_model.LogisticRegression()
  29. rbm = BernoulliRBM(random_state=0, verbose=True)
  30. classifier = Pipeline(steps=[('rbm', rbm), ('logistic', logistic)])
  31. rbm.learning_rate = 0.06
  32. rbm.n_iter = 20
  33. More components tend to give better prediction performance, but larger fitting time
  34. rbm.n_components = 100
  35. logistic.C = 6000.0
  36. classifier.fit(X_train, Y_train)
  37. logistic_classifier = linear_model.LogisticRegression(C=100.0)
  38. logistic_classifier.fit(X_train, Y_train)
  39. print()
  40. print("Logistic regression using RBM features:\n%s\n" % (
  41. metrics.classification_report(
  42. Y_test,classifier.predict(X_test)
  43. )
  44. ))
  45. print("Logistic regression using raw pixel features:\n%s\n" % (
  46. metrics.classification_report(
  47. Y_test,
  48. logistic_classifier.predict(X_test))))
  49. plt.figure(figsize=(4.2, 4))
  50. for i, comp in enumerate(rbm.components_):
  51. plt.subplot(10, 10, i + 1)
  52. plt.imshow(comp.reshape((8, 8)), cmap=plt.cm.gray_r,
  53. interpolation='nearest')
  54. plt.xticks(())
  55. plt.yticks(())
  56. plt.suptitle('100 components extracted by RBM', fontsize=16)
  57. plt.subplots_adjust(0.08, 0.02, 0.92, 0.85, 0.08, 0.23)
  58. plt.show()

 

 


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

网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。

本文链接:https://www.xckfsq.com/news/show.html?id=4261
赞同 0
评论 0 条
樱桃笨笨L0
粉丝 0 发表 8 + 关注 私信
上周热门
如何使用 StarRocks 管理和优化数据湖中的数据?  2941
【软件正版化】软件正版化工作要点  2860
统信UOS试玩黑神话:悟空  2819
信刻光盘安全隔离与信息交换系统  2712
镜舟科技与中启乘数科技达成战略合作,共筑数据服务新生态  1246
grub引导程序无法找到指定设备和分区  1213
华为全联接大会2024丨软通动力分论坛精彩议程抢先看!  163
点击报名 | 京东2025校招进校行程预告  162
2024海洋能源产业融合发展论坛暨博览会同期活动-海洋能源与数字化智能化论坛成功举办  160
华为纯血鸿蒙正式版9月底见!但Mate 70的内情还得接着挖...  157
本周热议
我的信创开放社区兼职赚钱历程 40
今天你签到了吗? 27
信创开放社区邀请他人注册的具体步骤如下 15
如何玩转信创开放社区—从小白进阶到专家 15
方德桌面操作系统 14
我有15积分有什么用? 13
用抖音玩法闯信创开放社区——用平台宣传企业产品服务 13
如何让你先人一步获得悬赏问题信息?(创作者必看) 12
2024中国信创产业发展大会暨中国信息科技创新与应用博览会 9
中央国家机关政府采购中心:应当将CPU、操作系统符合安全可靠测评要求纳入采购需求 8

加入交流群

请使用微信扫一扫!