Python
不想待在银河系
这个作者很懒,什么都没留下…
展开
-
pytorch shuffle tensor的函数(打乱张量)
np.random.shuffle能打乱numpy,但是对tensor没用查阅资料后发现,可以用torch.randperm实现if __name__ == '__main__': # init t = torch.tensor([[1,2],[3,4]]) print(t) # shuffle tensor idx = torch.randperm(t.nelement()) t = t.view(-1)[idx].view(t.size())翻译 2022-01-11 21:53:23 · 3672 阅读 · 0 评论 -
nn.Linear和kernel=1的nn.Conv的区别
相同点:都是线性运算,可以看成矩阵相乘,感受野都为1不同点:pytorch中初始化方案不同,nn.Conv有更好的初始化速度不同,Linear的速度要比nn.Conv快一个数量级,因为Linear是用高效的矩阵相乘实现的,而Conv是卷积核移动实现的输入尺寸不同,Conv是[batch, input_channel, H, W,…],linear是[batch, …, input_channel]但这一点其实不重要linear速度快很多,如果tensor的尺寸方便调整的话还是建议用linear。转载 2021-11-14 23:20:15 · 2452 阅读 · 0 评论 -
pytroch .require_grad不起作用/无效
试了半天,发现代码放在了with torch.no_grad()里,取消掉就好了。原创 2021-09-30 20:19:42 · 445 阅读 · 0 评论 -
pytorch 对tensor进行高斯模糊
# From https://discuss.pytorch.org/t/is-there-anyway-to-do-gaussian-filtering-for-an-image-2d-3d-in-pytorch/12351/3import torchimport mathimport torch.nn as nndef get_gaussian_kernel(kernel_size=3, sigma=2, channels=3): # Create a x, y coordinate转载 2021-09-21 18:08:30 · 7181 阅读 · 1 评论 -
pytorch 优化器采取不同学习率
def configure_optimizers(self): params = list(model.named_parameters()) def is_backbone(n): return 'bert' in n grouped_parameters = [ {"params": [p for n, p in params if is_backbone(n)], 'lr': args.lr}, {"params": [p for n, p.转载 2021-09-14 09:05:27 · 743 阅读 · 0 评论 -
pytorch 只保留tensor的最大值或最小值,其他位置置零
如下,x是输入张量,dim指定维度,max可以替换成minimport torchif __name__ == '__main__': x = torch.randn([1, 3, 4, 4]).cuda() mask = (x == x.max(dim=1, keepdim=True)[0]).to(dtype=torch.int32) result = torch.mul(mask, x) print(x) print(mask) p翻译 2021-09-12 16:29:44 · 6335 阅读 · 1 评论 -
pytorch/python 用pyinstrument统计代码段运行时间
用法:from pyinstrument import Profilerprofiler = Profiler()profiler.start()# code you want to profileprofiler.stop()profiler.print()官网:pyinstrument 例子:from pyinstrument import Profilerimport torchif __name__ == '__main__': profiler = Prof翻译 2021-08-18 11:28:27 · 441 阅读 · 0 评论 -
Pytorch报错:One of the differentiated Tensors does not require grad
原因:用到了autograd.grad求导函数,而其中一个求导变量require_grad属性是False解决方法单步调试发现,原来是建立变量的时候没有设置requires_grad=True,因此改成True就好了。torch.randn(batch, latent_dim, device=device, requires_grad=True)...原创 2021-05-20 19:53:07 · 8949 阅读 · 1 评论 -
Pytorch Kaiming 初始化(Initialization)中fan_in和fan_out的区别/应用场景
fan_in如果权重是通过线性层(卷积或全连接)隐性确定的,则需设置mode=fan_in。例子:import torchlinear_layer = torch.nn.Linear(node_in, node_out)init.kaiming_normal_(linear.weight, mode=’fan_in’)output_data = relu(linear_layer(input_data))fan_out如果通过创建随机矩阵显式创建权重,则应进行设置mode=‘fan_ou翻译 2021-02-28 23:35:15 · 4690 阅读 · 3 评论 -
Pytorch直接对张量进行旋转(rotate tensor)
方法1:利用affine_grid函数参考链接import torchimport torch.nn.functional as Fimport numpy as npimport matplotlib.pyplot as pltdef get_rot_mat(theta): theta = torch.tensor(theta) return torch.tensor([[torch.cos(theta), -torch.sin(theta), 0],转载 2021-01-17 10:22:27 · 11220 阅读 · 0 评论 -
pytorch维度不同的张量(tensor)相加规则
网上没找到,也不知道哪里有“张量加法”的源代码,就自己试了一下,大概有下面的规律。如果有错误,希望大家指正!规律1:如果相加的两个张量维度不一致,那么首先把维度低的那个张量从右边和维度高的张量对齐例如下面的代码,b的维度低一些,所以和a相加的时候b的维度就会先扩充为[1,1,5,6]。a = torch.ones([8, 4, 5, 6])print('a =',a.size())b = torch.ones([5, 6])print('b =',b.size())c = a+bprint原创 2020-12-07 20:37:53 · 25582 阅读 · 7 评论