大数据与云计算 | 掌握深度学习框架的应用

2022-08-09,,,,

一、实验目的

  1. 熟悉Python和numpy的基本用法
  2. 掌握深度学习框架的应用

 

二、实验内容

  1. 在本机安装Anaconda或者纯Python环境,了解Python基本语法和numpy基本用法
  2. 使用任意深度学习框架(TensorFlow,Keras等)实现MNIST手写数字识别*,正确率至少在90%以上
  3. *实现不用任何深度学习框架,仅使用numpy等基础库实现MNIST手写数字识别,正确率至少在85%以上

 

三、实验过程

3.1 安装Anaconda环境和PyCharm

成功配置的环境如下图所示

设置Pycharm的默认解释器为Anaconda

3.2 配置TensorFlow

3.2.1 Anaconda Prompt -> 创建一个python3.5的环境,环境名称为tensorflow,然后启动tensorflow环境:activate tensorflow

3.2.2安装cpu版本的TensorFlow:pip install --upgrade --ignore-installed tensorflow

3.2.3 安装Keras:pip install keras

3.3 加载keras的mnist数据

  1. # Plot ad hoc mnist instances  
  2. from keras.datasets import mnist  
  3. import matplotlib.pyplot as plt  
  4. # load (downloaded if needed) the MNIST dataset  
  5. (X_train, y_train), (X_test, y_test) = mnist.load_data()  
  6. # plot 4 images as gray scale  
  7. plt.subplot(221)  
  8. plt.imshow(X_train[0], cmap=plt.get_cmap('gray'))  
  9. plt.subplot(222)  
  10. plt.imshow(X_train[1], cmap=plt.get_cmap('gray'))  
  11. plt.subplot(223)  
  12. plt.imshow(X_train[2], cmap=plt.get_cmap('gray'))  
  13. plt.subplot(224)  
  14. plt.imshow(X_train[3], cmap=plt.get_cmap('gray'))  
  15. # show the plot  
  16. plt.show()  

上面的代码加载了数据集并画出了前4个图片:

编写代码:

  1. from keras.datasets import mnist  
  2. import matplotlib.pyplot as plt  
  3. (X_train, y_train), (X_test, y_test) = mnist.load_data()  
  4. plt.subplot(331)  
  5. plt.imshow(X_test[0], cmap=plt.get_cmap('gray'))  
  6. plt.subplot(332)  
  7. plt.imshow(X_train[1], cmap=plt.get_cmap('gray'))  
  8. plt.subplot(333)  
  9. plt.imshow(X_train[2], cmap=plt.get_cmap('gray'))  
  10. plt.subplot(334)  
  11. plt.imshow(X_train[3], cmap=plt.get_cmap('gray'))  
  12. plt.subplot(335)  
  13. plt.imshow(X_train[4], cmap=plt.get_cmap('gray'))  
  14. plt.subplot(336)  
  15. plt.imshow(X_train[5], cmap=plt.get_cmap('gray'))  
  16. plt.subplot(337)  
  17. plt.imshow(X_train[6], cmap=plt.get_cmap('gray'))  
  18. plt.subplot(338)  
  19. plt.imshow(X_train[7], cmap=plt.get_cmap('gray'))  
  20. plt.subplot(339)  
  21. plt.imshow(X_train[8], cmap=plt.get_cmap('gray'))  
  22. plt.show()  

运行结果:

3.4 编写多层感知机的baseline模型

【代码略去】

训练完成,模型的错误率为1.97%

 

3.5 实现卷积神经网络

【代码略去】

3.5.6 训练完成,模型的错误率为0.88%,相比于单隐层神经网络有很大提升

3.6 仅使用numpy实现MNIST手写数字识别(选做)

【代码略去】

训练结果:误差14.66%,在可接受范围内

 

四、实验结果

  1. 使用单隐层神经网络实现了MNIST手写数字识别,错误率1.97%
  2. 使用卷积神经网络实现了MNIST手写数字识别,错误率0.88%
  3. 仅使用numpy实现了MNIST手写数字识别,错误率14.66%

 

五、体会

通过本次实验,我对Python深度学习和numpy有了更深刻的理解,掌握了单隐层神经网络、卷积神经网络、numpy实现MNIST手写数字识别模型的构建与训练,对大数据与云计算的功能与原理有了进一步的了解。

本文地址:https://blog.csdn.net/lee1hong/article/details/107137210

《大数据与云计算 | 掌握深度学习框架的应用.doc》

下载本文的Word格式文档,以方便收藏与打印。