1.squeeze()和unsqueeze()函数
unsqueeze(1)为增加一个维度当作第1维度(从0开始),可以这么理解,在它的上一个维度下,创建一个抽象的空间,该空间为第1维度,然后把它上一个维度下的所有东西,放到这个空间中
【学习笔记】pytorch中squeeze()和unsqueeze()函数介绍_Jaborie203的博客-优快云博客_squeeze
2.torch的cat
PyTorch的torch.cat_my-GRIT的博客-优快云博客_torch.cat
3.expand
expand传的参数为,各个维度期望被扩展的大小,-1为保持不变
Example:: >>> x = torch.tensor([[1], [2], [3]]) >>> x.size() torch.Size([3, 1]) >>> x.expand(3, 4) tensor([[ 1, 1, 1, 1], [ 2, 2, 2, 2], [ 3, 3, 3, 3]]) >>> x.expand(-1, 4) # -1 means not changing the size of that dimension tensor([[ 1, 1, 1, 1], [ 2, 2, 2, 2], [ 3, 3, 3, 3]]) """ return _te.Tensor(*(), **{})
4.torch.split
torch.split(要切分的张量, 每个块的大小, dim=1)
torch.split()_skycrygg的博客-优快云博客_torch.split
5.一文学会 Pytorch 中的 einsum - 知乎
6.torch.sum()的用法_笨笨的蛋的博客-优快云博客_.sum()
sum(3)就是把第三个维度压缩,最后减少一个维度
7.torch.save()保存模型参数
torch.save(model.state_dict(), f'transformer_best.pth')
8. 加载模型
model.parameters()与model.state_dict() - 知乎
model.load_state_dict(torch.load(f'transformer_best.pth'))
打印检查点的权重
checkpoint = load_fsspec(restore_path, map_location="cpu")
print("打印检查点的模型")
mdd = checkpoint["model"]
for key in mdd:
print(key)
打印模型权重
model_dict = model.state_dict()
print("打印权重形式")
for key in model_dict:
print(key)
9.训练开始时的权重初始化是否影响载入预训练权重?
不影响
1.原模型随机初始化的权重
model_dict = model.state_dict()
print("打印原模型")
print(model_dict["emb_l.weight"])
tensor([[-0.8127, 0.4848, 0.7403, -0.0691]]) 因为随机初始化的,每次训练都不一样
2.打印检查点的模型
(checkpoint的checkpoint[model]已经是model_dict形式了)
checkpoint = load_fsspec(restore_path, map_location="cpu")
print("打印检查点的模型")
mdd = checkpoint["model"]
print(mdd["emb_l.weight"])
tensor([[-0.5100, 0.0040, 0.1548, 0.2193]])
3.载入后的原模型
print("打印载入后模型")
model_dict = self.model.state_dict()
print(model_dict["emb_l.weight"])
tensor([[-0.5100, 0.0040, 0.1548, 0.2193]], device='cuda:0')
4.nn.embedding
通俗讲解pytorch中nn.Embedding原理及使用 - 简书
5.查torch版本
pip list | grep torch
6.下载torch
https://download.pytorch.org/whl/torch_stable.html
7.离线安装wheel
下载wheel到一个
pip install whl文件名
8.CUDA kernel errors might be asynchronously reported at some other API call,so the stacktrace below might be incorrect.
For debugging consider passing CUDA_LAUNCH_BLOCKING=1
和代码本身也有关
如index < sizes[i] && "index out of bounds"` 数组越界
9.梯度反转
pytorch 实现Gradient Flipping 各种坑 - 知乎
10.域适应