代码来源:https://github.com/eriklindernoren/ML-From-Scratch

卷积神经网络中卷积层Conv2D(带stride、padding)的具体实现:https://www.cnblogs.com/xiximayou/p/12706576.html

激活函数的实现(sigmoid、softmax、tanh、relu、leakyrelu、elu、selu、softplus):https://www.cnblogs.com/xiximayou/p/12713081.html

损失函数定义(均方误差、交叉熵损失):https://www.cnblogs.com/xiximayou/p/12713198.html

优化器的实现(SGD、Nesterov、Adagrad、Adadelta、RMSprop、Adam):https://www.cnblogs.com/xiximayou/p/12713594.html

卷积层反向传播过程:https://www.cnblogs.com/xiximayou/p/12713930.html

全连接层实现:https://www.cnblogs.com/xiximayou/p/12720017.html

批量归一化层实现:https://www.cnblogs.com/xiximayou/p/12720211.html

池化层实现:https://www.cnblogs.com/xiximayou/p/12720324.html

padding2D实现:https://www.cnblogs.com/xiximayou/p/12720454.html

Flatten层实现:https://www.cnblogs.com/xiximayou/p/12720518.html

上采样层UpSampling2D实现:https://www.cnblogs.com/xiximayou/p/12720558.html

Dropout层实现:https://www.cnblogs.com/xiximayou/p/12720589.html

激活层实现:https://www.cnblogs.com/xiximayou/p/12720622.html

定义训练和测试过程:https://www.cnblogs.com/xiximayou/p/12725873.html

代码在mlfromscratch/examples/convolutional_neural_network.py 中:

  1. from __future__ import print_function
  2. from sklearn import datasets
  3. import matplotlib.pyplot as plt
  4. import math
  5. import numpy as np
  6.  
  7. # Import helper functions
  8. from mlfromscratch.deep_learning import NeuralNetwork
  9. from mlfromscratch.utils import train_test_split, to_categorical, normalize
  10. from mlfromscratch.utils import get_random_subsets, shuffle_data, Plot
  11. from mlfromscratch.utils.data_operation import accuracy_score
  12. from mlfromscratch.deep_learning.optimizers import StochasticGradientDescent, Adam, RMSprop, Adagrad, Adadelta
  13. from mlfromscratch.deep_learning.loss_functions import CrossEntropy
  14. from mlfromscratch.utils.misc import bar_widgets
  15. from mlfromscratch.deep_learning.layers import Dense, Dropout, Conv2D, Flatten, Activation, MaxPooling2D
  16. from mlfromscratch.deep_learning.layers import AveragePooling2D, ZeroPadding2D, BatchNormalization, RNN
  17.  
  18. def main():
  19.  
  20. #----------
  21. # Conv Net
  22. #----------
  23.  
  24. optimizer = Adam()
  25.  
  26. data = datasets.load_digits()
  27. X = data.data
  28. y = data.target
  29.  
  30. # Convert to one-hot encoding
  31. y = to_categorical(y.astype("int"))
  32.  
  33. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, seed=1)
  34.  
  35. # Reshape X to (n_samples, channels, height, width)
  36. X_train = X_train.reshape((-1,1,8,8))
  37. X_test = X_test.reshape((-1,1,8,8))
  38.  
  39. clf = NeuralNetwork(optimizer=optimizer,
  40. loss=CrossEntropy,
  41. validation_data=(X_test, y_test))
  42.  
  43. clf.add(Conv2D(n_filters=16, filter_shape=(3,3), stride=1, input_shape=(1,8,8), padding='same'))
  44. clf.add(Activation('relu'))
  45. clf.add(Dropout(0.25))
  46. clf.add(BatchNormalization())
  47. clf.add(Conv2D(n_filters=32, filter_shape=(3,3), stride=1, padding='same'))
  48. clf.add(Activation('relu'))
  49. clf.add(Dropout(0.25))
  50. clf.add(BatchNormalization())
  51. clf.add(Flatten())
  52. clf.add(Dense(256))
  53. clf.add(Activation('relu'))
  54. clf.add(Dropout(0.4))
  55. clf.add(BatchNormalization())
  56. clf.add(Dense(10))
  57. clf.add(Activation('softmax'))
  58.  
  59. print ()
  60. clf.summary(name="ConvNet")
  61.  
  62. train_err, val_err = clf.fit(X_train, y_train, n_epochs=50, batch_size=256)
  63.  
  64. # Training and validation error plot
  65. n = len(train_err)
  66. training, = plt.plot(range(n), train_err, label="Training Error")
  67. validation, = plt.plot(range(n), val_err, label="Validation Error")
  68. plt.legend(handles=[training, validation])
  69. plt.title("Error Plot")
  70. plt.ylabel('Error')
  71. plt.xlabel('Iterations')
  72. plt.show()
  73.  
  74. _, accuracy = clf.test_on_batch(X_test, y_test)
  75. print ("Accuracy:", accuracy)
  76.  
  77. y_pred = np.argmax(clf.predict(X_test), axis=1)
  78. X_test = X_test.reshape(-1, 8*8)
  79. # Reduce dimension to 2D using PCA and plot the results
  80. Plot().plot_in_2d(X_test, y_pred, title="Convolutional Neural Network", accuracy=accuracy, legend_labels=range(10))
  81.  
  82. if __name__ == "__main__":
  83. main()

我们还是一步步进行分析:

1、优化器使用Adam()

2、数据集使用的是sklearn.datasets中的手写数字,其部分数据如下:

  1. (1797, 64)
  2. (1797,)
  3. [[ 0. 0. 5. 13. 9. 1. 0. 0. 0. 0. 13. 15. 10. 15. 5. 0. 0. 3.
  4. 15. 2. 0. 11. 8. 0. 0. 4. 12. 0. 0. 8. 8. 0. 0. 5. 8. 0.
  5. 0. 9. 8. 0. 0. 4. 11. 0. 1. 12. 7. 0. 0. 2. 14. 5. 10. 12.
  6. 0. 0. 0. 0. 6. 13. 10. 0. 0. 0.]
  7. [ 0. 0. 0. 12. 13. 5. 0. 0. 0. 0. 0. 11. 16. 9. 0. 0. 0. 0.
  8. 3. 15. 16. 6. 0. 0. 0. 7. 15. 16. 16. 2. 0. 0. 0. 0. 1. 16.
  9. 16. 3. 0. 0. 0. 0. 1. 16. 16. 6. 0. 0. 0. 0. 1. 16. 16. 6.
  10. 0. 0. 0. 0. 0. 11. 16. 10. 0. 0.]
  11. [ 0. 0. 0. 4. 15. 12. 0. 0. 0. 0. 3. 16. 15. 14. 0. 0. 0. 0.
  12. 8. 13. 8. 16. 0. 0. 0. 0. 1. 6. 15. 11. 0. 0. 0. 1. 8. 13.
  13. 15. 1. 0. 0. 0. 9. 16. 16. 5. 0. 0. 0. 0. 3. 13. 16. 16. 11.
  14. 5. 0. 0. 0. 0. 3. 11. 16. 9. 0.]
  15. [ 0. 0. 7. 15. 13. 1. 0. 0. 0. 8. 13. 6. 15. 4. 0. 0. 0. 2.
  16. 1. 13. 13. 0. 0. 0. 0. 0. 2. 15. 11. 1. 0. 0. 0. 0. 0. 1.
  17. 12. 12. 1. 0. 0. 0. 0. 0. 1. 10. 8. 0. 0. 0. 8. 4. 5. 14.
  18. 9. 0. 0. 0. 7. 13. 13. 9. 0. 0.]
  19. [ 0. 0. 0. 1. 11. 0. 0. 0. 0. 0. 0. 7. 8. 0. 0. 0. 0. 0.
  20. 1. 13. 6. 2. 2. 0. 0. 0. 7. 15. 0. 9. 8. 0. 0. 5. 16. 10.
  21. 0. 16. 6. 0. 0. 4. 15. 16. 13. 16. 1. 0. 0. 0. 0. 3. 15. 10.
  22. 0. 0. 0. 0. 0. 2. 16. 4. 0. 0.]
  23. [ 0. 0. 12. 10. 0. 0. 0. 0. 0. 0. 14. 16. 16. 14. 0. 0. 0. 0.
  24. 13. 16. 15. 10. 1. 0. 0. 0. 11. 16. 16. 7. 0. 0. 0. 0. 0. 4.
  25. 7. 16. 7. 0. 0. 0. 0. 0. 4. 16. 9. 0. 0. 0. 5. 4. 12. 16.
  26. 4. 0. 0. 0. 9. 16. 16. 10. 0. 0.]
  27. [ 0. 0. 0. 12. 13. 0. 0. 0. 0. 0. 5. 16. 8. 0. 0. 0. 0. 0.
  28. 13. 16. 3. 0. 0. 0. 0. 0. 14. 13. 0. 0. 0. 0. 0. 0. 15. 12.
  29. 7. 2. 0. 0. 0. 0. 13. 16. 13. 16. 3. 0. 0. 0. 7. 16. 11. 15.
  30. 8. 0. 0. 0. 1. 9. 15. 11. 3. 0.]
  31. [ 0. 0. 7. 8. 13. 16. 15. 1. 0. 0. 7. 7. 4. 11. 12. 0. 0. 0.
  32. 0. 0. 8. 13. 1. 0. 0. 4. 8. 8. 15. 15. 6. 0. 0. 2. 11. 15.
  33. 15. 4. 0. 0. 0. 0. 0. 16. 5. 0. 0. 0. 0. 0. 9. 15. 1. 0.
  34. 0. 0. 0. 0. 13. 5. 0. 0. 0. 0.]
  35. [ 0. 0. 9. 14. 8. 1. 0. 0. 0. 0. 12. 14. 14. 12. 0. 0. 0. 0.
  36. 9. 10. 0. 15. 4. 0. 0. 0. 3. 16. 12. 14. 2. 0. 0. 0. 4. 16.
  37. 16. 2. 0. 0. 0. 3. 16. 8. 10. 13. 2. 0. 0. 1. 15. 1. 3. 16.
  38. 8. 0. 0. 0. 11. 16. 15. 11. 1. 0.]
  39. [ 0. 0. 11. 12. 0. 0. 0. 0. 0. 2. 16. 16. 16. 13. 0. 0. 0. 3.
  40. 16. 12. 10. 14. 0. 0. 0. 1. 16. 1. 12. 15. 0. 0. 0. 0. 13. 16.
  41. 9. 15. 2. 0. 0. 0. 0. 3. 0. 9. 11. 0. 0. 0. 0. 0. 9. 15.
  42. 4. 0. 0. 0. 9. 12. 13. 3. 0. 0.]]
  43. [0 1 2 3 4 5 6 7 8 9]

3、接着有一个to_categorical()函数,在mlfromscratch.utils下的data_manipulation.py中:

  1. def to_categorical(x, n_col=None):
  2. """ One-hot encoding of nominal values """
  3. if not n_col:
  4. n_col = np.amax(x) + 1
  5. one_hot = np.zeros((x.shape[0], n_col))
  6. one_hot[np.arange(x.shape[0]), x] = 1
  7. return one_hot

用于将标签转换为one-hot编码。

4、划分训练集和测试集:train_test_split(),在mlfromscratch.utils下的data_manipulation.py中:

  1. def train_test_split(X, y, test_size=0.5, shuffle=True, seed=None):
  2. """ Split the data into train and test sets """
  3. if shuffle:
  4. X, y = shuffle_data(X, y, seed)
  5. # Split the training data from test data in the ratio specified in
  6. # test_size
  7. split_i = len(y) - int(len(y) // (1 / test_size))
  8. X_train, X_test = X[:split_i], X[split_i:]
  9. y_train, y_test = y[:split_i], y[split_i:]
  10.  
  11. return X_train, X_test, y_train, y_test

5、由于卷积神经网络的输入是[batchsize,channel,wheight,width]的维度,因此要将原始数据进行转换,即将(1797,64)转换为(1797,1,8,8)格式的数据。这里batchsize就是样本的数量。

6、定义卷积神经网络的训练和测试过程:包括优化器、损失函数、测试数据

7、定义模型结构

8、输出模型每层的类型、参数数量以及输出大小

9、将数据输入到模型中,设置epochs的大小以及batch_size的大小

10、计算训练和测试的错误,并绘制成图

11、计算准确率

12、绘制测试集中每一类预测的结果,这里有一个plot_in_2d()函数,位于mlfromscratch.utils下的misc.py中

  1. # Plot the dataset X and the corresponding labels y in 2D using PCA.
  2. def plot_in_2d(self, X, y=None, title=None, accuracy=None, legend_labels=None):
  3. X_transformed = self._transform(X, dim=2)
  4. x1 = X_transformed[:, 0]
  5. x2 = X_transformed[:, 1]
  6. class_distr = []
  7.  
  8. y = np.array(y).astype(int)
  9.  
  10. colors = [self.cmap(i) for i in np.linspace(0, 1, len(np.unique(y)))]
  11.  
  12. # Plot the different class distributions
  13. for i, l in enumerate(np.unique(y)):
  14. _x1 = x1[y == l]
  15. _x2 = x2[y == l]
  16. _y = y[y == l]
  17. class_distr.append(plt.scatter(_x1, _x2, color=colors[i]))
  18.  
  19. # Plot legend
  20. if not legend_labels is None:
  21. plt.legend(class_distr, legend_labels, loc=1)
  22.  
  23. # Plot title
  24. if title:
  25. if accuracy:
  26. perc = 100 * accuracy
  27. plt.suptitle(title)
  28. plt.title("Accuracy: %.1f%%" % perc, fontsize=10)
  29. else:
  30. plt.title(title)
  31.  
  32. # Axis labels
  33. plt.xlabel('Principal Component 1')
  34. plt.ylabel('Principal Component 2')
  35.  
  36. plt.show()

接下来就可以实际进行操作了,我是在谷歌colab中,首先使用:

  1. !git clone https://github.com/eriklindernoren/ML-From-Scratch.git

将相关代码复制下来。

然后进行安装:在ML-From-Scratch目录下输入:

  1. !python setup.py install

最后输入:

  1. !python mlfromscratch/examples/convolutional_neural_network.py

最终结果:

  1. +---------+
  2. | ConvNet |
  3. +---------+
  4. Input Shape: (1, 8, 8)
  5. +----------------------+------------+--------------+
  6. | Layer Type | Parameters | Output Shape |
  7. +----------------------+------------+--------------+
  8. | Conv2D | 160 | (16, 8, 8) |
  9. | Activation (ReLU) | 0 | (16, 8, 8) |
  10. | Dropout | 0 | (16, 8, 8) |
  11. | BatchNormalization | 2048 | (16, 8, 8) |
  12. | Conv2D | 4640 | (32, 8, 8) |
  13. | Activation (ReLU) | 0 | (32, 8, 8) |
  14. | Dropout | 0 | (32, 8, 8) |
  15. | BatchNormalization | 4096 | (32, 8, 8) |
  16. | Flatten | 0 | (2048,) |
  17. | Dense | 524544 | (256,) |
  18. | Activation (ReLU) | 0 | (256,) |
  19. | Dropout | 0 | (256,) |
  20. | BatchNormalization | 512 | (256,) |
  21. | Dense | 2570 | (10,) |
  22. | Activation (Softmax) | 0 | (10,) |
  23. +----------------------+------------+--------------+
  24. Total Parameters: 538570
  25.  
  26. Training: 100% [------------------------------------------------] Time: 0:01:32
  27. <Figure size 640x480 with 1 Axes>
  28. Accuracy: 0.9846796657381616
  29. <Figure size 640x480 with 1 Axes>

至此,结合代码一步一步看卷积神经网络的整个实现过程就完成了。通过结合代码的形式,可以加深对深度学习中卷积神经网络相关知识的理解。

【python实现卷积神经网络】开始训练的更多相关文章

  1. 基于Python的卷积神经网络和特征提取

    基于Python的卷积神经网络和特征提取 用户1737318发表于人工智能头条订阅 224 在这篇文章中: Lasagne 和 nolearn 加载MNIST数据集 ConvNet体系结构与训练 预测 ...

  2. 《TensorFlow实战》中AlexNet卷积神经网络的训练中

    TensorFlow实战中AlexNet卷积神经网络的训练 01 出错 TypeError: as_default() missing 1 required positional argument: ...

  3. python机器学习卷积神经网络(CNN)

    卷积神经网络(CNN) 关注公众号"轻松学编程"了解更多. 一.简介 ​ 卷积神经网络(Convolutional Neural Network,CNN)是一种前馈神经网络,它的人 ...

  4. 【python实现卷积神经网络】定义训练和测试过程

    代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...

  5. Python CNN卷积神经网络代码实现

    # -*- coding: utf-8 -*- """ Created on Wed Nov 21 17:32:28 2018 @author: zhen "& ...

  6. 使用卷积神经网络CNN训练识别mnist

    算的的上是自己搭建的第一个卷积神经网络.网络结构比较简单. 输入为单通道的mnist数据集.它是一张28*28,包含784个特征值的图片 我们第一层输入,使用5*5的卷积核进行卷积,输出32张特征图, ...

  7. 【python实现卷积神经网络】卷积层Conv2D反向传播过程

    代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...

  8. 【python实现卷积神经网络】激活函数的实现(sigmoid、softmax、tanh、relu、leakyrelu、elu、selu、softplus)

    代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...

  9. 【python实现卷积神经网络】损失函数的定义(均方误差损失、交叉熵损失)

    代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...

随机推荐

  1. 欲善事先利器-IEAD插件篇

    工欲善其事,必先利其器,好鞋踢好球是非常合乎逻辑的事情. --<长江七号> 同样的开场白,不一样的酒,不一样的故事. 上篇<欲善事先利器--系统篇>已经推荐了一些个人常用的效率 ...

  2. connection closed by foreign host / Permissions 0620 for '/etc/ssh/ssh_host_ed25519_key' are too open 解决方案

    发生此次故障的原因: 在文件夹授权时 错误的执行了 chmod -R 755 / 本来只想授权当前文件夹的 结果... 然后就导致xshell连不上了 懵逼... 解决方案 将权限收回: 执行: ch ...

  3. linux redis安装 5.0.2

    参看:https://www.cnblogs.com/limit1/p/9045183.html 1.获取redis资源 wget http://download.redis.io/releases/ ...

  4. vscode不能打开浏览器(Open browser failed!! Please check if you have installed the browser correctly!)

    vscode出现上述问题,我也查了很多相关资料,什么改默认浏览器设置什么的,改配置,改系统环境变量什么的,不但麻烦而且最后都难以成功. 下面分享一个可以解决的最简单办法.那就是:舍弃open in b ...

  5. hdu1541树状数组(降维打击)

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1541/ 题意是:在二维图上有一系列坐标,其中坐标给出的顺序是:按照y升序排序,如果y值相同则按照x升序排序.这个 ...

  6. Flutter 强大的MediaQuery控件

    注意:无特殊说明,Flutter版本及Dart版本如下: Flutter版本: 1.12.13+hotfix.5 Dart版本: 2.7.0 MediaQuery 通常情况下,不会直接将MediaQu ...

  7. 学习java应该具备哪些以及怎么学习java

    JAVA为什么有前途?过去的十多年,JAVA基本每年都是全世界使用人数第一的语言.全世界数百万的IT企业构建了庞大的JAVA生态圈,大量的软件基于JAVA开发. JAVA也被誉为“计算机界的英语”. ...

  8. Dropout的前世与今生

    Dropout 是一类用于神经网络训练或推理的随机化技术,这类技术已经引起了研究者们的广泛兴趣,并且被广泛地应用于神经网络正则化.模型压缩等任务.虽然 Dropout 最初是为密集的神经网络层量身定制 ...

  9. 局部变量表中Slot复用对垃圾回收的影响详解

    看两段代码 1. package com.jvm; public class Test { public static void main(String[] args) { { byte[] plac ...

  10. Codeforces 1332F - Independent Set(树dp)

    题目链接 题意 给出一棵 n 个点的树, 求它的所有非空诱导子图的独立集种类数之和, 对 998244353 取模. n ≤ 3e5. 题解 不妨假设在独立集中的点被染色成 1, 其余不染色; 由于不 ...