深度学习 | Python下LeNet5的底层实现

前言

加油

一、导入MNIST手写数据集

数据读取参考:https://blog.csdn.net/hjimce/article/details/47323463#

import _pickle as cPickle
import gzip
import numpy as np
import matplotlib.pyplot as plt

with gzip.open('MNIST_data\\mnist.pkl.gz', 'rb') as f:
    train_set, valid_set, test_set = cPickle.load(f, encoding='bytes')
tx, ty = train_set
A = tx[5].reshape(28, 28)
Y = ty[5] # 真实数字

plt.imshow(A, cmap='gray')  # 显示手写字体图片
plt.show()
  1. 其中Python3无cPickle模块,已变成_pickle 详情
  2. 且读入时需加上 encoding='bytes'  详情

二、第一层:卷积层

def conv(input, filter, bias, step=1):
    """
    用于二维卷积的函数
    :param input: 输入图像
    :param filter: 卷积核 exp: [[卷积核1],[卷积核2],[卷积核3],[卷积核4]]
    :param bias:
    :param step: 步长
    :return: 返回特征图序列
    """
    shape_input = np.shape(input)
    shape_filter = np.shape(filter)
    conv_row = shape_input[0] - shape_filter[1] + 1  # 横向移动次数
    conv_col = shape_input[1] - shape_filter[2] + 1  # 纵向移动次数

    last_sum = []
    for num in range(0, shape_filter[0]):
        cov_sum = []
        for col in range(0, conv_col, step):
            sum = []
            for row in range(0, conv_row, step):
                cov = 0.0
                for i in range(0, shape_filter[1]):
                    for j in range(0, shape_filter[2]):
                        cov += (input[int(row + i)][int(col + j)] * filter[num][i][j]) / (
                                shape_filter[1] * shape_filter[2])
                sum.append(sigmoid(cov))
            cov_sum.append(sum)
        last_sum.append(cov_sum)
    return np.array(last_sum)

通过多次循环,实现多卷积核的二维卷积操作

你可能感兴趣的:(信息基础)