HW0

Date (online)
Instructor
Slides
Video
收获:
  • 熟悉了课程作业的体系,包括评分体系等。每个作业都围绕一个.ipynb 文件展开,只需要按照该文件 执行相应的步骤、编写对应的函数即可。作业可以在 google colab 完成也可以在本地完成。我选择了在本地完成。
  • 课程用mugrade来给作业评分,可以先在github下载下来,然后本地安装:
    • # 记得先转到任意指定下载目录,然后安装的时候,如果是用conda管理工具,那么会安装到当前环境下 git clone https://github.com/dlsyscourse/mugrade.git cd mugrade python3 setup.py install
  • 通过 Python struct 库来读取二进制文件,实现对 mnist 数据集的读取。仔细阅读 mnist 数据集 官方网站的数据格式,然后使用 struct来读取数据即可。这是本人第一次接触二进制文件。
  • 使用 Python 的numpy 库实现了 softmax 回归 和两层神经网络模型。认真听课以及课后自己推导一遍的话,实现起来还是相当容易的。
  • 使用 C++ 实现了softmax 回归。对c++矩阵运算处理的不够好,如果单独实现一个class,则感觉过于繁琐,所以使用了函数来实现各个功能。最后的速度相较 python 版本快了10倍多。

Loading MNIST data

def parse_mnist(image_filename, label_filename): """ Read an images and labels file in MNIST format. See this page: http://yann.lecun.com/exdb/mnist/ for a description of the file format. Args: image_filename (str): name of gzipped images file in MNIST format label_filename (str): name of gzipped labels file in MNIST format Returns: Tuple (X,y): X (numpy.ndarray[np.float32]): 2D numpy array containing the loaded data. The dimensionality of the data should be (num_examples x input_dim) where 'input_dim' is the full dimension of the data, e.g., since MNIST images are 28x28, it will be 784. Values should be of type np.float32, and the data should be normalized to have a minimum value of 0.0 and a maximum value of 1.0. The normalization should be applied uniformly across the whole dataset, _not_ individual images. y (numpy.ndarray[dtype=np.uint8]): 1D numpy array containing the labels of the examples. Values should be of type np.uint8 and for MNIST will contain the values 0-9. """ with gzip.open(image_filename, "rb") as img_file: magic_num, img_num, rows ,cols = struct.unpack('>IIII', img_file.read(16)) assert magic_num == 2051 total_pixels = rows * cols images = np.vstack([np.array(object=struct.unpack(f">{total_pixels}B", img_file.read(total_pixels)), dtype=np.float32) for _ in range(img_num)]) # 归一化 images = (images - images.min()) / (images.max() - images.min()) assert images.shape == (img_num, total_pixels) with gzip.open(label_filename, "rb") as lab_file: magic_num, lab_num = struct.unpack(">II", lab_file.read(8)) assert magic_num == 2049 labels = np.array(object=struct.unpack(f">{lab_num}B", lab_file.read(lab_num)), dtype=np.uint8) assert labels.shape == (lab_num,) return images, labels
数据集官网认真看一下数据存储格式,然后一字节一字节的读取即可。
struct.unpack() 的第一个参数 是指定了把读取的数据解析为什么格式,第二个参数指定了读取的字节数量。然后把读取的数据转为numpy对象。

Training MNIST with softmax regression

本节需要对softmax 认真推导后方可较好的实现,基本没有难度。

Softmax regression in C++

离开了numpy,不管是矩阵乘法、减法,还是softmax归一化都需要使用for循环来实现。不过也基本没难度。
只有是softmax的时候,需要考虑清楚。