深度学习常用公式与命令总结(更新中)

一、命令&代码

1.1 requirements.txt

生成:
pip install pipreqs
pipreqs . --encoding=utf8 --force
导入:
pip install -r requirements.txt

1.2 tensor

torch.Tensor(data):float32类型,不共享内存
torch.tensor(data):原有类型,不共享内存
torch.as_tensor(data):原有类型,共享内存
torch.from_numpy(data):原有类型,共享内存

1.3 shape

shape[0]:第一维
shape[1]:第二维
shape[-1]:最后一维

1.4 torch.jit.annotate

#Telling TorchScript that this empty dictionary is a (str -> int) dictionary
# instead of default dictionary type of (str -> Tensor).
d = torch.jit.annotate(Dict[str, int], {})  # {}表示字典  []表示列表

# Without `torch.jit.annotate` above, following statement would fail because of
# type mismatch.
d["name"] = 20

1.5 unbind / stack

# Removes a tensor dimension
# Returns a tuple of all slices along a given dimension, already without it.
a = torch.rand(3, 4)
print(a)
print(a.unbind(0))
print(a.unbind(1))
xmin, ymin, xmax, ymax = a.unbind(1)
print(torch.stack((xmin, ymin, xmax, ymax), dim=1))
tensor([[0.5002, 0.5609, 0.6350, 0.7716],
        [0.2276, 0.3949, 0.3382, 0.8615],
        [0.4510, 0.6123, 0.1608, 0.0649]])
(tensor([0.5002, 0.5609, 0.6350, 0.7716]), tensor([0.2276, 0.3949, 0.3382, 0.8615]), tensor([0.4510, 0.6123, 0.1608, 0.0649]))
(tensor([0.5002, 0.2276, 0.4510]), tensor([0.5609, 0.3949, 0.6123]), tensor([0.6350, 0.3382, 0.1608]), tensor([0.7716, 0.8615, 0.0649]))
tensor([[0.5002, 0.5609, 0.6350, 0.7716],
        [0.2276, 0.3949, 0.3382, 0.8615],
        [0.4510, 0.6123, 0.1608, 0.0649]])

1.6 torchvision._is_tracing()

待补充

1.7 torch.onnx

Open Neural Network Exchange,开放神经网络格式,tf、pytorch、caffee等深度学习框架都能转换为此格式,转换后不再依赖原先的框架;通过这个格式能在各个框架中进行转换。
待补充

1.8 view / reshape

# view和reshape功能是一样的,先展平所有元素在按照给定shape排列
# view函数只能用于内存中连续存储的tensor,permute等操作会使tensor在内存中变得不再连续,此时就不能再调用view函数
# 当然也可以先调用.contiguous()方法
# reshape则不需要依赖目标tensor是否在内存中是连续的

# layer-[batch_size, anchors_num_per_position * (C or 4), height, width]
layer = layer.view(N, -1, C, H, W)
# 调换tensor维度
layer = layer.permute(0, 3, 4, 1, 2)  # [N, H, W, -1, C]
layer = layer.reshape(N, -1, C)

1.9 torch.bmm(a, b) 两个tensor矩阵的乘法

proj_query = x.view(m_batchsize, C, -1)
proj_key = x.view(m_batchsize, C, -1).permute(0, 2, 1)
energy = torch.bmm(proj_query, proj_key)
# tensor a 的size为(b,h,w)
# tensor b 的size为(b,w,m)
# 输出维度 (b,h,m)

1.10 torch.cat

对多个tensor在dim维度上进行拼接。

torch.cat((x, y), 1)  # dim=1

1.11 torch.chunk

在dim维度上将tensor拆分成chunks份。

xs = torch.chunk(x, 2, dim=1)
x_cat = torch.cat((xs[0], y, xs[1], y), 1)

二、CNN

2.1 感受野

正推:output_size = (input_size + 2 * padding_size − ksize) / stride+1
反推:input_size = (output_size - 1) * stride + ksize -2 * padding_size

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值