3 11比較運算 排序

2021-10-25 08:06:12 字數 3943 閱讀 4075

import torch

a = torch.rand(2,

3)b = torch.rand(2,

3)print

(a)print

(b)

tensor([[0.2165, 0.9520, 0.2256],

[0.0793, 0.1522, 0.3120]])

tensor([[0.8147, 0.2384, 0.1755],

[0.6711, 0.9985, 0.4503]])

#維度相同才可以比較

print

(torch.eq(a, b)

)#返回乙個tensor

print

(torch.equal(a, b)

)#返回乙個值

print

(torch.ge(a, b)

)#>=

print

(torch.gt(a, b)

)#>

print

(torch.le(a, b)

)#<=

print

(torch.lt(a, b)

)#<

print

(torch.ne(a, b)

)#!=

tensor([[false, false, false],

[false, false, false]])

false

tensor([[false, true, true],

[false, false, false]])

tensor([[false, true, true],

[false, false, false]])

tensor([[ true, false, false],

[ true, true, true]])

tensor([[ true, false, false],

[ true, true, true]])

tensor([[true, true, true],

[true, true, true]])

####

a = torch.tensor([[

1,4,

4,3,

5],[

2,3,

1,3,

5]])

print

(a.shape)

print

(torch.sort(a, dim=1,

descending=

false))

#dim=1按行排序

torch.size([2, 5])

torch.return_types.sort(

values=tensor([[1, 3, 4, 4, 5],

[1, 2, 3, 3, 5]]),

indices=tensor([[0, 3, 1, 2, 4],

[2, 0, 1, 3, 4]]))

##topk

a = torch.tensor([[

2,4,

3,1,

5],[

2,3,

5,1,

4]])

print

(a.shape)

print

(torch.topk(a, k=

2, dim=

1, largest=

false))

#largest=false從小開始,公升序

#largest=true從大開始,降序

#返回最大前k個數值及其索引

print

(torch.kthvalue(a, k=

2, dim=1)

)#返回最小第k個數值及其索引

print

(torch.kthvalue(a, k=

2, dim=0)

)

torch.size([2, 5])

torch.return_types.topk(

values=tensor([[1, 2],

[1, 2]]),

indices=tensor([[3, 0],

[3, 0]]))

torch.return_types.kthvalue(

values=tensor([2, 2]),

indices=tensor([0, 0]))

torch.return_types.kthvalue(

values=tensor([2, 4, 5, 1, 5]),

indices=tensor([1, 0, 1, 1, 0]))

a = torch.rand(2,

3)print

(a)print

(a/0

)print

(torch.isfinite(a)

)#infinite表示有界

print

(torch.isfinite(a/0)

)print

(torch.isinf(a/0)

)#inf表示無界

print

(torch.isnan(a)

)

tensor([[0.1947, 0.8056, 0.3992],

[0.5999, 0.6308, 0.9381]])

tensor([[inf, inf, inf],

[inf, inf, inf]])

tensor([[true, true, true],

[true, true, true]])

tensor([[false, false, false],

[false, false, false]])

tensor([[true, true, true],

[true, true, true]])

tensor([[false, false, false],

[false, false, false]])

import numpy as np

a = torch.tensor([1

,2, np.nan]

)print

(torch.isnan(a)

)#是否是空

a = torch.rand(2,

3)print

(a)print

(torch.topk(a, k=

2, dim=

1, largest=

false))

print

(torch.topk(a, k=

2, dim=

1, largest=

true

))

tensor([false, false,  true])

tensor([[0.6729, 0.4109, 0.6635],

[0.6265, 0.6905, 0.9145]])

torch.return_types.topk(

values=tensor([[0.4109, 0.6635],

[0.6265, 0.6905]]),

indices=tensor([[1, 2],

[0, 1]]))

torch.return_types.topk(

values=tensor([[0.6729, 0.6635],

[0.9145, 0.6905]]),

indices=tensor([[0, 2],

[2, 1]]))

3 1 1 排序 內排序

穩定與非穩定 相等的數相對位置是否改變 內排序與外排序 是否用外存儲存 思路 從第乙個元素開始構建有序的排序 後面未排序的逐一往前面有序的排序中找適當位置插入 時間複雜度 分類 穩定 內排序 插入排序 void insertion sort int data,int len 比較和交換次數 int ...

SWUSTOJ 311 數字的運算

思路 找到運算子的位置,然後計算啊a,b的值,然後進行運算即可 ps 要注意可能會有負數的出現 這導致我wa了兩次 include define rep for int i 0 i define repn for int i 1 i n i define ll long long define pb...

Shell 比較運算

shell字串比較 判斷是否為數字 二元比較操作符,比較變數或者比較數字.注意數字與字串的區別.整數比較 eq 等於,如 if a eq b ne 不等於,如 if a ne b gt 大於,如 if a gt b ge 大於等於,如 if a ge b lt 小於,如 if a lt b le 小...