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()