python語言程序設計,python 近期用到的基礎知識匯總(八)

 2023-10-06 阅读 30 评论 0

摘要:1.pytorch 的scatter()函數 scatter()?和?scatter_()?的作用是一樣的,只不過 scatter() 不會直接修改原來的 Tensor,而 scatter_() 會. scatter(dim, index, src) 的參數有 3 個 dim:沿著哪個維度進行索引index:用來 scatter 的元素索引src:用來

1.pytorch 的scatter()函數

scatter()?和?scatter_()?的作用是一樣的,只不過 scatter() 不會直接修改原來的 Tensor,而 scatter_() 會.

scatter(dim, index, src) 的參數有 3 個

  • dim:沿著哪個維度進行索引
  • index:用來 scatter 的元素索引
  • src:用來 scatter 的源元素,可以是一個標量或一個張量
>>> torch.zeros(3, 5).scatter_(0, torch.tensor([[0, 1, 2, 0, 0], [2, 0, 0, 1, 2]]), 2)
tensor([[2., 2., 2., 2., 2.],[0., 2., 0., 2., 0.],[2., 0., 2., 0., 2.]])

?上面的例子中第一個參數‘0’代表的是第0維度,例子中也就是行。第二個參數‘torch.tensor([[0, 1, 2, 0, 0], [2, 0, 0, 1, 2]])’ 代表第元素索引,簡單來說那些地方要進行更改元素值的操作,例如里面第一行的[0,1,2,0,0]代表分別要對它們同列的0,1,2,0,0處元素值進行更改。第三個參數‘2’代表最終更改值設定為2.

python語言程序設計,參考:https://www.cnblogs.com/dogecheng/p/11938009.html

擴展到3d矩陣

>>> torch.zeros(2, 3, 5).scatter_(1, torch.tensor([[[0, 1, 2, 0, 0], [2, 0, 0, 1, 2]],[[0, 1, 2, 0, 0], [2, 0, 0, 1, 2]]]), 2)
tensor([[[2., 2., 2., 2., 2.],[0., 2., 0., 2., 0.],[2., 0., 2., 0., 2.]],[[2., 2., 2., 2., 2.],[0., 2., 0., 2., 0.],[2., 0., 2., 0., 2.]]])

對比一下看第一個維度參數是確定在哪個維度變換。(一般語義分割掩碼轉變成one-hot標簽時用到)

?

2.python中雙冒號[::]的使用

python 列表?1. range(n)生成[0,n)區間的整數 (左閉右開)

>>> data=np.array(range(10))
>>> data
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

2.data[start:end:step] 取值范圍[start,end)? ? (左閉右開)

>>> data[0::1]
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> data[0::2]
array([0, 2, 4, 6, 8])
>>> data[0::3]
array([0, 3, 6, 9])
>>> data[0:4:1]
array([0, 1, 2, 3])

3.當step等于負數的時候,從右向左取數。注意:numpy支持負數,torch.tensor不支持

>>> data[::-1]
array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
>>> data[::-2]
array([9, 7, 5, 3, 1])

4.a[:end]:取得范圍為[0,end)

>>> data[2:]
array([2, 3, 4, 5, 6, 7, 8, 9])
>>> data[:2]
array([0, 1])
>>> data[-2:]
array([8, 9])
>>> data[:-2]
array([0, 1, 2, 3, 4, 5, 6, 7])

python編程,5.yolov5中的Focus模塊代碼如下:

class Focus(nn.Module):# Focus wh information into c-spacedef __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True):  # ch_in, ch_out, kernel, stride, padding, groupssuper(Focus, self).__init__()self.conv = Conv(c1 * 4, c2, k, s, p, g, act)      # 這里輸入通道變成了4倍def forward(self, x):  # x(b,c,w,h) -> y(b,4c,w/2,h/2)return self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1))

其中[x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]],讓我一頭霧水。最后按照嘗試1,2,3,4的方式理解:

>>> a=np.array([[[1,2,1,2],[3,4,3,4],[1,2,1,2],[3,4,3,4]],[[1,2,1,2],[3,4,3,4],[1,2,1,2],[3,4,3,4]],[[1,2,1,2],[3,4,3,4],[1,2,1,2],[3,4,3,4]]])
>>> a
array([[[1, 2, 1, 2],[3, 4, 3, 4],[1, 2, 1, 2],[3, 4, 3, 4]],[[1, 2, 1, 2],[3, 4, 3, 4],[1, 2, 1, 2],[3, 4, 3, 4]],[[1, 2, 1, 2],[3, 4, 3, 4],[1, 2, 1, 2],[3, 4, 3, 4]]])
>>> a[...,::2,::2]
array([[[1, 1],[1, 1]],[[1, 1],[1, 1]],[[1, 1],[1, 1]]])
>>> a[:,::2,::2]
array([[[1, 1],[1, 1]],[[1, 1],[1, 1]],[[1, 1],[1, 1]]])
>>> a[:,::-2,::-2]
array([[[4, 4],[4, 4]],[[4, 4],[4, 4]],[[4, 4],[4, 4]]])
>>> a[:,::-1,::-1]
array([[[4, 3, 4, 3],[2, 1, 2, 1],[4, 3, 4, 3],[2, 1, 2, 1]],[[4, 3, 4, 3],[2, 1, 2, 1],[4, 3, 4, 3],[2, 1, 2, 1]],[[4, 3, 4, 3],[2, 1, 2, 1],[4, 3, 4, 3],[2, 1, 2, 1]]])

參考:https://blog.csdn.net/weixin_42135399/article/details/101150106,https://blog.csdn.net/qq_39056987/article/details/112712817。

?

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/123047.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息