首页
学习
活动
专区
工具
TVP
发布

使用Python解析MNIST数据集

五月两场 | NVIDIA DLI 深度学习入门课程

5月19日/5月26日

一天密集式学习 快速带你入门

正文共948个字(不含代码),2张图,预计阅读时间15分钟。

前言

最近在学习Keras,要使用到LeCun大神的MNIST手写数字数据集,直接从官网上下载了4个压缩包:

MNIST数据集

解压后发现里面每个压缩包里有一个idx-ubyte文件,没有图片文件在里面。回去仔细看了一下官网后发现原来这是IDX文件格式,是一种用来存储向量与多维度矩阵的文件格式。

IDX文件格式

官网上的介绍如下:

THE IDX FILE FORMAT

the IDX file format is a simple format for vectors and multidimensional matrices of various numerical types.

The basic format is

The magic number is an integer (MSB first). The first 2 bytes are always 0.

The third byte codes the type of the data:

The 4-th byte codes the number of dimensions of the vector/matrix: 1 for vectors, 2 for matrices....

The sizes in each dimension are 4-byte integers (MSB first, high endian, like in most non-Intel processors).

The data is stored like in a C array, i.e. the index in the last dimension changes the fastest.

解析脚本

根据以上解析规则,我使用了Python里的struct模块对文件进行读写(如果不熟悉struct模块的可以看我的另一篇博客文章《Python中对字节流/二进制流的操作:struct模块简易使用教程》)。IDX文件的解析通用接口如下:

针对MNIST数据集的解析脚本如下:

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20180517A1D2S400?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券