深度學習筆記-第3章-《深度學習入門——基於Python的理論與實現》的代碼解說

這個書各位大佬都很熟悉,我就不多說了。這個系列的文章肯定不是很長,整個整合到
機器學習-筆記目錄

方便的Numpy查詢手冊
https://docs.scipy.org/doc/numpy/search.html

版面儘量乾淨,分成三部分,代碼圖,重要的語法,舉例擴展。

1.代碼圖

  • show_mnist.py
    這個文件是結果展示文件,我把我的標號的備註都去除掉了。
    在這裏插入圖片描述

  • 1.1 neuralnet_mnist.py

這個文件用於讀取simple_weight.pkl來獲得已經計算好的權重,代入計算用10000張圖片來測試,計算精確度。
這個圖因爲比較小,備註也少,是完整的

在這裏插入圖片描述

.
.

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.

numpy.frombuffer(buffer, dtype=float, count=-1, offset=0)
在這裏插入圖片描述
這裏牽扯很多,另外一篇寫這個的。
https://blog.csdn.net/qq_42731466/article/details/83480156

基本文檔
https://docs.scipy.org/doc/numpy-1.13.0/user/basics.types.html
更高級的
https://docs.scipy.org/doc/numpy-1.13.0/user/basics.rec.html#structured-arrays

.
.

3.pickle
https://docs.python.org/3.6/library/pickle.html
有空再展開。

.
.
.
.
.

3. 舉例

小例子做擴展,可完全跳過。

結尾
在我的圖上,加了很多代碼的註釋,作爲圖層分開放了,爲了看起來簡潔明瞭我將圖層隱去了。不隱藏的時候,代碼圖是這樣的,需要的請自行下載dwg,不下載也不影響,該說的都說完了。

紅色是變量監控,橘色是註釋,紫色是詳細註釋和超鏈接,ctrl一下就能飛過去。
這是我能截取的最大的範圍了,縮小字會看不清,所以上面的圖我把註釋去掉了。
在這裏插入圖片描述
下載鏈接
https://download.csdn.net/download/qq_42731466/10748151

更新時間
2018.10.28——更新show_mnist的代碼圖和幾處重要的語法。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章