手机网站建设请示,安徽省建设工程信息网官方,建设银行吴中支行网站,小程序模板购买文章目录 一、配置环境二、Linux 进程管理总结三、ArgumentParser四、Saving and Loading Models nn.ModulesWarmstarting Model Using Parameters from a Different Model五、切片#xff01;拓展#xff1a; 一、配置环境
github配置环境可以直接赋值到txt中#xff0c;然… 文章目录 一、配置环境二、Linux 进程管理总结三、ArgumentParser四、Saving and Loading Models nn.ModulesWarmstarting Model Using Parameters from a Different Model五、切片拓展 一、配置环境
github配置环境可以直接赋值到txt中然后使用 pip install -r requ.txt 不过需要注意的是如果你原来已经下载了的版本他会直接覆盖掉先uninstall再install
二、Linux 进程管理总结
当使用CtrlZ键盘快捷键暂停了一个进程后该进程会被挂起暂停并且被移动到后台执行。
jobs命令 使用jobs命令可以查看当前终端中的所有作业包括后台和前台作业。在终端中输入jobs输出将显示当前所有作业的状态其中包括被暂停的进程的编号job number和状态。 %N命令 使用%N命令可以将某个后台作业切换到前台运行其中N是被暂停进程的编号。
在按下CtrlZ之后进程将不会继续在前台执行而是暂停在后台。换句话说进程的执行会被中止直到您采取下面的操作。 1、fgforeground当您在终端使用fg命令时它会将一个后台作业切换到前台执行使该作业占用终端输入和输出并让您可以与该作业进行交互。 2、bgbackground当您在终端使用bg命令时它会将一个前台暂停的作业切换到后台执行让该作业在后台默默地运行不占用终端输入和输出。 但是就算使用bg他还是会输出到终端上来可能是因为你没有使用输出重定向。原因 如果在后台执行的进程仍然占用您的终端输入和输出可能是因为该进程在后台运行时仍然输出大量的信息到终端导致终端被占用。在后台执行的进程如果产生大量输出会在终端上显示这些输出导致终端无法正常响应其他输入。 有几种方法可以解决这个问题
如果希望执行某个命令但又不希望在屏幕上显示输出结果那么可以将输出重定向到 /dev/nullcommand /dev/null 这样是直接丢弃 命令后面加是指在后台运行还可以重定向到txt文件 command process1.txt 使用终端多路复用器Terminal Multiplexer如tmux或screen可以让您在终端上创建多个会话并在后台保持进程运行同时在需要时可以切换到对应会话查看输出。screen reference Reference2 BTW是在后面追加
三、ArgumentParser
标志参数是一种命令行参数类型通常用于在命令行中表示一个布尔值即真True或假False。 标志参数不需要附加参数值而只需在命令行中指定参数名即可
parser.add_argument(--verbose, actionstore_true, helpFlag to enable verbose mode)如果设置为选项参数Optional Arguments
parser.add_argument(--finetune, typestr, defaulthahaha)一旦你在命令里写了--finetune你后面就必须有参数要不然会报如下错误 error: argument --finetune: expected one argument 四、Saving and Loading Models nn.Modules
Fine-tuning微调是指在预训练好的模型基础上通过继续训练fine-tune来适应特定的任务或数据集。通常情况下预训练的模型在大规模的数据集上进行了较长时间的训练学习到了丰富的特征表示。微调的目的是在这些预训练的特征表示的基础上针对特定任务或数据集进行调整以便更好地适应新的任务 Fine-tuning说白了 1.load保存好的模型 -- 你需要知道model.save到底保存的是什么以及model.load到底下载的什么 2.训练几个epoch 学习官方写法you can save any other items that may aid you in resuming training by simply appending them to the dictionary.
torch.save({epoch: epoch,model_state_dict: model.state_dict(),optimizer_state_dict: optimizer.state_dict(),best_acc: best_accloss: loss,...}, PATH)If you have more than one model, you can also store them in the form of dictionary. for instance:
torch.save({modelA_state_dict: modelA.state.dict(),modelB_state_dict: modelB.state.dict(),}, PATH)以后加载模型就按照这个模板写
# 先定义好model和optimizer
model TheModelClass(*args, **kwargs)
optimizer TheOptimizerClass(*args, **kwargs)
# 先用checkpoint下载下来
checkpoint torch.load(PATH)
# 再load_state_dict
model.load_state_dict(checkpoint[model_state_dict])
optimizer.load_state_dict(checkpoint[optimizer_state_dict])
epoch checkpoint[epoch]
loss checkpoint[loss]model.eval()
# - or -
model.train()记住在运行推理之前必须调用 model.eval() 将滤除层和批量归一化层设置为评估模式。如果不这样做推理结果将不一致。 如果希望恢复训练请调用 model.train() 以确保这些层处于训练模式。
Warmstarting Model Using Parameters from a Different Model
modelB.load_state_dict(torch.load(PATH), strictFalse) Partially loading a model or loading a partial model are common scenarios when transfer learning or training a new complex model. Leveraging trained parameters, even if only a few are usable, will help to warmstart the training process and hopefully help your model converge much faster than training from scratch. Whether you are loading from a partial state_dict, which is missing some keys, or loading a state_dict with more keys than the model that you are loading into, you can set the strict argument to False in the load_state_dict() function to ignore non-matching keys. 即当遇到不匹配的情况如
设置strictFalse你可以直接忽略不匹配的键值也就是说只传递匹配的键值。You can simply change the name of the parameter keys in the state_dict that you are loading to match the keys in the model that you are loading into.
e.g. new_state_dict {k.replace(module., ): v for k, v in model_state_dict.items()}
五、切片
定义 s e q u e n c e [ s t a r t : s t o p : s t e p ] sequence[start:stop:step] sequence[start:stop:step] sequence不可以是dict因为dict是无序的。 几种常见的用法 a [0,1,2,3,4,5,6,7]
a[::2]即隔一个选一个结果为 0,2,4,6 a[1::2] 1,3,5,7a[::-1] 7,6,5,4,3,2,1,0a[::-2]7,5,3,1
numbers torch.arange(1, 10) # [1,2,3....9]
evens numbers[1::2] # [2,4,...8]numbers里面的1是指从索引为1的开始
拓展
PyTorch allows a tensor to be a View of an existing tensor. View tensor shares the same underlying data with its base tensor.这么做可以节约开销。
t torch.rand(4, 4)
b t.view(2, 8)
t.storage().data_ptr() b.storage().data_ptr() - True
b[0][0] 3.14
t[0][0] - 3.14t是一个4x4的随机张量b是通过对T进行view操作得到的2x8的张量t和b共享相同的底层数据。因此对b的修改也会反映在t上。 我们日常中用的一些函数很多都是view模式比如 Basic slicing and indexing op, e.g. tensor[0, 2:, 1:7:2] returns a view of base tensor tensor_split(),T,transpose(),unsqueeze(),squeeze(),detach() etc.
所以在save tensor的时候 it saves their storage objects and tensor metadata separately. 例子如下
large torch.arange(1, 1000)
small large[0:5]
torch.save(small, small.pt)
loaded_small torch.load(small.pt)
loaded_small.storage().size() - 999当然你可以改成torch.save(small.clone(), small.pt) 这样就只保存五个了