self.parser.add_argument('--lr_use', action='store_true', default=False)
- # 終端運行不加入--lr_use,lr_use的值爲default: False, 加上--lr則爲True;
- # 有default值的時候,running時不申明爲default;
- # 申明的話,如果是store_false則默認True,如果是store_true則默認False;# 選擇特定編號的GPU運行
- 調用特定編號的GPU運行:
CUDA_VISIBLE_DEVICES=1 python my_script.py # 方法一
os.environ["CUDA_VISIBLE_DEVICES"] = "2" # 方法二
torch.cuda.set_device(id) # 方法三
- ImportError: libcudart.so.10.1: cannot open shared object file: No such file or directory
在 ~/.bashrc 添加cuda路徑(如下),然後 source ~/.bashrc 生效;
export PATH=/usr/local/cuda-10.1/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-10.1/lib64
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/cuda-10.1/lib64
- 服務器打印中文報錯:
print(chn_str.encode('utf-8').decode('latin1'))
- 文件中的unicode轉中文(讀取時以str格式讀取)
chn = char.encode('utf-8').decode('unicode-escape')
- 中文文本不是utf-8編碼轉爲utf-8: Notepad++打開,複製粘貼到新建的文本中即可
- windows 將 utf-8 with bom 轉化爲 utf-8 without bom
bom_file = os.path.join(file_dir, filename)
s = open(bom_file, mode='r', encoding='utf-8-sig').read()
open(bom_file, mode='w', encoding='utf-8').write(s)
- 讀取 .t7 文件
import torchfile
dict = torchfile.load('vocab.t7')
- 讀取 .json 文件
import json
js_file = 'cocotext.json'
file = open(js_file, "rb")
dict_info = json.load(file)
for k, v in dict_info.items():
print(k, v)
- 服務器配置生效
source ~/.bashrc
- 除Pycharm 文件 read-only status
sudo chown -R username dir
- 國內鏡像安裝第三方庫 e.g. pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple
清華大學鏡像
https://pypi.tuna.tsinghua.edu.cn/simple/
阿里雲
http://mirrors.aliyun.com/pypi/simple/
中科大鏡像
https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣鏡像
http://pypi.douban.com/simple/
中科大鏡像2
http://pypi.mirrors.ustc.edu.cn/simple/
- 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 --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --set show_channel_urls yes
# 創建虛擬環境
conda create -n env_name python=3.6
conda activate env_name
conda install pytorch=1.0 cudatoolkit=10.0
conda install pytorch torchvision cudatoolkit=10.0 -c pytorch-nightly
conda install --use-local pkg_file
# 退出環境
conda deactivate
# 刪除環境
conda remove -n env_name --all
- Linux 命令(wc, head, tail)
wc *.txt -l # 行數
wc *.txt -c # 字節數(大小)
wc *.txt -m # 字符數(字數)
wc *.txt -L # 最長的行的字符數
wc *.txt -w # 單詞數
head -n 10 1.txt # 查看 1.txt 開頭10行
tail -n 10 1.txt # 查看 1.txt 末尾10行
head 1.txt # default: 10 lines
tail 1.txt # default: 10 lines
# 進入 su 模式
sudo passwd # 連續輸入和確認密碼兩次
su # 進入超級用戶模式
# Terminal 粘貼
shift + ins
# 查找 string
/str # 按n跳轉到下一個匹配到的stirng
# 全局替換
:%s/str1/str2/g
- 防止含有
autograd
歷史的可微變量的累積
# 不要直接寫loss,寫成 loss.item() 或者 float(loss)
if i % 100 == 0:
print('batch %d, loss %s' % (i, loss.item()))
loss.backward()
total_loss += loss.item()