深度学习笔记-第3章-《深度学习入门——基于Python的理论与实现》的代码解说

这个书各位大佬都很熟悉,我就不多说了。这个系列的文章肯定不是很长,整个整合到
机器学习-笔记目录

版面尽量干净,分成三部分,代码图,重要的语法,举例扩展。

1.代码图

  • show_mnist.py
    这个文件是结果展示文件。
    深度学习笔记-第3章-《深度学习入门——基于Python的理论与实现》的代码解说

  • 1.1 neuralnet_mnist.py(待更)

.
.
.
.
.
.
.
.
.

2.涉及的语法

这里我截取主要的部分,最后总结一下。

This module provides a simple interface to compress and decompress files just like the GNU programs gzip and gunzip would.
The data compression is provided by the zlib module.

gzip.open(filename, mode='rb', compresslevel=9, encoding=None, errors=None, newline=None)

Open a gzip-compressed file in binary or text mode, returning a file object.
The filename argument can be an actual filename (a str or bytes object), or an existing file object to read from or write to.
The mode argument can be any of ‘r’, ‘rb’, ‘a’, ‘ab’, ‘w’, ‘wb’, ‘x’ or ‘xb’ for binary mode, or ‘rt’, ‘at’, ‘wt’, or ‘xt’ for text mode. The default is ‘rb’.
The compresslevel argument is an integer from 0 to 9, as for the GzipFile constructor.
For binary mode, this function is equivalent to the GzipFile constructor: GzipFile(filename, mode, compresslevel). In this >case, the encoding, errors and newline arguments must not be provided.

这个主要是用来解压几个.gz文件的,默认以二进制打开,代码中是gzip.open(file_path, 'rb')打开了四个.gz文件
需要注意的是,gzip打开文件,也可以接受文件对象。并且打开了文件之后返回的也是文件对象,不过我更喜欢说是文件操作符。举例在最后面
下一步的处理是data = np.frombuffer(f.read(), np.uint8, offset=16)放在下面讲

详细见上文的原文部分。以下是它的一些其它功能

gzip.compress(data, compresslevel=9)

Compress the data, returning a bytes object containing the compressed data. compresslevel has the same meaning as in the GzipFile constructor above.

gzip.decompress(data)

Decompress the data, returning a bytes object containing the uncompressed data.

gzip的源代码:https://github.com/python/cpython/blob/3.6/Lib/gzip.py

  • 2.np.frombuffer

https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.frombuffer.html

Interpret a buffer as a 1-dimensional array.

.
.
.
.
.

3. 举例

小例子做扩展,可完全跳过。

结尾
在我的图上,加了很多代码的注释,作为图层分开放了,为了看起来简洁明了我将图层隐去了。不隐藏的时候,代码图是这样的,需要的请自行下载dwg,不下载也不影响,该说的都说完了。

红色是变量监控,橘色是注释,紫色是详细注释和超链接,ctrl一下就能飞过去。
这是我能截取的最大的范围了,缩小字会看不清,所以上面的图我把注释去掉了。
深度学习笔记-第3章-《深度学习入门——基于Python的理论与实现》的代码解说

更新时间
2018.10.28——更新show_mnist的代码图和几处重要的语法。