Everyday Note 20200406

Posted by LIU Blog on April 6, 2020

微信公众号:[有得札记] 或学而有得,或思而有得,辄札记之——清 王筠 《菉友肊说》

python中’if not x:’、’if x is not None:’ 和 ‘ if not x is None:’的区别

这三者有很大区别,对于判断非空来说,if x is not None是最清晰明了的写法。

使用if not x这种写法的前提是:必须清楚x等于None, False, 空字符串””, 0, 空列表[],空字典{},空元组()时对判断没有影响才行。

if not x is None:这种写法有可能被误解为if / not x /is None,非常令人迷惑。

具体可以查看链接

linux 以易读格式查看文件大小

通常在Linux系统中查看文件我习惯用ls -l或者ll来查看,但是这样子查看文件大小是字节大小来显示,其实可以更易读的形式来显示,即ls -lh ,使用“-h”参数时,会根据文件的大小选择显示的单位是“K”、“M”还是“G”。

如果希望指定显示的单位,可以使用“–block-size”参数,但是用不同单位显示的时候,使用的是进一法,一个1k的东西,使用G为单位显示的是,会显示为“1G”。

所以,还是使用“-h”显示起来,更加好一点,指定size反而可能造成误解。

具体可以查看链接

PyTorch CPU版的安装(Windows系统)

推荐用conda,我直接下载Anaconda3。

可能会出现CondaHTTPError: HTTP 000 CONNECTION FAILED 这个报错,这是因为国外的conda源太慢了,换源即可。

添加清华源

conda  config --add channels  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda  config --add channels  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda  config --set show_channel_urls yes

添加第三方conda源:

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

只安装CPU版PyTorch

conda install pytorch torchvision cpuonly

具体可以查看链接

Tensorflow CPU版的安装(Windows系统)

用pip安装就行

pip install tensorflow-cpu

安装后在导入tensorflow包过程中,可能会出现”Could not find the DLL(s)’msvcp140_1.dll “的报错。

根据提示信息,去微软的官网下载Visual C++即可:

这个报错是由于2.1版本对Visual C++的依赖,这个bug实际给的报错信息非常多而宽泛,但是只要安装Visual C++的支持即可解决。

具体可以查看链接