常用命令

Note

在日常使用中,总是用到一些命令,在这些统一做归类整理

Conda

创建环境

1
conda create --name py36 python=3.6

激活环境

1
source activate py36

取消激活

1
conda deactivate py36

安装 pytorch(CUDA=11.7)

1
conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia

导出环境

1
conda env export > filename

导入环境

1
conda env create -n wbw_tgan -f tgan.env

PIP

Note

有些包 Conda 无法安装,pip 却可以。个人实践发现在迁移环境的时候,使用 pip 的成功率要高过 conda。可以用 conda 创建环境,在进入环境后用 pip 来安装。

导出依赖

1
pip freeze > requirements.txt

导入依赖

1
pip install -r requirements.txt

如果依赖中包含 Pytorch,还需要加上额外的索引。如 Pytorch 1.10.1:

1
pip install -r requirements.txt -f https://download.pytorch.org/whl/cu111/torch_stable.html

具体加什么索引参考 [Pytorch 官网的版本页](Previous PyTorch Versions | PyTorch)

CPU 占用

1
top

进入 top 后,CPU% 一列即为上一次更新到现在进程占用 CPU 的时间,这个时间间隔默认为 5s,可以通过 -d 参数调节。

会发现 CPU% 大于 100 的情况,是因为是以单个核心来作为基数的,如果要以全部作为基数,需要在进入 top 后,输入大写的 I 来切换。会提示 Irix mode off 即可。

Bash Shell

不知为啥,在 WSL 的模型训练速度远慢过在 Windows 下的速度,尤其是一些 CPU 任务。

好在使用 Conda 的话,可以在 Windows 下同样执行 Shell 脚本

1
conda install m2-base

貌似同样会影响速度

接续执行指令

  • 等待上一个命令完成再执行下一个命令
  • 将输出重定向
  • 会话退出后命令继续执行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
nohup sh -c '
command >> command.log 2>&1
command >> command.log 2>&1
command >> command.log 2>&1
...
' &

nohup sh -c '
python -u synthetic_main.py --dataset synthetic_0 > synthetic.log 2>&1
python -u synthetic_main.py --dataset synthetic_1 >> synthetic.log 2>&1
python -u synthetic_main.py --dataset synthetic_2 >> synthetic.log 2>&1
python -u synthetic_main.py --dataset synthetic_3 >> synthetic.log 2>&1
python -u synthetic_main.py --dataset synthetic_4 >> synthetic.log 2>&1
' &

nohup sh -c '
python -u acs_income_main.py --dataset acs_income_0 > acs_income.log 2>&1
python -u acs_income_main.py --dataset acs_income_1 >> acs_income.log 2>&1
python -u acs_income_main.py --dataset acs_income_2 >> acs_income.log 2>&1
python -u acs_income_main.py --dataset acs_income_3 >> acs_income.log 2>&1
python -u acs_income_main.py --dataset acs_income_4 >> acs_income.log 2>&1
' &

nohup sh -c '
python -u main_age.py --dataset compas_score_0 > age.log 2>&1
python -u main_age.py --dataset compas_score_1 >> age.log 2>&1
python -u main_age.py --dataset compas_score_2 >> age.log 2>&1
python -u main_age.py --dataset compas_score_3 >> age.log 2>&1
python -u main_age.py --dataset compas_score_4 >> age.log 2>&1
' &

nohup sh -c '
python -u main.py --dataset law_0 > law.log 2>&1
python -u main.py --dataset law_1 >> law.log 2>&1
python -u main.py --dataset law_2 >> law.log 2>&1
python -u main.py --dataset law_3 >> law.log 2>&1
python -u main.py --dataset law_4 >> law.log 2>&1
' &

nohup sh -c '
python -u main_no_bi.py --dataset compas_score_0 > main_no_bi.log 2>&1
python -u main_no_bi.py --dataset compas_score_1 >> main_no_bi.log 2>&1
python -u main_no_bi.py --dataset compas_score_2 >> main_no_bi.log 2>&1
python -u main_no_bi.py --dataset compas_score_3 >> main_no_bi.log 2>&1
python -u main_no_bi.py --dataset compas_score_4 >> main_no_bi.log 2>&1
' &