PYthon deep learning is based on Pytorch study notes
編輯:Python
This blog post is studying the books of Wu Maogui and others《Python深度學習基於Pytorch》(英文名為《Deep Learning with Python and Pytorch》)的時候,some notes taken,There are also problems that may occur when the version is updated. The version of the book in my hand is2019年10First printing of the first edition of the month,已經過去兩年了,I don't know what problems will be encountered in learning.我的Python版本是3.7.7.Will update if there is a problem,Although some of the books I was reading before were updated synchronously,Some pigeons,But believe me I can.
書中第18頁,1.6Section Generic Functions,1.math與numpy函數的性能比較.The function used for timing here istime.clock()記錄開始和結束時間,and use this as an estimate.However, this prompt will appearDeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead,意思是說在3.8版本之後time.clock將會消失,所以使用time.process_time()to be replaced. 另外,The record on my computer,math.sin和numpy.sin的時間對比,可以看到numpy.sin比math.sin運行速度快了14倍.
in accordance with the book25頁資料(第2.2.2節),使用conda安裝pytorch的時候,During the installation process, you will encounter that the name cannot be foundconda的module,after the error,原本的condaenvironment is also unavailable.The problem seems to be because if the default is inbase環境下面安裝pytorch的時候,will overwrite thepython包,resulting in a version number mismatch.解決方案是使用rm -rf ./anaconda3,先把anacondaThe environment is completely uninstalled(because I don't knowanacondaInternal exactly where there is a problem,Can only be completely uninstalled). 接下來使用conda create pytorchenv python=3.7Create an installation specificallypytorch環境的conda環境.and switch environmentsconda activate pytorchenv,在這個環境中安裝torch即可conda install pytorch torchvision torchaudio cudatoolkit=11.1 -c pytorch -c nvidia.
書中27頁(第2.3節),Configure Local Access ServerJupyter Notebook的時候,Encountered a problem that the machine and the computer server cannot be connected.The reason may be that the firewall of the server is turned on,A connection needs to be established between the local machine and the serverssh安全連接.sshThe secure connection command isssh 服務器的用戶名@服務器IP -L 127.0.0.1:1234:127.0.0.1:8888 -p 端口號 The overall access process is as follows:First adjust on the serverjupyter的配置文件,包括端口號/密碼/允許遠程訪問/allow allip訪問/Close the browser window by default.Next, you need to establish and server on this machinejupyter notebook的ssh連接,Note the port number for specifying the server connection,Otherwise it will be inaccessible due to firewall denial.Finally, in the native browser throughip:端口就可以訪問.
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
繪圖的時候,If you do not want to add the values of the abscissa and ordinate,可以使用plt.xticks([])來禁用.
繪圖的時候,when drawing an image,plt.scatter(x, y, label='something')直接添加label.without having to draw additionally after this.
當使用datasets.ImageFolder(路徑名)的時候,The system will display all the pictures under the folder,Sort and label by folder,Only need to put one class of file/Pictures are placed in the same folder,就可以使用datasets.ImageFolderThe batch build file data set.
The following is a brief introduction to how to use the local browser,查看服務器端的tensorboard可視化樣例. tensorboardX在WebThe method of displaying on the terminal is as follows; 1).在服務器端使用conda activate pytorchenv,進入pytorch環境下. 2).tensorboard --logdir=~/logs/logs --port 6006,It's made herelog地址之後,使用pythonAlso need to be output to the corresponding taglog文件夾下面. 3).使用sshcommand to establish a connection between the machine and the server.ssh 用戶名@IP地址 -L 127.0.0.1:6006:127.0.0.1:6006 -p 6138 4).Login in native browserlocalhost:6006即可.
如果想要刪除tensorboardsome images in,Just need to clicklogs文件夾,Delete the corresponding label file.只有重啟tensorboard服務之後,to find out that this image has been deleted.
有一個疑問,is about the normalization problem.The mean is subtracted and divided by the standard deviation during training,The subtraction should be the mean and standard deviation of the training dataset.During the testing phase and the evaluation phase,Should also need to subtract the mean and divide by the standard deviation,Which mean and standard deviation should I use??mean and standard deviation of the training set,Or the mean and standard deviation of the test set?? Chapter 5 of this book5.3.2Section corresponding code,The method shown is,在預測的時候,Also subtract the mean and standard deviation on the training data set,Not sure if this is the right way to do it.
在書中86page place,Do a batch regularization、Batch random zeroing operation,Used to show that using these methods improves generalization properties.But after my actual verification,After discovering that using these methods,The loss function drops slower.Not sure if this is consistent with what is presented in the book.
In the test data set to get the loss value calculation
The loss value obtained by the operation on the training data set But the result of running in the book is as follows:dropoutthe loss value obtained,is significantly smaller thanoriginal原始數據的.這就有點讓人費解了.