DL代码能力提升1

主要根据实验中不太理解的地方进行总结:

from collections import Counter计数器

from collections import Counter
s = "hello-python-hello-world"
a = Counter(s)
print(a)
# 结果 
Counter({'-': 3, 'd': 1, 'e': 2, 'h': 3, 'l': 5, 'n': 1, 'o': 4, 'p': 1, 'r': 1, 't': 1, 'w': 1, 'y': 1})

torch.softmax dim

其中dim=-1时,相当于对最后一维度。

pytorch中的 nelement() 可以统计 tensor (张量)中 元素的个数。

import torch
x = torch.randn(size =(4,3,5,6))
print(x.nelement())
结果是:
4*3*5*6

args中怎么定义bool类型参数:

parser.add_argument('--thresh_warmup',default=False,action="store_true")

python全局变量最好怎么申明?

a=0
def f():
    global a
    print(a)

f()

及在函数外申请,并在函数内申明。

ge函数

select=max_probs.ge(threshold)
#select
[1,0,0,1,1]

即逐元素对比,如果max_prosb[i]>=threshold,则为1,否则为0

猜你喜欢

转载自blog.csdn.net/qq_50213874/article/details/129572500
今日推荐